# \[Feature request\] is\_bitwise\_copyable\<T\>()

**URL:** <https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888>\
**Category:** libs\
**Created:** [June 25, 2022, 4:25am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888 "2022-06-25T04:25:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tower120](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tower120/32/9533_2.png) [@tower120](https://internals.rust-lang.org/u/tower120)\
**Post date:** [June 25, 2022, 4:25am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/1 "2022-06-25T04:25:42Z")

</div>

Consider adding `is_bitwise_copyable<T:Clone>()` which return false if cloning values of type `T` matters.

Something similar could be achieved with impl specializations, by checking does T implements Copy. But that's not the same - Clone -able type may be bitwise-copyable too!

---

<div class="post-metadata">

**Author:** ![eminence](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eminence/32/4018_2.png) [@eminence](https://internals.rust-lang.org/u/eminence)\
**Post date:** [June 25, 2022, 4:57am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/2 "2022-06-25T04:57:30Z")

</div>

Isn't the standard way for a type to say "I can be bitwise copied" is for it to impl `Copy`? If such a type didn't implement copy, how would it signal to `is_bitwise_copyable` that it is, in fact, bitwise copyable?

---

<div class="post-metadata">

**Author:** ![Neutron3529](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/neutron3529/32/6976_2.png) [@Neutron3529](https://internals.rust-lang.org/u/Neutron3529)\
**Post date:** [June 25, 2022, 5:15am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/3 "2022-06-25T05:15:52Z")

</div>

Maybe, 1GB of data is bitwise copyable, but we may never copy the whole data.

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [June 25, 2022, 5:24am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/4 "2022-06-25T05:24:13Z")

</div>

No; it's possible that a type implements `Copy`, but its `Clone` impl still does funky things (like printing a message to stdout).

But, the compiler knows whether a given type derives `Clone` (and as such doesn't do odd things), or if it does implement `Clone` manually (and thus might or might not do odd things).

Some containers in stdlib, like `Vec`, have the "optimization" that uses `Copy` instead of `Clone` whenever applicable. This could theoretically break buggy code, but the intent of `Clone` has always been that if it breaks it's because the `Clone` impl is bad, not because `Vec` itself is bad.

So maybe the compiler could provide a special macro-like primitive `derived!(impl Trait for Type)`, that works for all traits for which Rust provides a `#[derive]`, and returns `true` if the impl was generated by a `#[derive]`, returns `false` if it was written by hand, and emits a compiler error if the impl doesn't even exist.

If this existed, it could be possible to forgo calling `Clone` only if it was automatically derived. There may be interesting cases where it's important to know whether `PartialEq`, `PartialOrd`, `Eq` and `Ord` were automatically derived, as well.

However, it doesn't solve everything because the type may be a struct that contains, inside it, types that had custom impls.

---

<div class="post-metadata">

**Author:** ![8573](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/8573/32/4913_2.png) [@8573](https://internals.rust-lang.org/u/8573)\
**Post date:** [June 25, 2022, 10:16am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/5 "2022-06-25T10:16:44Z")

</div>

> [@dlight](#):
>
> There may be interesting cases where it's important to know whether `PartialEq`, `PartialOrd`, `Eq` and `Ord` were automatically derived, as well.

A single derive macro can implement multiple traits, one of which could be used to mark types that used the macro (although types could lie by implementing such a marker trait manually). The standard library does this:

- [StructuralEq in core::marker - Rust](https://doc.rust-lang.org/1.61.0/core/marker/trait.StructuralEq.html)
- [StructuralPartialEq in core::marker - Rust](https://doc.rust-lang.org/1.61.0/core/marker/trait.StructuralPartialEq.html)

Such a `derived!` macro presumably would have the advantage of not letting types lie.

---

<div class="post-metadata">

**Author:** ![eminence](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eminence/32/4018_2.png) [@eminence](https://internals.rust-lang.org/u/eminence)\
**Post date:** [June 25, 2022, 2:12pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/6 "2022-06-25T14:12:07Z")

</div>

Sorry, I am still not understanding your use-case for `is_bitwise_copyable`. What's your use-case for this function? It kind of sounds like you want to know if a `Clone` impl is going to have side-effects, but I don't see how `is_bitwise_copyable` helps with that.

I suppose such a function could be useful to know if a non-`Copy` type is safe to use with `std::intrinsics::copy`.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [June 25, 2022, 5:26pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/7 "2022-06-25T17:26:06Z")

</div>

How would rustc know that a non-`Copy` type is safe to use with `memcpy`/`ptr::copy`? The literal purpose of `Copy` is to indicate "safe to use with memcpy`.

---

<div class="post-metadata">

**Author:** ![tower120](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tower120/32/9533_2.png) [@tower120](https://internals.rust-lang.org/u/tower120)\
**Post date:** [June 25, 2022, 9:05pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/8 "2022-06-25T21:05:17Z")

</div>

> [@eminence](#):
>
> Sorry, I am still not understanding your use-case for `is_bitwise_copyable`. What's your use-case for this function? It kind of sounds like you want to know if a `Clone` impl is going to have side-effects, but I don't see how `is_bitwise_copyable` helps with that.

I want to know, should I call type's clone, or copy\_nonoverlapping is enough.

Concrete example here [https://github.com/tower120/any\_vec/blob/main/src/clone\_type.rs#L22-L28](https://github.com/tower120/any_vec/blob/main/src/clone_type.rs#L22-L28) . AnyVec is type erased Vec, and I store it's clone function. As optimization I want to eliminate clone\_fn call, If type is Copy-able. The problem is that impls! is actually not able to check if type is copyable in generic context.

I assume, that in general, one ptr::copy\_nonoverlapping over large chunk of memory is faster then multiple clone calls, even when types are known compile-time. (For my untyped case this is definitely true).

---

<div class="post-metadata">

**Author:** ![tower120](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tower120/32/9533_2.png) [@tower120](https://internals.rust-lang.org/u/tower120)\
**Post date:** [June 25, 2022, 9:11pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/9 "2022-06-25T21:11:19Z")

</div>

> How would rustc know that a non-`Copy` type is safe to use with `memcpy`/`ptr::copy`?

I imagine in similar way as cpp do `std::is_trivially_copyable`, or as rust check if type Copy-compatible. Namely - empty Drop::drop, copy-compatible struct fields, etc...

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [June 25, 2022, 9:27pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/10 "2022-06-25T21:27:33Z")

</div>

> Namely - empty Drop::drop, copy-compatible struct fields, etc...

That might not be quite sufficient. For example, perhaps a type which is `Clone` assigns a new unique id to each instance at the moment of cloning, for some reason — the internal id is `Copy` but shouldn't be copied.

I'd propose it should be defined as: _may_ return `true` if the type is known to implement `Copy`, or if it has a derived `Clone` impl.

(However, this might run into the same soundness issues as specialization, and if we have specialization we don't need it.)

---

<div class="post-metadata">

**Author:** ![tower120](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tower120/32/9533_2.png) [@tower120](https://internals.rust-lang.org/u/tower120)\
**Post date:** [June 25, 2022, 9:35pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/11 "2022-06-25T21:35:56Z")

</div>

> For example, perhaps a type which is `Clone` assigns a new unique id to each instance at the moment of cloning

Can't rust check for side effects?

> I'd propose it should be defined as: _may_ return `true` if the type is known to implement `Copy`, or if it has a derived `Clone` impl.

Why _may_? There could be false negatives with detecting if type `Copy`-able? Otherwise sounds good enough to me.

> (However, this might run into the same soundness issues as specialization, ..

Wait a minute - but `need_drop` works somehow. If it possible to detect Drop implementation, whats wrong with Copy?

> ... and if we have specialization ...

We would? I lost my hope for it.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [June 25, 2022, 10:00pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/12 "2022-06-25T22:00:45Z")

</div>

> Can't rust check for side effects?

It's hard to accurately determine if a function and every function it calls has externally-observable side effects. Optimization does make use of this information, but the thing about optimization is that it is invisible to the program semantics.

> Why _may_ ? There could be false negatives with detecting if type `Copy` -able?

When I wrote that word I was thinking that it would allow the test to be possibly sound even when specialization isn't, by not guaranteeing the answer to be consistent, but I'm not sure.

> Wait a minute - but `need_drop` works somehow. If it possible to detect Drop implementation, whats wrong with Copy?

`Drop` has extra rules; for example, this program doesn't compile:

```rust
struct Foo<T>(T);
impl<T> Drop for Foo<T> where T: 'static {
    fn drop(&mut self) {}
}

```

```rust
error[E0367]: `Drop` impl requires `T: 'static` but the struct it is implemented for does not
 --> src/lib.rs:3:34
  |
3 | impl<T> Drop for Foo<T> where T: 'static {
  | ^^^^^^^
  |
note: the implementor must specify the same requirement
 --> src/lib.rs:1:1
  |
1 | struct Foo<T>(T);
  | ^^^^^^^^^^^^^^^^^

```

I'm not an expert on the issues of specialization and I might be wrong, but I think this rule exists specifically so that things like `needs_drop` can be precise: either `Foo<...>` implements `Drop` regardless of its generic parameters, or it _never_ implements `Drop` regardless of its generic parameters. But `Copy` does not have any such restriction.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [June 25, 2022, 10:05pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/13 "2022-06-25T22:05:06Z")

</div>

> [@tower120](#):
>
> Can't rust check for side effects?

Looking at the function body would make changing the implementation of `Clone` a breaking change.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [June 25, 2022, 11:17pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/14 "2022-06-25T23:17:44Z")

</div>

Not necessarily, if `is_bitwise_copyable` is documented as "callers must have the same outward behavior when this returns false" — we don't consider changes in `size_of::<Foo>()` to be a breaking change by `Foo`, even though they're observable, unless `Foo` made promises about its representation.

---

<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:** [June 26, 2022, 1:52am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/15 "2022-06-26T01:52:17Z")

</div>

> [@dlight](#):
>
> Some containers in stdlib, like `Vec`, have the "optimization" that uses `Copy` instead of `Clone` whenever applicable. This could theoretically break buggy code, but the intent of `Clone` has always been that if it breaks it's because the `Clone` impl is bad, not because `Vec` itself is bad.

This is right. You can _always_ assume it's safe to copy instead of `clone()` if a type impls `Copy`, regardless of how exactly `Clone` is implemented. Trying to exclude manual impls of `Clone`, or even impls containing side effects, would be an anti-feature. There are valid reasons to manually impl `Clone`: the main one is [`#[derive]` producing incorrect bounds](https://github.com/rust-lang/rust/issues/26925), but there are other possibilities, e.g. you could have a side effect that only takes effect (or is only important) for types that are known to not be `Copy`. Any manual impls should still be subject to any sort of library-level\* `clone()`-to-copy optimization, just as they are subject to the `clone()`-to-copy optimizations currently performed by `Vec`.

So this hypothetical function should simply be "is this type known to be `Copy`". I am not sure whether it's possible to implement that soundly under the current rustc.

\* I do remember some debate about whether or not converting clones to copies is a valid optimization at a _compiler_ level, but that's different.

---

<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:** [June 26, 2022, 4:36am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/16 "2022-06-26T04:36:23Z")

</div>

If this is added, it should be `needs_clone`, like `needs_drop`.

| | `drop_in_place` | `clone` |
| --- | --- | --- |
| trivial (trait) | `T: Copy` | `T: Copy` |
| trivial (const) | `!needs_drop::<T>()` | `!needs_clone::<T>()` |
| possible | `T: ?Sized` | `T: Clone` |

> [@tower120](#):
>
> Wait a minute - but `need_drop` works somehow. If it possible to detect Drop implementation, whats wrong with Copy?

`needs_drop` is an optimization hint only, and is allowed to spuriously return the safe default `true`. Similarly, `needs_clone` could spuriously return `true`, and `is_trivially_copiable = !needs_clone` may spuriously return `false`.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [June 27, 2022, 9:45am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/17 "2022-06-27T09:45:29Z")

</div>

`needs_drop` exists, because `Drop` is unusable as a bound. A struct may contain fields with `Drop`, but _not_ implement `Drop` itself.

With copying this is not an issue. A copyable field doesn't make its struct `Copy`. The `Copy` trait _has_ to be explicitly implemented for the outer type, and can't be inferred from the structure. Otherwise it could undermine guarantees of the newtype pattern.

So I think `Copy` optimizations are best left to compiler smarts and specialization.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [June 27, 2022, 11:43am UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/18 "2022-06-27T11:43:08Z")

</div>

`Copy` can be implemented for `Foo<'static>` but not for other `Foo<'a>`. As such specialization on `Copy` can't be done in a sound way.

---

<div class="post-metadata">

**Author:** ![the8472](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@the8472](https://internals.rust-lang.org/u/the8472)\
**Post date:** [June 27, 2022, 1:52pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/19 "2022-06-27T13:52:49Z")

</div>

And yet...

> <https://github.com/rust-lang/rust/blob/bd2e51a338e36372b200a22f3e0e22189a1063e6/library/core/src/marker.rs#L374-L384>

So please don't `impl Copy for Foo<'static>` until specialization gets fixed.

---

<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:** [June 27, 2022, 7:43pm UTC](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888/20 "2022-06-27T19:43:58Z")

</div>

> [@the8472](#):
>
> And yet...

Here's an interesting note, though: the unsoundness of a `Copy` specialization is that it behaves as a superset of a sound `T: Copy`, but it's still a subset of "all fields are bitwise copyable" because even a lifetime conditional `Copy` impl requires all fields are `Copy` (potentially conditional on the lifetime). While it's still possible to exploit this unsoundness by externally relying on the side effects of `Clone` happening it _seems_ impossible to exploit internally to `T` without itself specializing on a lifetime (...potentially hidden behind `Self: Copy`).

It's also an interesting observation that (in the `Unpin` case) the root cause of some `T` not being bitwise cloneäble is necessarily not that its fields aren't — all primitive types are — but that it adds some nontrivial logic on top of those primitive fields.

[Next page](https://internals.rust-lang.org/t/feature-request-is-bitwise-copyable-t/16888.md?page=2)
