pub trait Bytes:
Deref<Target = [u8]>
+ DerefMut
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Default
+ Sized {
// Required methods
fn capacity(&self) -> usize;
fn len(&self) -> usize;
fn extend_from_slice(&mut self, other: &[u8]) -> BytesResult;
fn wipe_data(&mut self);
// Provided methods
fn is_empty(&self) -> bool { ... }
fn as_slice(&self) -> &[u8] ⓘ { ... }
fn as_mut_slice(&mut self) -> &mut [u8] ⓘ { ... }
fn extend_into<T>(self, other: &[u8]) -> BytesResult<T>
where Self: Sized,
T: Bytes + Default { ... }
fn extend_and_wipe<T>(&mut self, source: T) -> BytesResult
where Self: Sized,
T: AsRef<[u8]> + AsMut<[u8]> { ... }
}Available on crate features
types-bytes and types only.Expand description
定容字节容器的统一接口:写入、擦除与向更大容器搬运。
所有实现(StackBytes / HeapBytes)在 Drop 与显式擦除时经易失写清零。
§Feature Requirement
需要启用 "types-bytes" 特性。
§Examples
use lib_unknown::types::bytes::{Bytes, StackBytes};
let mut b = StackBytes::<8>::new();
b.extend_from_slice(b"hi").unwrap();
assert_eq!(b.as_slice(), b"hi");
b.wipe_data();
assert!(b.is_empty());Required Methods§
Sourcefn extend_from_slice(&mut self, other: &[u8]) -> BytesResult
fn extend_from_slice(&mut self, other: &[u8]) -> BytesResult
追加 other 的全部字节。
§Feature Requirement
需要启用 "types-bytes" 特性。
§Examples
use lib_unknown::types::bytes::{Bytes, StackBytes};
let mut b = StackBytes::<8>::new();
b.extend_from_slice(b"hi").unwrap();
assert_eq!(b.len(), 2);§Errors
- 当追加后总长度超出容量时返回
BytesError::CapacityExceeded,容器内容不变。
Provided Methods§
Sourcefn as_mut_slice(&mut self) -> &mut [u8] ⓘ
fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
Sourcefn extend_into<T>(self, other: &[u8]) -> BytesResult<T>
fn extend_into<T>(self, other: &[u8]) -> BytesResult<T>
将自身内容与 other 先后写入新容器,并擦除自身。
§Feature Requirement
需要启用 "types-bytes" 特性。
§Examples
use lib_unknown::types::bytes::{Bytes, StackBytes};
let mut b = StackBytes::<8>::new();
b.extend_from_slice(b"hi").unwrap();
let c: StackBytes<8> = b.extend_into(b"!").unwrap();
assert_eq!(c.as_slice(), b"hi!");§Errors
- 当任一写入超出目标容器容量时返回
BytesError::CapacityExceeded。
Sourcefn extend_and_wipe<T>(&mut self, source: T) -> BytesResult
fn extend_and_wipe<T>(&mut self, source: T) -> BytesResult
将 source 内容追加到自身,成功后清零 source;失败时 source 保持不变。
§Feature Requirement
需要启用 "types-bytes" 特性。
§Examples
use lib_unknown::types::bytes::{Bytes, StackBytes};
let mut b = StackBytes::<8>::new();
let mut secret = *b"hi";
b.extend_and_wipe(&mut secret).unwrap();
assert_eq!(secret, [0u8; 2]);§Errors
- 当追加后总长度超出容量时返回
BytesError::CapacityExceeded。
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".