808e0482c3
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
16 lines
382 B
Rust
16 lines
382 B
Rust
//! 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(())
|
|
}
|