Skip to content

Commit

Permalink
Add section about alternative poll_drop design
Browse files Browse the repository at this point in the history
  • Loading branch information
zetanumbers committed Apr 15, 2024
1 parent a1dfe7e commit 0c4c5eb
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/async-drop-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ its end. The `impl_trait_in_assoc_type` feature is used there to not
implement futures manually, perhaps this can be simplified further with
return-position `impl Trait` and async methods in traits.

### Choosing against `poll_drop`

You may wonder about possible alternative design of async drop, usually
named `poll_drop`:

```rust,ignore
#[lang = "async_drop"]
pub trait AsyncDrop {
fn poll_drop(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>;
}
```

We have decided against it since it would require to embed the state
of asynchronous destruction into the type itself. For example `Vec<T>`
would need to store an additional index to know which element is currently
in the process of asynchronous destruction (unless we `poll_drop` every
element on each parent call, but I imagine that could become expensive
quick, and it is not exactly symmetrical to how the regular `Drop`
functions). Also each element of the vector would require additional
space for these embedded asynchronous destructors, even tho it would be
utilized one at a time.

However there is indeed one benefit of `poll_drop` which I hypothesized
to be a supplemental interface [down below](#dyn_trait).

## Asynchronous drop glue

To run async drop glue on a type we can use public `async_drop` or
Expand Down Expand Up @@ -269,7 +294,7 @@ reference the dropped object, perhaps it would be more beneficial to have
would be less unsafe and we won't have to deal with pointers infecting
async destructor types with `!Send` and `!Sync`.

### Async drop glue for dyn Trait
### Async drop glue for dyn Trait { #dyn_trait }

The problem with dynamic types is basically about
loosing the type information. We cannot know `<dyn Trait as
Expand Down

0 comments on commit 0c4c5eb

Please sign in to comment.