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

Use shorter lifetimes #1

Closed
alexkreidler opened this issue Aug 24, 2020 · 3 comments
Closed

Use shorter lifetimes #1

alexkreidler opened this issue Aug 24, 2020 · 3 comments

Comments

@alexkreidler
Copy link

Right now, I use the function: fn nurse( &self, fut: impl Future<Output = Out> + Send + 'static ) -> Result<(), NurseErr>; This unfortunately means I can't use references in my async fn with a lifetime less than 'static, which is quite inconvenient.

I'm not really sure of the complexity of changing that, but I'd appreciate any thoughts you have on it.

@najamelan
Copy link
Owner

najamelan commented Aug 24, 2020

You can't spawn non static futures on any executor without unsafe. The problem is that you can use std::mem::forget to leak the nursery, which would then outlive it's parent scope, which might hold the pointed to value. Thus when the stack frame ends, that value get's dropped, but the nursery can outlive it and still run code that has a reference to that memory. That is a memory safety issue which is not allowed in safe rust.

The only crate that can help you here is https://docs.rs/async-scoped which requires you use unsafe. If you want to go that path, I would suggest you use async-scoped. Unless there is some very good arguments for it and it can be implemented cleanly, I don't have any plans to add such an unsafe API to async_nursery. That being said, if you have some very compelling argument, please share.

Some more discussion about the issue can be found here: tokio-rs/tokio#1879 (comment)

@najamelan
Copy link
Owner

najamelan commented Aug 24, 2020

ps: I'm adding this to the readme: 6c2620e#diff-04c6e90faac2675aa89e2176d2eec7d8R69

ps2: If you are struggling to design something, don't hesitate to ask. Maybe there's a better way to achieve what you want.

@alexkreidler
Copy link
Author

Thanks!

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