Struct Take

pub struct Take<T> { /* private fields */ }

A Buf adapter which limits the bytes read from an underlying buffer.

This struct is generally created by calling take() on Buf. See documentation of take() for more details.

Implementations

impl<T> Take<T>

fn into_inner(self) -> T

Consumes this Take, returning the underlying value.

Examples

use bytes::{Buf, BufMut};

let mut buf = b"hello world".take(2);
let mut dst = vec![];

dst.put(&mut buf);
assert_eq!(*dst, b"he"[..]);

let mut buf = buf.into_inner();

dst.clear();
dst.put(&mut buf);
assert_eq!(*dst, b"llo world"[..]);
fn get_ref(&self) -> &T

Gets a reference to the underlying Buf.

It is inadvisable to directly read from the underlying Buf.

Examples

use bytes::Buf;

let buf = b"hello world".take(2);

assert_eq!(11, buf.get_ref().remaining());
fn get_mut(&mut self) -> &mut T

Gets a mutable reference to the underlying Buf.

It is inadvisable to directly read from the underlying Buf.

Examples

use bytes::{Buf, BufMut};

let mut buf = b"hello world".take(2);
let mut dst = vec![];

buf.get_mut().advance(2);

dst.put(&mut buf);
assert_eq!(*dst, b"ll"[..]);
fn limit(&self) -> usize

Returns the maximum number of bytes that can be read.

Note

If the inner Buf has fewer bytes than indicated by this method then that is the actual number of available bytes.

Examples

use bytes::Buf;

let mut buf = b"hello world".take(2);

assert_eq!(2, buf.limit());
assert_eq!(b'h', buf.get_u8());
assert_eq!(1, buf.limit());
fn set_limit(&mut self, lim: usize)

Sets the maximum number of bytes that can be read.

Note

If the inner Buf has fewer bytes than lim then that is the actual number of available bytes.

Examples

use bytes::{Buf, BufMut};

let mut buf = b"hello world".take(2);
let mut dst = vec![];

dst.put(&mut buf);
assert_eq!(*dst, b"he"[..]);

dst.clear();

buf.set_limit(3);
dst.put(&mut buf);
assert_eq!(*dst, b"llo"[..]);

Trait Implementations

impl<T: Buf> Buf for Take<T>

fn remaining(&self) -> usize
fn chunk(&self) -> &[u8]
fn advance(&mut self, cnt: usize)
fn copy_to_bytes(&mut self, len: usize) -> Bytes
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize

impl<T: Debug> Debug for Take<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<T> Freeze for Take<T> where T: Freeze,

impl<T> RefUnwindSafe for Take<T> where T: RefUnwindSafe,

impl<T> Send for Take<T> where T: Send,

impl<T> Sync for Take<T> where T: Sync,

impl<T> Unpin for Take<T> where T: Unpin,

impl<T> UnsafeUnpin for Take<T> where T: UnsafeUnpin,

impl<T> UnwindSafe for Take<T> where T: UnwindSafe,

Blanket Implementations

impl<T> Any for Take<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Take<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Take<T> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for Take<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for Take<T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Take<T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Take<T> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>