Trait BufMut
unsafe trait BufMut
A trait for values that provide sequential write access to bytes.
Write bytes to a buffer
A buffer stores bytes in memory such that write operations are infallible.
The underlying storage may or may not be in contiguous memory. A BufMut
value is a cursor into the buffer. Writing to BufMut advances the cursor
position.
The simplest BufMut is a Vec<u8>.
use BufMut;
let mut buf = vec!;
buf.put;
assert_eq!;
Required Methods
fn remaining_mut(self: &Self) -> usizeReturns the number of bytes that can be written from the current position until the end of the buffer is reached.
This value is greater than or equal to the length of the slice returned by
chunk_mut().Writing to a
BufMutmay involve allocating more memory on the fly. Implementations may fail before reaching the number of bytes indicated by this method if they encounter an allocation failure.Examples
use BufMut; let mut dst = ; let mut buf = &mut dst; let original_remaining = buf.remaining_mut; buf.put; assert_eq!;Implementer notes
Implementations of
remaining_mutshould ensure that the return value does not change unless a call is made toadvance_mutor any other function that is documented to change theBufMut's current position.Note
remaining_mutmay return value smaller than actual available space.unsafe fn advance_mut(self: &mut Self, cnt: usize)Advance the internal cursor of the BufMut
The next call to
chunk_mutwill return a slice startingcntbytes further into the underlying buffer.Safety
The caller must ensure that the next
cntbytes ofchunkare initialized.Examples
use BufMut; let mut buf = Vecwith_capacity; // Write some data buf.chunk_mut.copy_from_slice; unsafe ; // write more bytes buf.chunk_mut.copy_from_slice; unsafe assert_eq!; assert_eq!;Panics
This function may panic if
cnt > self.remaining_mut().Implementer notes
It is recommended for implementations of
advance_mutto panic ifcnt > self.remaining_mut(). If the implementation does not panic, the call must behave as ifcnt == self.remaining_mut().A call with
cnt == 0should never panic and be a no-op.fn chunk_mut(self: &mut Self) -> &mut UninitSliceReturns a mutable slice starting at the current BufMut position and of length between 0 and
BufMut::remaining_mut(). Note that this can be shorter than the whole remainder of the buffer (this allows non-continuous implementation).This is a lower level function. Most operations are done with other functions.
The returned byte slice may represent uninitialized memory.
Examples
use BufMut; let mut buf = Vecwith_capacity; unsafe assert_eq!; assert_eq!;Implementer notes
This function should never panic.
chunk_mut()should return an empty slice if and only ifremaining_mut()returns 0. In other words,chunk_mut()returning an empty slice implies thatremaining_mut()will return 0 andremaining_mut()returning 0 implies thatchunk_mut()will return an empty slice.This function may trigger an out-of-memory abort if it tries to allocate memory and fails to do so.
Provided Methods
fn has_remaining_mut(self: &Self) -> boolReturns true if there is space in
selffor more bytes.This is equivalent to
self.remaining_mut() != 0.Examples
use BufMut; let mut dst = ; let mut buf = &mut dst; assert!; buf.put; assert!;fn put<T: super::Buf>(self: &mut Self, src: T) where Self: SizedTransfer bytes into
selffromsrcand advance the cursor by the number of bytes written.Examples
use BufMut; let mut buf = vec!; buf.put_u8; buf.put; buf.put; assert_eq!;Panics
Panics if
selfdoes not have enough capacity to containsrc.fn put_slice(self: &mut Self, src: &[u8])Transfer bytes into
selffromsrcand advance the cursor by the number of bytes written.selfmust have enough remaining capacity to contain all ofsrc.use BufMut; let mut dst = ; assert_eq!;fn put_bytes(self: &mut Self, val: u8, cnt: usize)Put
cntbytesvalintoself.Logically equivalent to calling
self.put_u8(val)cnttimes, but may work faster.selfmust have at leastcntremaining capacity.use BufMut; let mut dst = ; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u8(self: &mut Self, n: u8)Writes an unsigned 8 bit integer to
self.The current position is advanced by 1.
Examples
use BufMut; let mut buf = vec!; buf.put_u8; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i8(self: &mut Self, n: i8)Writes a signed 8 bit integer to
self.The current position is advanced by 1.
Examples
use BufMut; let mut buf = vec!; buf.put_i8; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u16(self: &mut Self, n: u16)Writes an unsigned 16 bit integer to
selfin big-endian byte order.The current position is advanced by 2.
Examples
use BufMut; let mut buf = vec!; buf.put_u16; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u16_le(self: &mut Self, n: u16)Writes an unsigned 16 bit integer to
selfin little-endian byte order.The current position is advanced by 2.
Examples
use BufMut; let mut buf = vec!; buf.put_u16_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u16_ne(self: &mut Self, n: u16)Writes an unsigned 16 bit integer to
selfin native-endian byte order.The current position is advanced by 2.
Examples
use BufMut; let mut buf = vec!; buf.put_u16_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_i16(self: &mut Self, n: i16)Writes a signed 16 bit integer to
selfin big-endian byte order.The current position is advanced by 2.
Examples
use BufMut; let mut buf = vec!; buf.put_i16; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i16_le(self: &mut Self, n: i16)Writes a signed 16 bit integer to
selfin little-endian byte order.The current position is advanced by 2.
Examples
use BufMut; let mut buf = vec!; buf.put_i16_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i16_ne(self: &mut Self, n: i16)Writes a signed 16 bit integer to
selfin native-endian byte order.The current position is advanced by 2.
Examples
use BufMut; let mut buf = vec!; buf.put_i16_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_u32(self: &mut Self, n: u32)Writes an unsigned 32 bit integer to
selfin big-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_u32; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u32_le(self: &mut Self, n: u32)Writes an unsigned 32 bit integer to
selfin little-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_u32_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u32_ne(self: &mut Self, n: u32)Writes an unsigned 32 bit integer to
selfin native-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_u32_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_i32(self: &mut Self, n: i32)Writes a signed 32 bit integer to
selfin big-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_i32; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i32_le(self: &mut Self, n: i32)Writes a signed 32 bit integer to
selfin little-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_i32_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i32_ne(self: &mut Self, n: i32)Writes a signed 32 bit integer to
selfin native-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_i32_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_u64(self: &mut Self, n: u64)Writes an unsigned 64 bit integer to
selfin the big-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_u64; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u64_le(self: &mut Self, n: u64)Writes an unsigned 64 bit integer to
selfin little-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_u64_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u64_ne(self: &mut Self, n: u64)Writes an unsigned 64 bit integer to
selfin native-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_u64_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_i64(self: &mut Self, n: i64)Writes a signed 64 bit integer to
selfin the big-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_i64; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i64_le(self: &mut Self, n: i64)Writes a signed 64 bit integer to
selfin little-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_i64_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i64_ne(self: &mut Self, n: i64)Writes a signed 64 bit integer to
selfin native-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_i64_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_u128(self: &mut Self, n: u128)Writes an unsigned 128 bit integer to
selfin the big-endian byte order.The current position is advanced by 16.
Examples
use BufMut; let mut buf = vec!; buf.put_u128; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u128_le(self: &mut Self, n: u128)Writes an unsigned 128 bit integer to
selfin little-endian byte order.The current position is advanced by 16.
Examples
use BufMut; let mut buf = vec!; buf.put_u128_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_u128_ne(self: &mut Self, n: u128)Writes an unsigned 128 bit integer to
selfin native-endian byte order.The current position is advanced by 16.
Examples
use BufMut; let mut buf = vec!; buf.put_u128_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_i128(self: &mut Self, n: i128)Writes a signed 128 bit integer to
selfin the big-endian byte order.The current position is advanced by 16.
Examples
use BufMut; let mut buf = vec!; buf.put_i128; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i128_le(self: &mut Self, n: i128)Writes a signed 128 bit integer to
selfin little-endian byte order.The current position is advanced by 16.
Examples
use BufMut; let mut buf = vec!; buf.put_i128_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_i128_ne(self: &mut Self, n: i128)Writes a signed 128 bit integer to
selfin native-endian byte order.The current position is advanced by 16.
Examples
use BufMut; let mut buf = vec!; buf.put_i128_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_uint(self: &mut Self, n: u64, nbytes: usize)Writes an unsigned n-byte integer to
selfin big-endian byte order.The current position is advanced by
nbytes.Examples
use BufMut; let mut buf = vec!; buf.put_uint; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
selfor ifnbytesis greater than 8.fn put_uint_le(self: &mut Self, n: u64, nbytes: usize)Writes an unsigned n-byte integer to
selfin the little-endian byte order.The current position is advanced by
nbytes.Examples
use BufMut; let mut buf = vec!; buf.put_uint_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
selfor ifnbytesis greater than 8.fn put_uint_ne(self: &mut Self, n: u64, nbytes: usize)Writes an unsigned n-byte integer to
selfin the native-endian byte order.The current position is advanced by
nbytes.Examples
use BufMut; let mut buf = vec!; buf.put_uint_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
selfor ifnbytesis greater than 8.fn put_int(self: &mut Self, n: i64, nbytes: usize)Writes low
nbytesof a signed integer toselfin big-endian byte order.The current position is advanced by
nbytes.Examples
use BufMut; let mut buf = vec!; buf.put_int; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
selfor ifnbytesis greater than 8.fn put_int_le(self: &mut Self, n: i64, nbytes: usize)Writes low
nbytesof a signed integer toselfin little-endian byte order.The current position is advanced by
nbytes.Examples
use BufMut; let mut buf = vec!; buf.put_int_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
selfor ifnbytesis greater than 8.fn put_int_ne(self: &mut Self, n: i64, nbytes: usize)Writes low
nbytesof a signed integer toselfin native-endian byte order.The current position is advanced by
nbytes.Examples
use BufMut; let mut buf = vec!; buf.put_int_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
selfor ifnbytesis greater than 8.fn put_f32(self: &mut Self, n: f32)Writes an IEEE754 single-precision (4 bytes) floating point number to
selfin big-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_f32; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_f32_le(self: &mut Self, n: f32)Writes an IEEE754 single-precision (4 bytes) floating point number to
selfin little-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_f32_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_f32_ne(self: &mut Self, n: f32)Writes an IEEE754 single-precision (4 bytes) floating point number to
selfin native-endian byte order.The current position is advanced by 4.
Examples
use BufMut; let mut buf = vec!; buf.put_f32_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn put_f64(self: &mut Self, n: f64)Writes an IEEE754 double-precision (8 bytes) floating point number to
selfin big-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_f64; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_f64_le(self: &mut Self, n: f64)Writes an IEEE754 double-precision (8 bytes) floating point number to
selfin little-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_f64_le; assert_eq!;Panics
This function panics if there is not enough remaining capacity in
self.fn put_f64_ne(self: &mut Self, n: f64)Writes an IEEE754 double-precision (8 bytes) floating point number to
selfin native-endian byte order.The current position is advanced by 8.
Examples
use BufMut; let mut buf = vec!; buf.put_f64_ne; if cfg! elsePanics
This function panics if there is not enough remaining capacity in
self.fn limit(self: Self, limit: usize) -> Limit<Self> where Self: SizedCreates an adaptor which can write at most
limitbytes toself.Examples
use BufMut; let arr = &mut ; assert_eq!; let dst = arr.limit; assert_eq!;fn writer(self: Self) -> Writer<Self> where Self: SizedCreates an adaptor which implements the
Writetrait forself.This function returns a new value which implements
Writeby adapting theWritetrait functions to theBufMuttrait functions. Given thatBufMutoperations are infallible, none of theWritefunctions will return withErr.Examples
use BufMut; use Write; let mut buf = vec!.writer; let num = buf.write.unwrap; assert_eq!; let buf = buf.into_inner; assert_eq!;fn chain_mut<U: BufMut>(self: Self, next: U) -> Chain<Self, U> where Self: SizedCreates an adapter which will chain this buffer with another.
The returned
BufMutinstance will first write to all bytes fromself. Afterwards, it will write tonext.Examples
use BufMut; let mut a = ; let mut b = ; let mut chain = .chain_mut; chain.put_slice; assert_eq!; assert_eq!;
Implementors
impl<T: BufMut> BufMut for Limit<T>impl BufMut for alloc::vec::Vec<u8>impl<T, U> BufMut for Chain<T, U>impl BufMut for &mut [u8]impl<T: BufMut + ?Sized> BufMut for &mut Timpl<T: BufMut + ?Sized> BufMut for alloc::boxed::Box<T>impl BufMut for &mut [core::mem::MaybeUninit<u8>]impl BufMut for BytesMut