Closed matteopolak closed 3 months ago
Currently it's not possible to package up the bitcode crate's derives without requiring users to include the dependency locally
Have you considered writing a prelude?
[package]
name = "library"
version = "0.1.0"
edition = "2021"
[dependencies]
bitcode = { version = "0.6.0", features = [ "derive" ] }
pub mod prelude {
pub use bitcode::{self, Encode, Decode, DecodeOwned};
}
Dependents can then do this without bitcode
in their Cargo.toml
:
use library::prelude::*;
#[derive(Encode)]
struct Foo;
Yes, but it's hidden behind a macro (the user doesn't derive anything themselves), so having them include the entire prelude would be prone to error.
It's a proc_macro_attribute that adds derives from a bunch of crates based on the feature set (serde, bincode, bitcode, validator, schemars) to avoid the user needing to add 6 derives to everything (there's more to it, but good enough)
Currently, everything except bitcode and validator have a crate = "..."
attribute so it'd be great if that was something you guys supported too
Thanks for the PR the additional context! I'm personally in favor of this, as long as it doesn't break existing code (see comment).
Awesome, glad to hear! I changed the behaviour so the default will now work with the following (same behaviour as it used to be):
use bitcode_rename as bitcode;
#[derive(bitcode::Encode)]
struct Data {
name: String,
}
Great contribution, thanks again! Released under bitcode@0.6.1
, which I've confirmed works in a codebase that used the prelude method.
Haven't tried the new feature; let me know if it works for you :)
Looks like a new version of bitcode_derive
wasn't published, may need to bump them both to 0.6.2
Oops, thanks, fixed!
Looks good now, thanks! And thanks for your hard work on this crate, I really appreciate it :)
Hey I think putting the ::
prefix with a user-given crate path was a mistake on my end. Hadn't realized this, but ::crate
, ::super
, and ::self
are not valid (https://github.com/rust-lang/rust/issues/45477 must have been reverted or I read it incorrectly). One option would be checking if the path starts with one of those keywords, but it's probably better to just paste in exactly what the user gives instead. The fix would be commenting out this line, probably too small for a PR but I can create one if needed
Sorry about that!
The fix
No problem, done in 0.6.3
. Let me know if it works!
Thanks! It's all working now :)
Currently it's not possible to package up the
bitcode
crate's derives without requiring users to include the dependency locally (cluttering up their deps list if they don't actually use it).This PR adds an optional crate attribute that overrides all references to the crate with a new name.
It also works with paths, e.g.