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

Async/await support? #167

Open
llebout opened this issue Jan 11, 2020 · 5 comments
Open

Async/await support? #167

llebout opened this issue Jan 11, 2020 · 5 comments

Comments

@llebout
Copy link

llebout commented Jan 11, 2020

Currently it's not possible to do:

lazy_static!{
        static ref FOO: bar = MyFooStruct::new().await;
}
@BurntSushi
Copy link
Member

Could you say more about why you think this library should support that? What exactly would that entail?

@llebout
Copy link
Author

llebout commented Jan 11, 2020

@BurntSushi I do not know about the internals of that library so I would not know. It seems that the macro creates a block of code that is synchronous and .await can only be used in async blocks. It is impossible to use lazy_static! within asynchronous functions right now.

@KodrAus
Copy link
Contributor

KodrAus commented Jan 13, 2020

Hi @leo-lb 👋

I don't think it's feasible for lazy_static to support awaiting futures internally, but you can technically wrap a Future in a lazy_static:

async fn make_i32() -> i32 {
    41
}

lazy_static! {
    static ref FUT: Box<dyn Future<Output = i32> + Sync> = Box::new(async {
        let r = make_i32().await;
        r + 1
    });
}

You can't do much with that future because you can't get mutable access to it without some additional synchronization, and futures tend to be poisoned after producing their result the first time. At this stage, I think this is out-of-scope for lazy_static to deal with.

So I think the best way forward here would be either fork lazy_static and see what a usable Future-aware implementation might look like, or build a library like async_lazy_static on top of lazy_static with the synchronization pieces needed to capture a static future and yield its static result correctly.

@hjiayz
Copy link

hjiayz commented Jul 17, 2020

like this?
https://crates.io/crates/async_once

@llebout
Copy link
Author

llebout commented Jul 17, 2020

@KodrAus hey there! I forgot about this issue.

What I was thinking about is to await the future the first time and then store the resulting value and that's all?

@hjiayz

That looks like it, thanks for sharing!

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

4 participants