Here is a snippet from some (more realistic) code for creating a struct that contains a closure which, when run, will delete the struct:
struct Foo<F> {
f: Option<F>,
}
unsafe fn make_self_deleting() -> impl Sized {
let foo = Box::into_raw(Box::new(Foo { f: None }));
(*foo).f = Some(|| {
let _ = Box::from_raw(foo);
});
foo
}
(Playground)
This involves a self-referential type, since the struct Foo is generic over the closure that it contains, but the closure captures a pointer to Foo, so it's unsurprising that the compiler rejects it. However the error is a bit weird:
cyclic type of infinite size
I agree with the "cyclic" part, but I was surprised by the "infinite size" part. That's not actually true here, is it? The closure needs to capture only one pointer.