SoftbearStudios / bitcode

A binary encoder/decoder for Rust
https://crates.io/crates/bitcode/
MIT License
369 stars 19 forks source link

feat: add `#[bitcode(crate = "...")]` for crate alias #28

Closed matteopolak closed 3 months ago

matteopolak commented 3 months ago

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.

bitcode_renamed = { package = "bitcode", version = "0.6" }
#[derive(bitcode_renamed::Encode)]
#[bitcode(crate = "bitcode_renamed")]
pub struct MyData {
  // ...
}

It also works with paths, e.g.

#[bitcode(crate = "some::other::location")]
finnbear commented 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;
matteopolak commented 3 months ago

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

finnbear commented 3 months ago

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).

matteopolak commented 3 months ago

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,
}
finnbear commented 3 months ago

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 :)

matteopolak commented 3 months ago

Looks like a new version of bitcode_derive wasn't published, may need to bump them both to 0.6.2

finnbear commented 3 months ago

Oops, thanks, fixed!

matteopolak commented 3 months ago

Looks good now, thanks! And thanks for your hard work on this crate, I really appreciate it :)

matteopolak commented 3 months ago

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!

finnbear commented 3 months ago

The fix

No problem, done in 0.6.3. Let me know if it works!

matteopolak commented 3 months ago

Thanks! It's all working now :)