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

Move "extra" methods to extension traits #306

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 0 additions & 107 deletions src/buf/buf_impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use super::{Take, Chain};

#[cfg(feature = "std")]
use super::Reader;

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

#[cfg(feature = "std")]
Expand Down Expand Up @@ -796,108 +791,6 @@ pub trait Buf {
f64::from_bits(Self::get_u64_le(self))
}

/// 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
{
super::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 a "by reference" adaptor for this instance of `Buf`.
///
/// The returned adaptor also implements `Buf` and will simply borrow `self`.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, BufMut};
///
/// let mut buf = &b"hello world"[..];
/// let mut dst = vec![];
///
/// {
/// let mut reference = buf.by_ref();
/// dst.put(&mut reference.take(5));
/// assert_eq!(dst, &b"hello"[..]);
/// } // drop our &mut reference so we can use `buf` again
///
/// dst.clear();
/// dst.put(&mut buf);
/// assert_eq!(dst, &b" world"[..]);
/// ```
fn by_ref(&mut self) -> &mut Self where Self: Sized {
self
}

/// 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::{Buf, Bytes};
/// 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 {
super::reader::new(self)
}

/// Consumes remaining bytes inside self and returns new instance of `Bytes`
///
/// # Examples
Expand Down
59 changes: 0 additions & 59 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(feature = "std")]
use super::Writer;

use core::{cmp, mem::{self, MaybeUninit}, ptr, usize};

#[cfg(feature = "std")]
Expand Down Expand Up @@ -872,62 +869,6 @@ pub trait BufMut {
fn put_f64_le(&mut self, n: f64) {
self.put_u64_le(n.to_bits());
}

/// Creates a "by reference" adaptor for this instance of `BufMut`.
///
/// The returned adapter also implements `BufMut` and will simply borrow
/// `self`.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
/// use std::io;
///
/// let mut buf = vec![];
///
/// {
/// let mut reference = buf.by_ref();
///
/// // Adapt reference to `std::io::Write`.
/// let mut writer = reference.writer();
///
/// // Use the buffer as a writer
/// io::Write::write(&mut writer, &b"hello world"[..]).unwrap();
/// } // drop our &mut reference so that we can use `buf` again
///
/// assert_eq!(buf, &b"hello world"[..]);
/// ```
fn by_ref(&mut self) -> &mut Self where Self: Sized {
self
}

/// 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 {
super::writer::new(self)
}
}

impl<T: BufMut + ?Sized> BufMut for &mut T {
Expand Down
58 changes: 22 additions & 36 deletions src/buf/chain.rs → src/buf/ext/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ use crate::buf::IoSliceMut;
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use bytes::buf::Chain;
/// use bytes::{Bytes, Buf, buf::BufExt};
///
/// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
/// let mut buf = (&b"hello "[..])
/// .chain(&b"world"[..]);
///
/// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"hello world"[..]);
Expand All @@ -41,19 +40,6 @@ pub struct Chain<T, U> {

impl<T, U> Chain<T, U> {
/// Creates a new `Chain` sequencing the provided values.
///
/// # Examples
///
/// ```
/// use bytes::BytesMut;
/// use bytes::buf::Chain;
///
/// let buf = Chain::new(
/// BytesMut::with_capacity(1024),
/// BytesMut::with_capacity(1024));
///
/// // Use the chained buffer
/// ```
pub fn new(a: T, b: U) -> Chain<T, U> {
Chain {
a,
Expand All @@ -66,10 +52,10 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use bytes::buf::BufExt;
///
/// let buf = Bytes::from(&b"hello"[..])
/// .chain(Bytes::from(&b"world"[..]));
/// let buf = (&b"hello"[..])
/// .chain(&b"world"[..]);
///
/// assert_eq!(buf.first_ref()[..], b"hello"[..]);
/// ```
Expand All @@ -82,15 +68,15 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use bytes::{Buf, buf::BufExt};
///
/// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
/// let mut buf = (&b"hello"[..])
/// .chain(&b"world"[..]);
///
/// buf.first_mut().advance(1);
///
/// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"ello world"[..]);
/// let full = buf.to_bytes();
/// assert_eq!(full, b"elloworld"[..]);
/// ```
pub fn first_mut(&mut self) -> &mut T {
&mut self.a
Expand All @@ -101,10 +87,10 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use bytes::buf::BufExt;
///
/// let buf = Bytes::from(&b"hello"[..])
/// .chain(Bytes::from(&b"world"[..]));
/// let buf = (&b"hello"[..])
/// .chain(&b"world"[..]);
///
/// assert_eq!(buf.last_ref()[..], b"world"[..]);
/// ```
Expand All @@ -117,15 +103,15 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use bytes::{Buf, buf::BufExt};
///
/// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
/// let mut buf = (&b"hello "[..])
/// .chain(&b"world"[..]);
///
/// buf.last_mut().advance(1);
///
/// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"hello orld"[..]);
/// let full = buf.to_bytes();
/// assert_eq!(full, b"hello orld"[..]);
/// ```
pub fn last_mut(&mut self) -> &mut U {
&mut self.b
Expand All @@ -136,10 +122,10 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf};
/// use bytes::buf::BufExt;
///
/// let chain = Bytes::from(&b"hello"[..])
/// .chain(Bytes::from(&b"world"[..]));
/// let chain = (&b"hello"[..])
/// .chain(&b"world"[..]);
///
/// let (first, last) = chain.into_inner();
/// assert_eq!(first[..], b"hello"[..]);
Expand Down
Loading