Skip to content

Commit

Permalink
Add ability to step on a lego (#25)
Browse files Browse the repository at this point in the history
- Resolves #23
  • Loading branch information
Speykious authored Feb 23, 2024
2 parents 63a0aae + d4c1bdc commit c65a653
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

1 comment on commit c65a653

@wizzymore
Copy link

@wizzymore wizzymore commented on c65a653 Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

The legos now

Please sign in to comment.