Skip to main content

SecretBytesMut

Struct SecretBytesMut 

Source
pub struct SecretBytesMut { /* private fields */ }
Expand description

Clear-on-drop wrapper around BytesMut.

Clearing expands the buffer to its reported capacity, volatile-clears that initialized view, then resets the length to zero. This covers the owned capacity exposed by BytesMut; it does not make claims about allocator internals outside that buffer.

§Security

This wrapper treats capacity as fixed after construction. Appending beyond capacity would force BytesMut to reallocate and free the old allocation while it still contains secret bytes. SecretBytesMut::extend_from_slice therefore returns CapacityError instead of growing implicitly. Allocate the maximum expected size up front with SecretBytesMut::with_capacity.

Implementations§

Source§

impl SecretBytesMut

Source

pub fn new() -> Self

Create an empty secret byte buffer.

Source

pub fn with_capacity(capacity: usize) -> Self

Allocate secret byte storage with at least capacity bytes.

Source

pub fn from_slice(bytes: &[u8]) -> Self

Copy a slice into a new secret byte buffer.

Source

pub fn from_bytes_mut(inner: BytesMut) -> Self

Wrap an existing BytesMut.

Source

pub fn len(&self) -> usize

Number of initialized bytes.

Source

pub fn is_empty(&self) -> bool

Returns true when there are no initialized bytes.

Source

pub fn capacity(&self) -> usize

Reported capacity of the underlying BytesMut.

Source

pub fn extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), CapacityError>

Append bytes to the secret buffer without reallocating.

Returns CapacityError if the append would exceed the current capacity. This avoids leaving secret bytes in a freed old allocation after an implicit BytesMut growth.

Source

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

Borrow initialized bytes.

Source

pub fn with_secret<R>(&self, inspect: impl FnOnce(&[u8]) -> R) -> R

Run a closure with read-only access to initialized bytes.

Source

pub fn with_secret_mut<R>(&mut self, edit: impl FnOnce(&mut [u8]) -> R) -> R

Run a closure with mutable access to initialized bytes.

Source

pub fn clear_secret(&mut self)

Sanitize the reported capacity and clear the buffer.

Source

pub fn into_cleared(self)

Consume after first sanitizing all accessible capacity.

Trait Implementations§

Source§

impl Debug for SecretBytesMut

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SecretBytesMut

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for SecretBytesMut

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl SecureSanitize for SecretBytesMut

Source§

fn secure_sanitize(&mut self)

Clear the sensitive bytes owned by this value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.