Skip to content

Instantly share code, notes, and snippets.

@8051Enthusiast
Last active November 10, 2023 07:54
Show Gist options
  • Save 8051Enthusiast/0a9411b6ce82c3b9f0500a63c8324784 to your computer and use it in GitHub Desktop.
Save 8051Enthusiast/0a9411b6ce82c3b9f0500a63c8324784 to your computer and use it in GitHub Desktop.
fucked up fizzbuzz (compile with `RUST_MIN_STACK=1000000000` and cranelift backend (and roughly 10GB of memory))
#![recursion_limit = "1000000"]
trait N {
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(fun: impl FnMut());
}
impl N for i8 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<Self, C, D, E, F, G, H, I>(fun)
}
}
impl N for u8 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<C, Self, D, E, F, G, H, I>(fun)
}
}
impl N for i16 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<C, D, Self, E, F, G, H, I>(fun)
}
}
impl N for u16 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<C, D, E, Self, F, G, H, I>(fun)
}
}
impl N for i32 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<C, D, E, F, Self, G, H, I>(fun)
}
}
impl N for u32 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<C, D, E, F, G, Self, H, I>(fun)
}
}
impl N for i64 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<C, D, E, F, G, H, Self, I>(fun)
}
}
impl N for u64 {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(mut fun: impl FnMut()) {
fun();
B::f::<C, D, E, F, G, H, I, Self>(fun)
}
}
impl N for () {
#[inline(never)]
fn f<B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(_: impl FnMut()) {}
}
#[inline(never)]
fn repeat_n_times_impl<A: N, B: N, C: N, D: N, E: N, F: N, G: N, H: N, I: N>(
fun: impl FnMut(),
n: u8,
) {
if n == 0xff {
return A::f::<B, C, D, E, F, G, H, I>(fun);
}
let bit = 7 - n.leading_ones();
let n = n | (1 << bit);
(match bit {
0 => repeat_n_times_impl::<B, A, C, D, E, F, G, H, I>,
1 => repeat_n_times_impl::<A, C, B, D, E, F, G, H, I>,
2 => repeat_n_times_impl::<A, B, D, C, E, F, G, H, I>,
3 => repeat_n_times_impl::<A, B, C, E, D, F, G, H, I>,
4 => repeat_n_times_impl::<A, B, C, D, F, E, G, H, I>,
5 => repeat_n_times_impl::<A, B, C, D, E, G, F, H, I>,
6 => repeat_n_times_impl::<A, B, C, D, E, F, H, G, I>,
7 => repeat_n_times_impl::<A, B, C, D, E, F, G, I, H>,
_ => unreachable!(),
})(fun, n)
}
fn repeat_n_times(fun: impl FnMut(), n: u8) {
repeat_n_times_impl::<i8, u8, i16, u16, i32, u32, i64, u64, ()>(fun, n)
}
fn main() {
let mut i = 0;
repeat_n_times(|| {
i += 1;
match (i % 3, i % 5) {
(0, 0) => println!("fizzbuzz"),
(0, _) => println!("fizz"),
(_, 0) => println!("buzz"),
_ => println!("{}", i),
}
}, 100)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment