Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to step on a lego #25

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ debug = false
default = []
download-more-ram = ["ureq"]
give-up = ["oorandom"]
step-on-lego = ["oorandom"]

[[bench]]
name = "transmute"
Expand Down
33 changes: 23 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,35 @@ pub fn construct_fake_string(ptr: *mut u8, cap: usize, len: usize) -> String {
crate::transmute::<_, String>(actual_buf)
}

/// Good for job security.
#[cfg(feature = "give-up")]
pub fn give_up<T: 'static>() -> Box<T> {
#[cfg(any(feature = "give-up", feature = "step-on-lego"))]
fn seed() -> u64 {
use std::time::SystemTime;

let seed = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();

seed.as_secs()
}

/// It can be fatal if you step hard enough.
#[cfg(any(feature = "give-up", feature = "step-on-lego"))]
pub fn step_on_lego() -> u32 {
let mut rng = oorandom::Rand64::new(seed() as u128);

let lego = crate::transmute::<usize, &'static u32>(rng.rand_u64() as usize);

*lego
}

/// Good for job security.
#[cfg(any(feature = "give-up", feature = "step-on-lego"))]
pub fn give_up<T: 'static>() -> Box<T> {
let size = std::mem::size_of::<T>();

let mut v = Vec::with_capacity(size);

let mut rng = {
let seed = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();

oorandom::Rand32::new(seed.as_secs())
};
let mut rng = oorandom::Rand32::new(seed());

for _ in 0..size {
v.push((rng.rand_u32() % 256) as u8);
Expand Down