# Destructorless types

**URL:** <https://internals.rust-lang.org/t/destructorless-types/7968>\
**Category:** Uncategorized\
**Created:** [July 16, 2018, 8:08pm UTC](https://internals.rust-lang.org/t/destructorless-types/7968 "2018-07-16T20:08:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [July 16, 2018, 8:08pm UTC](https://internals.rust-lang.org/t/destructorless-types/7968/1 "2018-07-16T20:08:44Z")

</div>

I recently run into the somewhat obnoxious fact that if `T: Drop`, then you can’t move out of public fields of `T` (for obvious reasons). In the presence of a [C++ ptr-to-member type](https://internals.rust-lang.org/t/pre-pre-rfc-field-offsets/7845/7) this actually becomes really annoying, because there’s no way to write a function that takes a generic `T`, a field offset over it, and uses it to move out of the `T` with it:

```rust
// using syntax from my other post
fn move_out_of<T, U>(x: T, offset: T.U) -> U { 
    x.(offset)
    // drop glue here
}
// .. imagine we're in liballoc
let x: Unique<i32> = move_out_of(Box::new(0), offsetof Box::<_>.0); // unsound!

```

It’s unclear if this is a problem, since we could, in principle, make `T.U` unilaterally uninhabited if `T: Drop`… but then we’re not allowed to do write this function, which is _always_ sound:

```rust
fn ref_out_of<T, U>(x: &T, offset: T.U) -> &U {
    &x.(offset)
    // drop glue? what drop glue?
}

```

I think this problem can be solved completely without touching the compiler:

```rust
// in core::marker or whatever
pub unsafe auto trait NoDrop {}
impl<T: Drop> !NoDrop for T {}

```

I’m pretty sure implementing this trait manually is _always_ UB. Of course, this trait needs to be turned into a lang item for the purposes of generic move, so you can write

```rust
fn move_out_of<T: NoDrop, U>(x: T, offset: T.U) -> U { 
    x.(offset)
}

```

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 16, 2018, 8:12pm UTC](https://internals.rust-lang.org/t/destructorless-types/7968/2 "2018-07-16T20:12:40Z")

</div>

There’s not generally support for negative trait bounds, but one thing you can rely on is that `Copy` and `Drop` are exclusive. Of course, this still leaves many types that are neither.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [July 17, 2018, 2:03am UTC](https://internals.rust-lang.org/t/destructorless-types/7968/3 "2018-07-17T02:03:45Z")

</div>

Agreed- Niko’s chalk blogposts convinced me that negative reasoning is a pretty bad idea, and special casing `T: !Drop` is… gross, much like `T: ?Sized` is. I’m proposing the addition of an `auto trait`, for which de-implementations are allowed.

A lot of this comes out of the realization that “has trivial destructor” is the sort of assertion you’d like to be able to use, but because of how destructors were implemented we’re kind of stuck in this hole where we need to add a negation trait. Afaik, though, the compiler will accept my definition of `NoDrop` just fine.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/destructorless-types/7968/4 "2019-03-25T08:30:31Z")

</div>

Closed on March 25, 2019, 8:30am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
