Skip to content

Commit

Permalink
Move "extra" methods to extension traits
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Oct 30, 2019
1 parent 2ac7233 commit 1bddd89
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 151 deletions.
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
30 changes: 0 additions & 30 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 @@ -901,33 +898,6 @@ pub trait BufMut {
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
File renamed without changes.
153 changes: 153 additions & 0 deletions src/buf/ext/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//! Extra utilities for `Buf` and `BufMut` types.

use super::{Buf, BufMut};

mod chain;
mod reader;
mod take;
mod writer;

use self::take::Take;
use self::chain::Chain;

#[cfg(feature = "std")]
use self::{reader::Reader, writer::Writer};

/// Extra methods for implementations of `Buf`.
pub trait BufExt: Buf {
/// 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 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 {
reader::new(self)
}
}

impl<B: Buf + ?Sized> BufExt for B {}

/// Extra methods for implementations of `BufMut`.
pub trait BufMutExt: BufMut {
/// 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)
}
}

impl<B: BufMut + ?Sized> BufMutExt for B {}
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 2 additions & 14 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,13 @@

mod buf_impl;
mod buf_mut;
mod chain;
pub mod ext;
mod iter;
mod take;
mod vec_deque;

// When std::io::Reader etc. traits are not available, skip these
#[cfg(feature = "std")]
mod reader;
#[cfg(feature = "std")]
mod writer;

pub use self::buf_impl::Buf;
pub use self::buf_mut::BufMut;
#[cfg(feature = "std")]
pub use self::buf_mut::IoSliceMut;
pub use self::chain::Chain;
pub use self::iter::IntoIter;
#[cfg(feature = "std")]
pub use self::reader::Reader;
pub use self::take::Take;
#[cfg(feature = "std")]
pub use self::writer::Writer;

0 comments on commit 1bddd89

Please sign in to comment.