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 Drop #3541
Async Drop #3541
Conversation
How would drop glue be handled? |
Good question! In my initial thought, I considered adopting a LIFO approach, much like the synchronous case. This is evident in the management of #[tokio::main]
async fn main() {
{
let async_res1 = AsyncResource {
data: String::from("Example Data 1"),
};
let async_res2 = AsyncResource {
data: String::from("Example Data 2"),
};
// The drop method will be triggered here
}
// `async_res2` will be dropped first, followed by `async_res1`
} In this case, I anticipated that the However, upon reconsideration, I am now pondering the implications within the context of the event loop. The event loop introduces an asynchronous dimension, raising questions about how these asynchronous resources will be dropped. For this reason, I am open to any further refinements of the RFC. |
Is this not just proposing to add garbage collection back into the language? I mean, what you're effectively proposing is to, instead of immediately dropping resources when they fall out of scope, to mark them for reclamation and add them to a task queue. That's basically what asynchronous drop is, after all. Except this is obviously a much more naïve implementation. The main concern I have with this approach is that it can be very confusing to now have this weird matrix of drop traits depending on what is implemented. I would expect there to be some safeguard for dropping things in a sync context with only dedicated There is value to complicated destructors, I just personally avoid them and thus can't see much value in this implementation since it seems prone to lots of error. If you want to do anything with blocking I/O in a destructor, that feels wrong to me, so, what other cases would you want to block? |
No. It is more like improving the ownership model. It is like instead of throwing out things when you're done with them, carefully execute some custom logic when dropping these items. It's not about reintroducing garbage collection; it's about upgrading our ownership model to be more refined and efficient.
The introduction of
Yes. I am currently researching this area. I think
trait AsyncDrop {
async fn drop(self);
}
impl AsyncDrop for AsyncConnection {
async fn drop(self) {
// Async operation to close the connection
self.close().await;
}
}
trait AsyncDrop {
async fn drop(self);
}
impl AsyncDrop for DatabaseConnection {
async fn drop(self) {
// Async operation to release the connection to the pool
self.release_to_pool().await;
}
} and so forth. In these examples, async drop allows you to encapsulate and execute asynchronous cleanup logic when an object goes out of scope, providing a convenient and structured way to handle resources that involve asynchronous operations during cleanup. Finally, I am open to additional enhancements or further refinements to this RFC. |
use std::future::Future;
fn block_on<T>(fut: impl Future<Output = T>) -> T {
use std::pin::pin;
use std::task::{Waker, Context, Poll};
let waker = Waker::noop();
let mut cx = Context::from_waker(&waker);
let mut fut = pin!(fut);
loop {
if let Poll::Ready(value) = fut.as_mut().poll(&mut cx) {
return value;
}
}
} if
What is the "appropriate context" here? |
I don't think I communicated my intent properly. What I meant is for code like this:
how does the compiler handle the dropping of each of the fields of |
Thanks for your contribution to the conversation, @SkiFire13! I have updated the RFC accordingly. |
This is written using the same method from the author's previous RFC. There are many parts of the RFC that reek of this gunk. It contains long, drawn out sentences ("High fructose corn sentences") and plenty of internal inconsistencies. I understand everyone is trying to respond and critique in good faith despite the author's past actions, but I would rather this time be spent on an actual human written RFC with meaningful content. |
I don't want to address this statement because it is counterproductive. If you have additional points to share, please do so. We all make mistakes, and I'm far from perfect. While I have the option to submit an RFC from an alt account, I'm not afraid or hesitant to reveal my true identity, even if it could have consequences for my career.
We would greatly appreciate your help in enhancing this RFC. Your insights and suggestions are invaluable in refining and optimizing the proposal.
Again, we all use LLMs in one way or another. Saying otherwise is a straight-up lie. |
Thanks for the follow-up @python-ast-person. I am going to address your concerns. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be my last review here. It is clear who or what wrote this RFC lacks understanding of basic Rust features, not to mention async
. You should focus on that before proposing changes to the language.
I spent way too much time reading this proposal and I'll avoid further comments.
Again, we all use LLMs in one way or another. Saying otherwise is a straight-up lie.
Speak for yourself, I've never used a LLM except for testing what they are capable when they first became available. I recognize they can be useful to setup a paragraph but whatever technical information they split out cannot be relied upon.
Once again, thank you to everyone who has contributed to this conversation to improve the RFC. I will dedicate further efforts to its enhancement over the weekend. Your valuable contributions are greatly appreciated. |
AI generated RFCs are not allowed here. |
Introduction
This PR proposes the introduction of native async drop support in Rust by adding the
async fn drop
method to theDrop
trait. Additionally, it suggests the creation of anAsyncDrop
trait, allowing us to define custom asynchronous cleanup logic.Motivation
The primary motivation is to standardize asynchronous resource cleanup in Rust, streamlining adoption and reducing dependency management overhead. This enhancement addresses the current gap in Rust's support for async lifetimes, making the language more versatile for modern asynchronous applications.
Implementation and Benefits
The design introduces native async drop support through the
async fn drop
method, providing a standardized way to handle asynchronous cleanup. The proposal also outlines anAsyncDrop
trait, enabling us to define custom asynchronous cleanup logic for our types. This native support simplifies codebases, reduces external dependencies, and fosters a more consistent and streamlined experience for us working with asynchronous resource management.Rendered