Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove ext traits #431

Merged
merged 4 commits into from Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/buf/buf_impl.rs
@@ -1,3 +1,5 @@
use crate::buf::{Chain, reader, Reader, take, Take};

use core::{cmp, mem, ptr};

#[cfg(feature = "std")]
Expand Down Expand Up @@ -807,6 +809,87 @@ pub trait Buf {
ret.put(self);
ret.freeze()
}

/// Creates an adaptor which will read at most `limit` bytes from `self`.
///
/// This function returns a new instance of `Buf` which will read at most
/// `limit` bytes.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, BufMut};
///
/// let mut buf = b"hello world"[..].take(5);
/// let mut dst = vec![];
///
/// dst.put(&mut buf);
/// assert_eq!(dst, b"hello");
///
/// let mut buf = buf.into_inner();
/// dst.clear();
/// dst.put(&mut buf);
/// assert_eq!(dst, b" world");
/// ```
fn take(self, limit: usize) -> Take<Self>
where
Self: Sized,
{
take::new(self, limit)
}

/// Creates an adaptor which will chain this buffer with another.
///
/// The returned `Buf` instance will first consume all bytes from `self`.
/// Afterwards the output is equivalent to the output of next.
///
/// # Examples
///
/// ```
/// use bytes::Buf;
///
/// let mut chain = b"hello "[..].chain(&b"world"[..]);
///
/// let full = chain.to_bytes();
/// assert_eq!(full.bytes(), b"hello world");
/// ```
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
where
Self: Sized,
{
Chain::new(self, next)
}

/// Creates an adaptor which implements the `Read` trait for `self`.
///
/// This function returns a new value which implements `Read` by adapting
/// the `Read` trait functions to the `Buf` trait functions. Given that
/// `Buf` operations are infallible, none of the `Read` functions will
/// return with `Err`.
///
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use std::io::Read;
///
/// let buf = Bytes::from("hello world");
///
/// let mut reader = buf.reader();
/// let mut dst = [0; 1024];
///
/// let num = reader.read(&mut dst).unwrap();
///
/// assert_eq!(11, num);
/// assert_eq!(&dst[..11], &b"hello world"[..]);
/// ```
#[cfg(feature = "std")]
fn reader(self) -> Reader<Self>
where
Self: Sized,
{
reader::new(self)
}
}

macro_rules! deref_forward_buf {
Expand Down
79 changes: 79 additions & 0 deletions src/buf/buf_mut.rs
@@ -1,3 +1,5 @@
use crate::buf::{Chain, limit, Limit, writer, Writer};

use core::{
cmp,
mem::{self, MaybeUninit},
Expand Down Expand Up @@ -878,6 +880,83 @@ pub trait BufMut {
fn put_f64_le(&mut self, n: f64) {
self.put_u64_le(n.to_bits());
}

/// Creates an adaptor which can write at most `limit` bytes to `self`.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let arr = &mut [0u8; 128][..];
/// assert_eq!(arr.remaining_mut(), 128);
///
/// let dst = arr.limit(10);
/// assert_eq!(dst.remaining_mut(), 10);
/// ```
fn limit(self, limit: usize) -> Limit<Self>
where
Self: Sized,
{
limit::new(self, limit)
}

/// Creates an adaptor which implements the `Write` trait for `self`.
///
/// This function returns a new value which implements `Write` by adapting
/// the `Write` trait functions to the `BufMut` trait functions. Given that
/// `BufMut` operations are infallible, none of the `Write` functions will
/// return with `Err`.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
/// use std::io::Write;
///
/// let mut buf = vec![].writer();
///
/// let num = buf.write(&b"hello world"[..]).unwrap();
/// assert_eq!(11, num);
///
/// let buf = buf.into_inner();
///
/// assert_eq!(*buf, b"hello world"[..]);
/// ```
#[cfg(feature = "std")]
fn writer(self) -> Writer<Self>
where
Self: Sized,
{
writer::new(self)
}

/// Creates an adapter which will chain this buffer with another.
///
/// The returned `BufMut` instance will first write to all bytes from
/// `self`. Afterwards, it will write to `next`.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut a = [0u8; 5];
/// let mut b = [0u8; 6];
///
/// let mut chain = (&mut a[..]).chain_mut(&mut b[..]);
///
/// chain.put_slice(b"hello world");
///
/// assert_eq!(&a[..], b"hello");
/// assert_eq!(&b[..], b" world");
/// ```
fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>
where
Self: Sized,
{
Chain::new(self, next)
}
}

macro_rules! deref_forward_bufmut {
Expand Down
12 changes: 6 additions & 6 deletions src/buf/ext/chain.rs → src/buf/chain.rs
Expand Up @@ -20,7 +20,7 @@ use std::io::IoSlice;
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf, buf::BufExt};
/// use bytes::{Bytes, Buf};
///
/// let mut buf = (&b"hello "[..])
/// .chain(&b"world"[..]);
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::buf::BufExt;
/// use bytes::Buf;
///
/// let buf = (&b"hello"[..])
/// .chain(&b"world"[..]);
Expand All @@ -65,7 +65,7 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Buf, buf::BufExt};
/// use bytes::Buf;
///
/// let mut buf = (&b"hello"[..])
/// .chain(&b"world"[..]);
Expand All @@ -84,7 +84,7 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::buf::BufExt;
/// use bytes::Buf;
///
/// let buf = (&b"hello"[..])
/// .chain(&b"world"[..]);
Expand All @@ -100,7 +100,7 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Buf, buf::BufExt};
/// use bytes::Buf;
///
/// let mut buf = (&b"hello "[..])
/// .chain(&b"world"[..]);
Expand All @@ -119,7 +119,7 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::buf::BufExt;
/// use bytes::Buf;
///
/// let chain = (&b"hello"[..])
/// .chain(&b"world"[..]);
Expand Down