Skip to content

Instantly share code, notes, and snippets.

@eira-fransham
Created September 29, 2021 10:47
Show Gist options
  • Save eira-fransham/8c816600947e79390cb545e46581be6b to your computer and use it in GitHub Desktop.
Save eira-fransham/8c816600947e79390cb545e46581be6b to your computer and use it in GitHub Desktop.
#![feature(allocator_api)]
mod cursed_lazy {
use std::{
alloc::{Allocator, Layout, AllocError},
ptr::NonNull,
};
pub struct LazyAlloc;
unsafe impl Allocator for LazyAlloc {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
todo!()
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
todo!()
}
}
pub type Box<T> = std::boxed::Box<T, LazyAlloc>;
pub struct LazyCtx;
impl LazyCtx {
pub fn lazy<F, O>(&self, func: F) -> O
where
F: FnOnce() -> O,
{
todo!()
}
}
pub fn lazy<F, O>(func: F) -> Box<O>
where
F: FnOnce(&LazyCtx) -> O,
{
todo!()
}
}
macro_rules! lazy {
($val:expr) => {
cursed_lazy::lazy(|ctx| {
macro_rules! lazy {
($inner_val:expr) => {
ctx.lazy(|| $inner_val)
};
}
$val
})
};
}
struct Foo {
elem: u64,
inner: cursed_lazy::Box<Foo>,
}
struct Bar {
a: u32,
b: u32,
c: u32,
}
fn test() {
let mut i = 0;
fn make_foo(i: u64) -> impl FnOnce(&cursed_lazy::LazyCtx) -> Foo {
move |ctx: &cursed_lazy::LazyCtx| Foo {
elem: i,
inner: cursed_lazy::lazy(make_foo(i + 1)),
}
}
let lazy: cursed_lazy::Box<Foo> = cursed_lazy::lazy(make_foo(0));
let lazy: cursed_lazy::Box<Bar> = lazy!(Bar { a: lazy!(1), b: lazy!(2), c: lazy!(3) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment