Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow renaming of defmt in Format derive #928

Open
jamesmunns opened this issue Jan 30, 2025 · 3 comments
Open

Allow renaming of defmt in Format derive #928

jamesmunns opened this issue Jan 30, 2025 · 3 comments

Comments

@jamesmunns
Copy link

In the postcard ecosystem, I've started gating all deps behind versioned features, e.g. defmt-v0_3 instead of defmt. I typically rename the package to allow multiple incompatible versions to exist.

I noticed that the Format macro does not like this:

#[cfg_attr(feature = "defmt-v0_3", derive(defmt_v0_3::Format))]
pub struct Key([u8; 8]);

leads to a compilation error:

error[E0433]: failed to resolve: use of undeclared crate or module `defmt`
  --> source/postcard-schema/src/key/mod.rs:26:43
   |
26 | #[cfg_attr(feature = "defmt-v0_3", derive(defmt_v0_3::Format))]
   |                                           ^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `defmt`
   |
   = note: this error originates in the derive macro `defmt_v0_3::Format` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0433`.

Serde allows for something like:

#[derive(serde_other::Serialize, serde_other::Deserialize)]
#[serde(crate = "serde_other")]
struct Example { }

This might be nice to have for defmt as well.

@jonathanpallant
Copy link
Contributor

Oof. The expansion of the macro is relying on defmt::_stuff() working.

I guess we need that extra attribute to rename what it expects.

@jonathanpallant
Copy link
Contributor

Although it should be noted that we're going to a lot of trouble to make sure this kind of import naming will never be required for defmt, because 0.3 will have a semver-trick crate to import-export defmt-1.0.

@jamesmunns
Copy link
Author

Yep, can't fault you if you decide not to prioritize this for that reason. I was looking to add defmt 0.3 today, and 1.0 later.

Serde's macro expansion has one level of indirection, instead of serde:: it does #serde::: https://github.com/serde-rs/serde/blob/04ff3e8f950eb9862759781d85ec29d906917b70/serde_derive/src/ser.rs#L26-L55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants