google / google/zerocopy

Allow deriving all traits using a single derive

Open
#712 1 comment 0 reactions 0 assignees View on GitHub
compatibility-nonbreaking
Dominant language
Rust
Stars
2.6k
Forks
179
Avg merge
1d 19h
Merged PRs (30d)
29

Description

I recently spent multiple hours [upgrading Fuchsia's vendored zerocopy to 0.8.0-alpha.1](https://fuchsia-review.googlesource.com/c/fuchsia/+/958312). The vast majority of this effort was spent updating `#[derive(...)]` annotations to also derive our new `NoCell` trait.

The vast majority of types I encountered could soundly implement all of zerocopy's current traits, and could also soundly implement future planned traits such as `TryFromBytes`. For these types, if there had been an "alias" that represented "all of zerocopy's traits", it would have saved me a huge amount of time. For the small subset of types which could not support this, still listing traits manually would be trivial.

I'm thinking something like:

```rust
#[deive(zerocopy::AllTraits)]
#[repr(C)]
struct Foo { ... }
```

The main drawback of this approach is that it's only forwards-compatible with adding new traits when those traits are guaranteed to be derivable on all types for which the old set of traits was derivable. We may end up in a situation where, within a given version train, we have to add a trait that one would reasonably expect to be implied by `AllTraits`, but cannot be until the next version train. That might not be the worst thing, since the main benefit of `AllTraits` is in making semver-breaking upgrades lower-friction. It would mean that, when performing a semver-breaking upgrade, derives would *mostly* continue to just work with the exception of newly-added traits that aren't backwards-compatible.

Another question to consider is whether error messages will be easy to understand. It may be the case that it would be unclear which trait is generating a particular error message. For situations like `derive(FromBytes)` implying `derive(FromZeros)`, this is less of an issue since `FromBytes: FromZeros` means that you *must* derive `FromZeros` in order to derive `FromBytes`, so it doesn't really matter whether the error technically comes from the derive of `FromZeros` or from the derive of `FromBytes`. By contrast, a user encountering an error from `AllTraits` will likely want to understand which trait is causing the problem, and then replace the derive with a derive of all but the offending trait.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.