Closed nsunderland1 closed 1 year ago
An amendment: Cell
only implements Clone
for T: Copy
, which makes this less useful but not completely useless.
https://doc.rust-lang.org/std/cell/struct.Cell.html#impl-Clone-for-Cell%3CT%3E
Oh and the implementation I posted above will definitely not work for sync::Lazy
, only unsync::Lazy
, because of the cell. I think the only way you could implement Clone
for sync::Lazy
would be to do something silly like force
it in clone
Yeah, I think we should do this for unsync. For sync, it's tricky. We managed to do this for OnceCell: https://github.com/matklad/once_cell/issues/31, but I am not entirelysure that'll work for lazy
Display and Debug and Selialize and etc... also should implemented for Lazy.
const MESSAGE: Lazy<String> = Lazy::new(|| "Hello, World!");
println!("hoge {}",MESSAGE);
This code does not work because Lazy
Thinking more about this, no, I don't think either sync or unsync flavor should implement Clone.
Which is better:
T: Clone
F
and potentially forcing it twiceSeems unclear, and, given that reducing work is the reason for OnceCell to exist, this seems like an important question which is better left to the user to decide.
It seems like it should be possible for
Lazy<T, F>
(both sync and unsync, unless there's a subtlety that I'm missing) to implementClone
if bothT
andF
implementClone
, keeping in mind that closures implementClone
if all their captures do.Right now if you want to clone a
Lazy
, you're forced to useOnceCell
instead.