Skip to content

Commit

Permalink
Ouch
Browse files Browse the repository at this point in the history
  • Loading branch information
Speykious committed Feb 22, 2024
1 parent 667ffe8 commit d4c1bdc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
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

0 comments on commit d4c1bdc

Please sign in to comment.