# A type that is sometimes Drop and sometimes Copy: is this even theoretically possible?

**URL:** <https://internals.rust-lang.org/t/a-type-that-is-sometimes-drop-and-sometimes-copy-is-this-even-theoretically-possible/14234>\
**Category:** libs\
**Created:** [March 10, 2021, 11:33pm UTC](https://internals.rust-lang.org/t/a-type-that-is-sometimes-drop-and-sometimes-copy-is-this-even-theoretically-possible/14234 "2021-03-10T23:33:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [March 10, 2021, 11:33pm UTC](https://internals.rust-lang.org/t/a-type-that-is-sometimes-drop-and-sometimes-copy-is-this-even-theoretically-possible/14234/1 "2021-03-10T23:33:14Z")

</div>

(This kind of toes the line between irlo and urlo, as well as between the libs and language categories.)

Is it even theoretically possible to have a coherent language extension that allows a type to conditionally implement both `Drop` and `Copy` (in a disjoint manner, of course)?

My motivating example is [`erasable::Thin|](https://docs.rs/erasable/1.2.1/erasable/struct.Thin.html). It's a type roughly formulated as

```rust
struct Thin<Ptr: CanBeErased> {
    ptr: ErasedPtr,
    phantom<Ptr>,
}

```

(`ErasedPtr` is just `ptr::NonNull<VoldemortZST>`.) And, ideally, I'd want to support

```rust
impl<Ptr: CanBeErased> Copy for Thin<Ptr>
where
    Ptr: Copy,
{}

impl<Ptr: CanBeErased> Drop for Thin<Ptr>
where
    Ptr: !Copy,
{
    fn drop(&mut self) {
        let ptr: Ptr = Ptr::unerase(self.ptr);
        drop(ptr);
    }
}

```

This obviously isn't possible with current Rust, for multiple reasons:

- Drop cannot be bounded any differently than on the base structure (Drop impl must be universal).
- Negative bounds cannot be expressed (!Copy).
- Drop and Copy are mutually exclusive traits. (At least for a given instantiation of a type. As Drop is universal, we don't know whether a conditional Drop could share space with `Copy` soundly.)
- Coherence checker doesn't like having to prove disjointness of impls.

* * *

So my actual question here is: is it feasible for _some_ future version of Rust to support this code (or something equivalent)?

I remain unconvinced as of posting this. While this is a "simple" case due to "obvious" disjointness (`Trait` and `!Trait`, there are a number of remaining flaws I'm worried about:

- `Drop` is special, and _very_ important to soundness and coherence of the Rust safety model. I know `Drop` impls used to not require being universal (pre-1.0) but were given that requirement to make them easier for the compiler to reason about.
- I'm pretty sure that we decided we'd ideally like `!Trait` mean "explicitly promised to not implement `Trait`", either by explicit `impl !Trait` or otherwise coherently prohibited from implementing the trait (maybe?). That way implementing a trait you didn't before still can't be a breaking change. (Thus neither impls would cover types which didn't impl `Copy` nor `Drop`, but might have (implementation detail) `Drop` glue.)

So I think it'll always be impossible to express `Thin` in a way that both inherits `Copy` and drop glue from its wrapped type in Rust, forever. I'd love to be proven wrong, though, even if I never see it available in rustc.

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [March 11, 2021, 7:03am UTC](https://internals.rust-lang.org/t/a-type-that-is-sometimes-drop-and-sometimes-copy-is-this-even-theoretically-possible/14234/2 "2021-03-11T07:03:17Z")

</div>

Maybe not directly, but there's always specialization plus associated types…

```rust
#![feature(specialization)]
struct Thin<Ptr> { inner: <Ptr as ThinHelper>::Inner }
trait ThinHelper { type Inner; }
impl<Ptr> ThinHelper for Ptr { default type Inner = ThinDrop<Ptr>; }
impl<Ptr: Copy> ThinHelper for Ptr { type Inner = ThinCopy<Ptr>; }
struct ThinDrop<Ptr> { phantom: std::marker::PhantomData<Ptr> }
impl<Ptr> Drop for ThinDrop<Ptr> { fn drop(&mut self) { /* ... */ } }
#[derive(Copy, Clone)]
struct ThinCopy<Ptr: Copy> { phantom: std::marker::PhantomData<Ptr> }

```

---

<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:** [June 9, 2021, 7:03am UTC](https://internals.rust-lang.org/t/a-type-that-is-sometimes-drop-and-sometimes-copy-is-this-even-theoretically-possible/14234/3 "2021-06-09T07:03:32Z")

</div>

Closed on June 9, 2021, 7:03am UTC

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