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
impl SecretBytesMut
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Allocate secret byte storage with at least capacity bytes.
Sourcepub fn from_slice(bytes: &[u8]) -> Self
pub fn from_slice(bytes: &[u8]) -> Self
Copy a slice into a new secret byte buffer.
Sourcepub fn from_bytes_mut(inner: BytesMut) -> Self
pub fn from_bytes_mut(inner: BytesMut) -> Self
Wrap an existing BytesMut.
Sourcepub fn extend_from_slice(&mut self, bytes: &[u8]) -> Result<(), CapacityError>
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.
Sourcepub fn with_secret<R>(&self, inspect: impl FnOnce(&[u8]) -> R) -> R
pub fn with_secret<R>(&self, inspect: impl FnOnce(&[u8]) -> R) -> R
Run a closure with read-only access to initialized bytes.
Sourcepub fn with_secret_mut<R>(&mut self, edit: impl FnOnce(&mut [u8]) -> R) -> R
pub fn with_secret_mut<R>(&mut self, edit: impl FnOnce(&mut [u8]) -> R) -> R
Run a closure with mutable access to initialized bytes.
Sourcepub fn clear_secret(&mut self)
pub fn clear_secret(&mut self)
Sanitize the reported capacity and clear the buffer.
Sourcepub fn into_cleared(self)
pub fn into_cleared(self)
Consume after first sanitizing all accessible capacity.