pub struct RngGuard {
_private: (),
}Expand description
RAII guard for R’s RNG state.
Calls GetRNGstate() on creation and PutRNGstate() on drop.
This ensures RNG state is properly saved even if the function panics
or returns early.
§Example
ⓘ
use miniextendr_api::rng::RngGuard;
use miniextendr_api::ffi::unif_rand;
fn generate_uniform() -> f64 {
let _guard = RngGuard::new();
unsafe { unif_rand() }
}§Warning: R Longjumps
This guard relies on Rust’s drop semantics. If R triggers a longjmp
(via Rf_error etc.), the destructor will NOT run unless the code
is wrapped in with_r_unwind_protect. For functions exposed to R,
prefer using #[miniextendr(rng)] which handles this correctly.
§Safety
Must be used on R’s main thread. The guard assumes it has exclusive access to R’s RNG state while alive.
Fields§
§_private: ()Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RngGuard
impl RefUnwindSafe for RngGuard
impl Send for RngGuard
impl Sync for RngGuard
impl Unpin for RngGuard
impl UnsafeUnpin for RngGuard
impl UnwindSafe for RngGuard
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