pub struct LocalState<'a> { /* private fields */ }Expand description
A local state for vDSO-based getrandom operations.
This state is rented from a shared Pool and used to fill buffers with random bytes.
use vdso_rng::{Pool, LocalState};
let pool = Pool::new().expect("Failed to create shared pool");
let mut local_state = LocalState::new(&pool).expect("Failed to create local state");
let mut buf = [0u8; 64];
local_state.fill(&mut buf, 0).expect("Failed to fill buffer");
assert!(buf.iter().any(|&x| x != 0), "Buffer should not be empty");Typically, LocalState is intended to be used in a thread-local fashion. A thread rents
a state once and reuses it for multiple getrandom calls. Since acquiring a new state incurs
synchronization overhead, reusing the state within the same thread is strongly recommended.
On drop, the state is returned to the pool for reuse.
§Safety
- Not async-signal-safe:
LocalStateis not safe to use within signal handlers. Reentrant usage (e.g., invokingLocalState::fillfrom a signal handler that interrupts aLocalState::fill) can lead to secret leakage or corruption. - Reentrancy detection: While Rust’s borrowing rules typically prevent such misuse, it may occur in the presence of undefined behavior. Under debug builds, reentrancy is explicitly checked and will panic if detected.
Implementations§
Source§impl<'a> LocalState<'a>
impl<'a> LocalState<'a>
Sourcepub fn new(pool: &'a Pool) -> Result<Self, Error>
pub fn new(pool: &'a Pool) -> Result<Self, Error>
Create a new local state from the given pool. The pool must outlive the local state.
Trait Implementations§
Source§impl<'a> Drop for LocalState<'a>
impl<'a> Drop for LocalState<'a>
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for LocalState<'a>
impl<'a> !Sync for LocalState<'a>
impl<'a> !UnwindSafe for LocalState<'a>
impl<'a> Freeze for LocalState<'a>
impl<'a> Send for LocalState<'a>
impl<'a> Unpin for LocalState<'a>
impl<'a> UnsafeUnpin for LocalState<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more