feat: support cloning random handles
Add OsRandom::try_clone for fallible duplication of the underlying /dev/urandom file handle, and implement Clone as the infallible convenience form. This gives independent call sites their own OsRandom values without adding interior mutability or shared buffering to the core type. The fallible method is documented as the preferred API when callers need to handle a failed file-handle duplication. Clone mirrors Default by panicking only for the convenience path. Document both cloning forms with doctested examples and add a runnable clone example that uses independent handles for IDs and string helpers. Test Plan: - cargo test - cargo clippy - cargo clippy --benches - cargo clippy --tests - cargo +nightly fmt Refs: IDEAS.md ergonomics backlog
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
//! Run with: `cargo run --example clone`
|
||||
|
||||
use ez_urandom::OsRandom;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let mut ids = OsRandom::try_new()?;
|
||||
let mut tokens = ids.try_clone()?;
|
||||
let mut fallback = tokens.clone();
|
||||
|
||||
println!("id : {}", ids.get_u64()?);
|
||||
println!("token : {}", tokens.token(16)?);
|
||||
println!("fallback: {}", fallback.hex(16)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user