google / zerocopy

https://discord.gg/MAvWH2R6zk
Apache License 2.0
1.13k stars 86 forks source link

The derive feature can cause duplicate import errors #1587

Open felinira opened 1 month ago

felinira commented 1 month ago

The docs state the following:

However, you may experience better compile times if you instead directly depend on both zerocopy and zerocopy-derive in your Cargo.toml, since doing so will allow Rust to compile these crates in parallel. To do so, do not enable the derive feature, and list both dependencies in your Cargo.toml with the same leading non-zero version number; e.g:

However, this can lead to issues if another dependency in the chain imports zerocopy with the derive feature, as now you suddenly have two macros in scope.

error[E0252]: the name `AsBytes` is defined multiple times
 --> /run/build/abc/cargo/vendor/gvdb-0.7.0/src/read/hash.rs:9:23
  |
8 | use zerocopy::{AsBytes, FromBytes};
  |                ------- previous import of the macro `AsBytes` here
9 | use zerocopy_derive::{AsBytes, FromBytes, FromZeroes};
  |                       ^^^^^^^--
  |                       |
  |                       `AsBytes` reimported here
  |                       help: remove unnecessary import
  |
  = note: `AsBytes` must be defined only once in the macro namespace of this module

which is then only solveable within the dependency that imports zerocopy_derive in this way.

Therefore this usage should probably be discouraged, or warned against for anything other than binary crates.

jswrenn commented 1 month ago

Perhaps we should recommend:

- use zerocopy_derive::{AsBytes, FromBytes, FromZeroes};
+ use zerocopy_derive::*;

...so the imports shadow without conflict.