pub trait EncodeExt: Encode + Sized {
// Provided methods
fn then<E>(self, e: E) -> impl Encode
where E: Encode { ... }
fn u8(self, u: u8) -> impl Encode { ... }
fn u16(self, u: u16) -> impl Encode { ... }
fn u32(self, u: u32) -> impl Encode { ... }
fn u64(self, u: u64) -> impl Encode { ... }
fn u128(self, u: u128) -> impl Encode { ... }
fn usize(self, u: usize) -> impl Encode { ... }
fn encode<B>(self, buf: &mut B) -> Result<&mut [u8], usize>
where B: Buffer + ?Sized { ... }
}Expand description
An extension trait for encodable types.
This trait allows for appending an encoding sequence by encodable values.
Integers are encoded in big-endian byte order by default.
To encode integers in little-endian order use LittleEndianEncodeExt.
§Examples
use co::EncodeExt;
let mut code = [0; 5];
// Encode a sequence of bytes to the `code` buffer
b'h'
.then(b"el") // `then` accepts any encodable
.u8(b'l') // `u8` is a special method for u8 type
.u8(b'o')
.encode(&mut code);
assert_eq!(&code, b"hello");Provided Methods§
Sourcefn u8(self, u: u8) -> impl Encode
fn u8(self, u: u8) -> impl Encode
Appends a u8 value to the encodable sequence.
§Examples
use co::EncodeExt;
let mut code = [0];
().u8(37).encode(&mut code);
assert_eq!(code, [37]);Sourcefn u16(self, u: u16) -> impl Encode
fn u16(self, u: u16) -> impl Encode
Appends a u16 value to the encodable
sequence with big-endian byte ordering.
§Examples
use co::EncodeExt;
let mut code = [0; 2];
().u16(255).encode(&mut code);
assert_eq!(code, [0, 0xFF]);Sourcefn u32(self, u: u32) -> impl Encode
fn u32(self, u: u32) -> impl Encode
Appends a u32 value to the encodable
sequence with big-endian byte ordering.
§Examples
use co::EncodeExt;
let mut code = [0; 4];
().u32(255).encode(&mut code);
assert_eq!(code, [0, 0, 0, 0xFF]);Sourcefn u64(self, u: u64) -> impl Encode
fn u64(self, u: u64) -> impl Encode
Appends a u64 value to the encodable
sequence with big-endian byte ordering.
§Examples
use co::EncodeExt;
let mut code = [0; 8];
().u64(255).encode(&mut code);
assert_eq!(code, [0, 0, 0, 0, 0, 0, 0, 0xFF]);Sourcefn u128(self, u: u128) -> impl Encode
fn u128(self, u: u128) -> impl Encode
Appends a u128 value to the encodable
sequence with big-endian byte ordering.
§Examples
use co::EncodeExt;
let mut code = [0; 16];
().u128(255).encode(&mut code);
assert_eq!(code, [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0xFF,
]);Sourcefn usize(self, u: usize) -> impl Encode
fn usize(self, u: usize) -> impl Encode
Appends a usize value to the encodable
sequence with big-endian byte ordering.
§Examples
use co::EncodeExt;
let mut code = [0; size_of::<usize>()];
().usize(255).encode(&mut code);
assert_eq!(code.last(), Some(&0xFF));Sourcefn encode<B>(self, buf: &mut B) -> Result<&mut [u8], usize>
fn encode<B>(self, buf: &mut B) -> Result<&mut [u8], usize>
Encodes the sequence and writes the result into the buffer.
Returns Ok with a slice of the written data if the buffer size matches the
size of the encoded sequence. Otherwise, it returns Err with the required
size. In case of an error, expand the buffer to the required size and call this
method again.
§Examples
use {core::mem::MaybeUninit, co::EncodeExt};
let mut buf = [MaybeUninit::uninit(); 3];
let code = b"uwu".encode(&mut buf);
// The buffer length matches the size of the encoded sequence
assert!(code.is_ok_and(|code| code == b"uwu"));
let code = b"uwu".encode(&mut buf[..2]);
// Error: buffer length must be equal to 3
assert_eq!(code, Err(3));§Note
For more optimal buffer allocation, you can preallocate more than necessary for the sequence, but this method must be called with exactly the required length. The implementation does not truncate the buffer if needed; this is the caller responsibility.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.