Skip to main content

Bytes

Trait Bytes 

Source
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§

Source

fn capacity(&self) -> usize

返回容器容量(字节),与已写入长度无关。

§Feature Requirement

需要启用 "types-bytes" 特性。

Source

fn len(&self) -> usize

返回已写入数据的长度(字节)。

§Feature Requirement

需要启用 "types-bytes" 特性。

Source

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
Source

fn wipe_data(&mut self)

清零全部底层存储并将长度置 0。

§Feature Requirement

需要启用 "types-bytes" 特性。

Provided Methods§

Source

fn is_empty(&self) -> bool

已写入数据是否为空。

§Feature Requirement

需要启用 "types-bytes" 特性。

Source

fn as_slice(&self) -> &[u8] ⓘ

以不可变切片查看已写入数据。

§Feature Requirement

需要启用 "types-bytes" 特性。

Source

fn as_mut_slice(&mut self) -> &mut [u8] ⓘ

以可变切片访问已写入数据(不改变长度)。

§Feature Requirement

需要启用 "types-bytes" 特性。

Source

fn extend_into<T>(self, other: &[u8]) -> BytesResult<T>
where Self: Sized, T: Bytes + Default,

将自身内容与 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
Source

fn extend_and_wipe<T>(&mut self, source: T) -> BytesResult
where Self: Sized, T: AsRef<[u8]> + AsMut<[u8]>,

将 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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<const N: usize> Bytes for HeapBytes<N>

Source§

impl<const N: usize> Bytes for StackBytes<N>