It seems as if Lazy
has a larger size than necessary.
Here is the current definition:
pub struct OnceCell<T> {
// Invariant: written to at most once.
inner: UnsafeCell<Option<T>>,
}
pub struct Lazy<T, F = fn() -> T> {
cell: OnceCell<T>,
init: Cell<Option<F>>,
}
Either cell
or init
will be empty, but Lazy<T, F>
still needs the memory of T
and F
combined. An alternative might be an OnceCell
that takes two types:
enum OnceCellContent<T, U> {
Init(T),
Uninit(U)
}
pub struct OnceCell<T, U> {
// Invariant: written to at most once.
inner: UnsafeCell<OnceCellContent<T, U>>,
}
pub struct Lazy<T, F = fn() -> T> {
cell: OnceCell<T, F>
}
I didn't find any previous discussion about the size of Lazy
, so perhaps I've missed something and this would be a bad idea for some reason. Or what do you think?