add BufMutExt::limit (#309)

This type is the counterpart to BufExt::take. It limits how many bytes
some `BufMut` can actually be filled with.
This commit is contained in:
Sean McArthur
2019-11-20 15:10:30 -08:00
committed by Carl Lerche
parent b3f86d8654
commit 44fb206148
2 changed files with 57 additions and 0 deletions
+21
View File
@@ -3,12 +3,14 @@
use super::{Buf, BufMut};
mod chain;
mod limit;
#[cfg(feature = "std")]
mod reader;
mod take;
#[cfg(feature = "std")]
mod writer;
use self::limit::Limit;
use self::take::Take;
use self::chain::Chain;
@@ -98,6 +100,25 @@ impl<B: Buf + ?Sized> BufExt for B {}
/// Extra methods for implementations of `BufMut`.
pub trait BufMutExt: BufMut {
/// Creates an adaptor which can write at most `limit` bytes to `self`.
///
/// # Examples
///
/// ```
/// use bytes::{BufMut, buf::BufMutExt};
///
/// 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