Trait Buf
trait Buf
Read bytes from a buffer.
A buffer stores bytes in memory such that read operations are infallible.
The underlying storage may or may not be in contiguous memory. A Buf value
is a cursor into the buffer. Reading from Buf advances the cursor
position. It can be thought of as an efficient Iterator for collections of
bytes.
The simplest Buf is a &[u8].
use Buf;
let mut buf = &b"hello world";
assert_eq!;
assert_eq!;
assert_eq!;
let mut rest = ;
buf.copy_to_slice;
assert_eq!;
Required Methods
fn remaining(self: &Self) -> usizeReturns the number of bytes between the current position and the end of the buffer.
This value is greater than or equal to the length of the slice returned by
chunk().Examples
use Buf; let mut buf = &b"hello world"; assert_eq!; buf.get_u8; assert_eq!;Implementer notes
Implementations of
remainingshould ensure that the return value does not change unless a call is made toadvanceor any other function that is documented to change theBuf's current position.fn chunk(self: &Self) -> &[u8]Returns a slice starting at the current position and of length between 0 and
Buf::remaining(). Note that this can return a shorter slice (this allows non-continuous internal representation).This is a lower level function. Most operations are done with other functions.
Examples
use Buf; let mut buf = &b"hello world"; assert_eq!; buf.advance; assert_eq!;Implementer notes
This function should never panic.
chunk()should return an empty slice if and only ifremaining()returns 0. In other words,chunk()returning an empty slice implies thatremaining()will return 0 andremaining()returning 0 implies thatchunk()will return an empty slice.fn advance(self: &mut Self, cnt: usize)Advance the internal cursor of the Buf
The next call to
chunk()will return a slice startingcntbytes further into the underlying buffer.Examples
use Buf; let mut buf = &b"hello world"; assert_eq!; buf.advance; assert_eq!;Panics
This function may panic if
cnt > self.remaining().Implementer notes
It is recommended for implementations of
advanceto panic ifcnt > self.remaining(). If the implementation does not panic, the call must behave as ifcnt == self.remaining().A call with
cnt == 0should never panic and be a no-op.
Provided Methods
fn chunks_vectored<'a>(self: &'a Self, dst: &mut [IoSlice<'a>]) -> usizeFills
dstwith potentially multiple slices starting atself's current position.If the
Bufis backed by disjoint slices of bytes,chunk_vectoredenables fetching more than one slice at once.dstis a slice ofIoSlicereferences, enabling the slice to be directly used withwritevwithout any further conversion. The sum of the lengths of all the buffers written todstwill be less than or equal toBuf::remaining().The entries in
dstwill be overwritten, but the data contained by the slices will not be modified. The return value is the number of slices written todst. IfBuf::remaining()is non-zero, then this writes at least one non-empty slice todst.This is a lower level function. Most operations are done with other functions.
Implementer notes
This function should never panic. Once the end of the buffer is reached, i.e.,
Buf::remainingreturns 0, calls tochunk_vectoredmust return 0 without mutatingdst.Implementations should also take care to properly handle being called with
dstbeing a zero length slice.fn has_remaining(self: &Self) -> boolReturns true if there are any more bytes to consume
This is equivalent to
self.remaining() != 0.Examples
use Buf; let mut buf = &b"a"; assert!; buf.get_u8; assert!;fn copy_to_slice(self: &mut Self, dst: &mut [u8])Copies bytes from
selfintodst.The cursor is advanced by the number of bytes copied.
selfmust have enough remaining bytes to filldst.Examples
use Buf; let mut buf = &b"hello world"; let mut dst = ; buf.copy_to_slice; assert_eq!; assert_eq!;Panics
This function panics if
self.remaining() < dst.len().fn get_u8(self: &mut Self) -> u8Gets an unsigned 8 bit integer from
self.The current position is advanced by 1.
Examples
use Buf; let mut buf = &b"\x08 hello"; assert_eq!;Panics
This function panics if there is no more remaining data in
self.fn get_i8(self: &mut Self) -> i8Gets a signed 8 bit integer from
self.The current position is advanced by 1.
Examples
use Buf; let mut buf = &b"\x08 hello"; assert_eq!;Panics
This function panics if there is no more remaining data in
self.fn get_u16(self: &mut Self) -> u16Gets an unsigned 16 bit integer from
selfin big-endian byte order.The current position is advanced by 2.
Examples
use Buf; let mut buf = &b"\x08\x09 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u16_le(self: &mut Self) -> u16Gets an unsigned 16 bit integer from
selfin little-endian byte order.The current position is advanced by 2.
Examples
use Buf; let mut buf = &b"\x09\x08 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u16_ne(self: &mut Self) -> u16Gets an unsigned 16 bit integer from
selfin native-endian byte order.The current position is advanced by 2.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i16(self: &mut Self) -> i16Gets a signed 16 bit integer from
selfin big-endian byte order.The current position is advanced by 2.
Examples
use Buf; let mut buf = &b"\x08\x09 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i16_le(self: &mut Self) -> i16Gets a signed 16 bit integer from
selfin little-endian byte order.The current position is advanced by 2.
Examples
use Buf; let mut buf = &b"\x09\x08 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i16_ne(self: &mut Self) -> i16Gets a signed 16 bit integer from
selfin native-endian byte order.The current position is advanced by 2.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u32(self: &mut Self) -> u32Gets an unsigned 32 bit integer from
selfin the big-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf = &b"\x08\x09\xA0\xA1 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u32_le(self: &mut Self) -> u32Gets an unsigned 32 bit integer from
selfin the little-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf = &b"\xA1\xA0\x09\x08 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u32_ne(self: &mut Self) -> u32Gets an unsigned 32 bit integer from
selfin native-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i32(self: &mut Self) -> i32Gets a signed 32 bit integer from
selfin big-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf = &b"\x08\x09\xA0\xA1 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i32_le(self: &mut Self) -> i32Gets a signed 32 bit integer from
selfin little-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf = &b"\xA1\xA0\x09\x08 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i32_ne(self: &mut Self) -> i32Gets a signed 32 bit integer from
selfin native-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u64(self: &mut Self) -> u64Gets an unsigned 64 bit integer from
selfin big-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u64_le(self: &mut Self) -> u64Gets an unsigned 64 bit integer from
selfin little-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u64_ne(self: &mut Self) -> u64Gets an unsigned 64 bit integer from
selfin native-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i64(self: &mut Self) -> i64Gets a signed 64 bit integer from
selfin big-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i64_le(self: &mut Self) -> i64Gets a signed 64 bit integer from
selfin little-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i64_ne(self: &mut Self) -> i64Gets a signed 64 bit integer from
selfin native-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u128(self: &mut Self) -> u128Gets an unsigned 128 bit integer from
selfin big-endian byte order.The current position is advanced by 16.
Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u128_le(self: &mut Self) -> u128Gets an unsigned 128 bit integer from
selfin little-endian byte order.The current position is advanced by 16.
Examples
use Buf; let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_u128_ne(self: &mut Self) -> u128Gets an unsigned 128 bit integer from
selfin native-endian byte order.The current position is advanced by 16.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i128(self: &mut Self) -> i128Gets a signed 128 bit integer from
selfin big-endian byte order.The current position is advanced by 16.
Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i128_le(self: &mut Self) -> i128Gets a signed 128 bit integer from
selfin little-endian byte order.The current position is advanced by 16.
Examples
use Buf; let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_i128_ne(self: &mut Self) -> i128Gets a signed 128 bit integer from
selfin native-endian byte order.The current position is advanced by 16.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_uint(self: &mut Self, nbytes: usize) -> u64Gets an unsigned n-byte integer from
selfin big-endian byte order.The current position is advanced by
nbytes.Examples
use Buf; let mut buf = &b"\x01\x02\x03 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self, or ifnbytesis greater than 8.fn get_uint_le(self: &mut Self, nbytes: usize) -> u64Gets an unsigned n-byte integer from
selfin little-endian byte order.The current position is advanced by
nbytes.Examples
use Buf; let mut buf = &b"\x03\x02\x01 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self, or ifnbytesis greater than 8.fn get_uint_ne(self: &mut Self, nbytes: usize) -> u64Gets an unsigned n-byte integer from
selfin native-endian byte order.The current position is advanced by
nbytes.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self, or ifnbytesis greater than 8.fn get_int(self: &mut Self, nbytes: usize) -> i64Gets a signed n-byte integer from
selfin big-endian byte order.The current position is advanced by
nbytes.Examples
use Buf; let mut buf = &b"\x01\x02\x03 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self, or ifnbytesis greater than 8.fn get_int_le(self: &mut Self, nbytes: usize) -> i64Gets a signed n-byte integer from
selfin little-endian byte order.The current position is advanced by
nbytes.Examples
use Buf; let mut buf = &b"\x03\x02\x01 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self, or ifnbytesis greater than 8.fn get_int_ne(self: &mut Self, nbytes: usize) -> i64Gets a signed n-byte integer from
selfin native-endian byte order.The current position is advanced by
nbytes.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self, or ifnbytesis greater than 8.fn get_f32(self: &mut Self) -> f32Gets an IEEE754 single-precision (4 bytes) floating point number from
selfin big-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf = &b"\x3F\x99\x99\x9A hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_f32_le(self: &mut Self) -> f32Gets an IEEE754 single-precision (4 bytes) floating point number from
selfin little-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf = &b"\x9A\x99\x99\x3F hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_f32_ne(self: &mut Self) -> f32Gets an IEEE754 single-precision (4 bytes) floating point number from
selfin native-endian byte order.The current position is advanced by 4.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_f64(self: &mut Self) -> f64Gets an IEEE754 double-precision (8 bytes) floating point number from
selfin big-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_f64_le(self: &mut Self) -> f64Gets an IEEE754 double-precision (8 bytes) floating point number from
selfin little-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn get_f64_ne(self: &mut Self) -> f64Gets an IEEE754 double-precision (8 bytes) floating point number from
selfin native-endian byte order.The current position is advanced by 8.
Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!;Panics
This function panics if there is not enough remaining data in
self.fn try_copy_to_slice(self: &mut Self, dst: &mut [u8]) -> Result<(), TryGetError>Copies bytes from
selfintodst.The cursor is advanced by the number of bytes copied.
selfmust have enough remaining bytes to filldst.Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"hello world"; let mut dst = ; assert_eq!; assert_eq!; assert_eq!;use ; let mut buf = &b"hello world"; let mut dst = ; assert_eq!; assert_eq!;fn try_get_u8(self: &mut Self) -> Result<u8, TryGetError>Gets an unsigned 8 bit integer from
self.The current position is advanced by 1.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b""; assert_eq!;fn try_get_i8(self: &mut Self) -> Result<i8, TryGetError>Gets a signed 8 bit integer from
self.The current position is advanced by 1.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b""; assert_eq!;fn try_get_u16(self: &mut Self) -> Result<u16, TryGetError>Gets an unsigned 16 bit integer from
selfin big-endian byte order.The current position is advanced by 2.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08\x09 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08"; assert_eq!; assert_eq!;fn try_get_u16_le(self: &mut Self) -> Result<u16, TryGetError>Gets an unsigned 16 bit integer from
selfin little-endian byte order.The current position is advanced by 2.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x09\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08"; assert_eq!; assert_eq!;fn try_get_u16_ne(self: &mut Self) -> Result<u16, TryGetError>Gets an unsigned 16 bit integer from
selfin native-endian byte order.The current position is advanced by 2.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08"; assert_eq!; assert_eq!;fn try_get_i16(self: &mut Self) -> Result<i16, TryGetError>Gets a signed 16 bit integer from
selfin big-endian byte order.The current position is advanced by 2.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08\x09 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08"; assert_eq!; assert_eq!;fn try_get_i16_le(self: &mut Self) -> Result<i16, TryGetError>Gets an signed 16 bit integer from
selfin little-endian byte order.The current position is advanced by 2.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x09\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08"; assert_eq!; assert_eq!;fn try_get_i16_ne(self: &mut Self) -> Result<i16, TryGetError>Gets a signed 16 bit integer from
selfin native-endian byte order.The current position is advanced by 2.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08"; assert_eq!; assert_eq!;fn try_get_u32(self: &mut Self) -> Result<u32, TryGetError>Gets an unsigned 32 bit integer from
selfin big-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08\x09\xA0\xA1 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03"; assert_eq!; assert_eq!;fn try_get_u32_le(self: &mut Self) -> Result<u32, TryGetError>Gets an unsigned 32 bit integer from
selfin little-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\xA1\xA0\x09\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08\x09\xA0"; assert_eq!; assert_eq!;fn try_get_u32_ne(self: &mut Self) -> Result<u32, TryGetError>Gets an unsigned 32 bit integer from
selfin native-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08\x09\xA0"; assert_eq!; assert_eq!;fn try_get_i32(self: &mut Self) -> Result<i32, TryGetError>Gets a signed 32 bit integer from
selfin big-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08\x09\xA0\xA1 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03"; assert_eq!; assert_eq!;fn try_get_i32_le(self: &mut Self) -> Result<i32, TryGetError>Gets a signed 32 bit integer from
selfin little-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\xA1\xA0\x09\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08\x09\xA0"; assert_eq!; assert_eq!;fn try_get_i32_ne(self: &mut Self) -> Result<i32, TryGetError>Gets a signed 32 bit integer from
selfin native-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08\x09\xA0"; assert_eq!; assert_eq!;fn try_get_u64(self: &mut Self) -> Result<u64, TryGetError>Gets an unsigned 64 bit integer from
selfin big-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"; assert_eq!; assert_eq!;fn try_get_u64_le(self: &mut Self) -> Result<u64, TryGetError>Gets an unsigned 64 bit integer from
selfin little-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"; assert_eq!; assert_eq!;fn try_get_u64_ne(self: &mut Self) -> Result<u64, TryGetError>Gets an unsigned 64 bit integer from
selfin native-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"; assert_eq!; assert_eq!;fn try_get_i64(self: &mut Self) -> Result<i64, TryGetError>Gets a signed 64 bit integer from
selfin big-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"; assert_eq!; assert_eq!;fn try_get_i64_le(self: &mut Self) -> Result<i64, TryGetError>Gets a signed 64 bit integer from
selfin little-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"; assert_eq!; assert_eq!;fn try_get_i64_ne(self: &mut Self) -> Result<i64, TryGetError>Gets a signed 64 bit integer from
selfin native-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"; assert_eq!; assert_eq!;fn try_get_u128(self: &mut Self) -> Result<u128, TryGetError>Gets an unsigned 128 bit integer from
selfin big-endian byte order.The current position is advanced by 16.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"; assert_eq!; assert_eq!;fn try_get_u128_le(self: &mut Self) -> Result<u128, TryGetError>Gets an unsigned 128 bit integer from
selfin little-endian byte order.The current position is advanced by 16.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"; assert_eq!; assert_eq!;fn try_get_u128_ne(self: &mut Self) -> Result<u128, TryGetError>Gets an unsigned 128 bit integer from
selfin native-endian byte order.The current position is advanced by 16.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"; assert_eq!; assert_eq!;fn try_get_i128(self: &mut Self) -> Result<i128, TryGetError>Gets a signed 128 bit integer from
selfin big-endian byte order.The current position is advanced by 16.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"; assert_eq!; assert_eq!;fn try_get_i128_le(self: &mut Self) -> Result<i128, TryGetError>Gets a signed 128 bit integer from
selfin little-endian byte order.The current position is advanced by 16.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"; assert_eq!; assert_eq!;fn try_get_i128_ne(self: &mut Self) -> Result<i128, TryGetError>Gets a signed 128 bit integer from
selfin native-endian byte order.The current position is advanced by 16.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"; assert_eq!; assert_eq!;fn try_get_uint(self: &mut Self, nbytes: usize) -> Result<u64, TryGetError>Gets an unsigned n-byte integer from
selfin big-endian byte order.The current position is advanced by
nbytes.Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x01\x02\x03 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03"; assert_eq!; assert_eq!;Panics
This function panics if
nbytes> 8.fn try_get_uint_le(self: &mut Self, nbytes: usize) -> Result<u64, TryGetError>Gets an unsigned n-byte integer from
selfin little-endian byte order.The current position is advanced by
nbytes.Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x03\x02\x01 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03"; assert_eq!; assert_eq!;Panics
This function panics if
nbytes> 8.fn try_get_uint_ne(self: &mut Self, nbytes: usize) -> Result<u64, TryGetError>Gets an unsigned n-byte integer from
selfin native-endian byte order.The current position is advanced by
nbytes.Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;Panics
This function panics if
nbytesis greater than 8.fn try_get_int(self: &mut Self, nbytes: usize) -> Result<i64, TryGetError>Gets a signed n-byte integer from
selfin big-endian byte order.The current position is advanced by
nbytes.Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x01\x02\x03 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03"; assert_eq!; assert_eq!;Panics
This function panics if
nbytesis greater than 8.fn try_get_int_le(self: &mut Self, nbytes: usize) -> Result<i64, TryGetError>Gets a signed n-byte integer from
selfin little-endian byte order.The current position is advanced by
nbytes.Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x03\x02\x01 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x01\x02\x03"; assert_eq!; assert_eq!;Panics
This function panics if
nbytesis greater than 8.fn try_get_int_ne(self: &mut Self, nbytes: usize) -> Result<i64, TryGetError>Gets a signed n-byte integer from
selfin native-endian byte order.The current position is advanced by
nbytes.Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;Panics
This function panics if
nbytesis greater than 8.fn try_get_f32(self: &mut Self) -> Result<f32, TryGetError>Gets an IEEE754 single-precision (4 bytes) floating point number from
selfin big-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x3F\x99\x99\x9A hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x3F\x99\x99"; assert_eq!; assert_eq!;fn try_get_f32_le(self: &mut Self) -> Result<f32, TryGetError>Gets an IEEE754 single-precision (4 bytes) floating point number from
selfin little-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x9A\x99\x99\x3F hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x3F\x99\x99"; assert_eq!; assert_eq!;fn try_get_f32_ne(self: &mut Self) -> Result<f32, TryGetError>Gets an IEEE754 single-precision (4 bytes) floating point number from
selfin native-endian byte order.The current position is advanced by 4.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x3F\x99\x99"; assert_eq!; assert_eq!;fn try_get_f64(self: &mut Self) -> Result<f64, TryGetError>Gets an IEEE754 double-precision (8 bytes) floating point number from
selfin big-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"; assert_eq!; assert_eq!;fn try_get_f64_le(self: &mut Self) -> Result<f64, TryGetError>Gets an IEEE754 double-precision (8 bytes) floating point number from
selfin little-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"; assert_eq!; assert_eq!;use ; let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"; assert_eq!; assert_eq!;fn try_get_f64_ne(self: &mut Self) -> Result<f64, TryGetError>Gets an IEEE754 double-precision (8 bytes) floating point number from
selfin native-endian byte order.The current position is advanced by 8.
Returns
Err(TryGetError)when there are not enough remaining bytes to read the value.Examples
use Buf; let mut buf: & = match cfg! ; assert_eq!; assert_eq!;use ; let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"; assert_eq!; assert_eq!;fn copy_to_bytes(self: &mut Self, len: usize) -> crate::BytesConsumes
lenbytes inside self and returns new instance ofByteswith this data.This function may be optimized by the underlying type to avoid actual copies. For example,
Bytesimplementation will do a shallow copy (ref-count increment).Examples
use Buf; let bytes = .copy_to_bytes; assert_eq!;Panics
This function panics if
len > self.remaining().fn take(self: Self, limit: usize) -> Take<Self> where Self: SizedCreates an adaptor which will read at most
limitbytes fromself.This function returns a new instance of
Bufwhich will read at mostlimitbytes.Examples
use ; let mut buf = b"hello world".take; let mut dst = vec!; dst.put; assert_eq!; let mut buf = buf.into_inner; dst.clear; dst.put; assert_eq!;fn chain<U: Buf>(self: Self, next: U) -> Chain<Self, U> where Self: SizedCreates an adaptor which will chain this buffer with another.
The returned
Bufinstance will first consume all bytes fromself. Afterwards the output is equivalent to the output of next.Examples
use Buf; let mut chain = b"hello ".chain; let full = chain.copy_to_bytes; assert_eq!;fn reader(self: Self) -> Reader<Self> where Self: SizedCreates an adaptor which implements the
Readtrait forself.This function returns a new value which implements
Readby adapting theReadtrait functions to theBuftrait functions. Given thatBufoperations are infallible, none of theReadfunctions will return withErr.Examples
use ; use Read; let buf = from; let mut reader = buf.reader; let mut dst = ; let num = reader.read.unwrap; assert_eq!; assert_eq!;
Implementors
impl<T: Buf + ?Sized> Buf for &mut Timpl Buf for &[u8]impl<T, U> Buf for Chain<T, U>impl Buf for Bytesimpl Buf for BytesMutimpl<T: Buf + ?Sized> Buf for alloc::boxed::Box<T>impl<T: Buf> Buf for Take<T>impl Buf for alloc::collections::VecDeque<u8>impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T>