Skip to main content

LocalState

Struct LocalState 

Source
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: LocalState is not safe to use within signal handlers. Reentrant usage (e.g., invoking LocalState::fill from a signal handler that interrupts a LocalState::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>

Source

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.

Source

pub fn try_fill(&mut self, buf: &mut [u8], flag: c_uint) -> Result<usize, Error>

Fill the provided buffer with random bytes. This method may not fill the entire buffer due to interrupts or low entropy conditions.

Source

pub fn fill(&mut self, buf: &mut [u8], flag: c_uint) -> Result<(), Error>

Fill the provided buffer with random bytes. This method will block until the buffer is filled. It is implemented as a loop wrapping around LocalState::try_fill.

Trait Implementations§

Source§

impl<'a> Drop for LocalState<'a>

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

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> 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 = !

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.