Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unsized coercions for Lazy type. #159

Open
EFanZh opened this issue Sep 3, 2021 · 2 comments
Open

Support unsized coercions for Lazy type. #159

EFanZh opened this issue Sep 3, 2021 · 2 comments

Comments

@EFanZh
Copy link

EFanZh commented Sep 3, 2021

Currently, the following code does not compile:

use once_cell::sync::Lazy;
use std::sync::Arc;

fn foo() {
    let x: Arc<Lazy<[i32; 3]>> = Arc::new(Lazy::new(|| [2, 3, 4]));
    let y: Arc<Lazy<[i32]>> = x;
}

Is it possible for Lazy to support unsized coercions like types like RefCell? For example, with RefCell, I can do this:

use std::cell::RefCell;
use std::sync::Arc;

fn foo() {
    let x: Arc<RefCell<[i32; 3]>> = Arc::new(RefCell::new([2, 3, 4]));
    let y: Arc<RefCell<[i32]>> = x;
}
@matklad
Copy link
Owner

matklad commented Sep 3, 2021

What a delightful nerdy issue, thanks!

Yeah, I think that should be possible with some work. I think (haven't checked) that the reason why it doesn't work today is because we ultimately store an Option. Using MaybeUninit and a bool flag might help here!

@EFanZh
Copy link
Author

EFanZh commented Sep 3, 2021

Now I start to think it might not be possible. The problem is that Lazy has another generic argument F which should be a FnOnce type. So the sized Lazy type could be:

Lazy<[i32; 3], impl FnOnce() -> [i32; 3]>

And the unsized Lazy type would be

Lazy<[i32], impl FnOnce() -> [i32]>

I have not idea how the conversion would work. If it is not possible for Lazy, how about OnceCell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants