Skip to content

Commit

Permalink
Avoids skipping getter in MessageSessionId when testing.
Browse files Browse the repository at this point in the history
Recently, the getters for the priority field in
`uavcan::session_id::message::MessageSessionId` were disabled from being
generated to avoid an unused warning, as they are, indeed, unused in the build
codebase.

Nonetheless, the getter for `priority` is actually used by one of the tests, such
that is needed, but only when testing.

The general way to do this, would be to add a `#[cfg_attr(...)]` for the `skip`
attribute to avoid generating it during testing.

Unfortunately, `cfg*` attributes are not expanded before `proc_macro_attributes`
expansion and the `modular-bitfield` does not manage them manually.

This is a general problem in the proc-macro environment, with an example being
taiki-e/pin-project#68.

The rust team is working on a solution to this, see, for example:

https://doc.rust-lang.org/beta/unstable-book/library-features/cfg-eval.html
https://doc.rust-lang.org/beta/unstable-book/language-features/macro-attributes-in-derive-output.html

And the related issues.

In particular, `cfg_eval` provides a way to ensure that the `cfg` attributes are
evaluated before the expansion of the following macros.

For this reason, the `cfg_eval` was activated, a `#[cfg_eval]` attribute was
added to `MessageSessionId` and a `cfg_attr` conditioned on `test` was added to
the priority field of the same structure.

As this is not yet stabilized, the crate now depends on nightly, and cannot be
compiled in stable.

It was possible to provide workarounds to avoid moving outside stable.
For example, by duplicating `MessageSessionsId` and providing a version with the
`skip` attribute and one without, conditioning the whole structure, instead of a
field attribute, on `test`.

Nonetheless, being nightly only is not considered a problem for this crate
usage and the "forward-correct" solution was thus preferred.
  • Loading branch information
diseraluca committed May 31, 2021
1 parent 011b77a commit 07a8431
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
#![feature(cfg_eval)]

pub mod rx;
pub mod session_id;
Expand Down
3 changes: 2 additions & 1 deletion src/session_id/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{node_id::NodeId, subject_id::SubjectId, transfer_priority::TransferPriority};
use modular_bitfield::prelude::*;

#[cfg_eval]
#[bitfield]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand All @@ -20,7 +21,7 @@ pub struct MessageSessionId {
is_anonymous: bool,
pub is_service: bool,
#[bits = 3]
#[skip(getters)]
#[cfg_attr(not(test), skip(getters))]
priority: TransferPriority,
#[skip]
__: B3,
Expand Down

0 comments on commit 07a8431

Please sign in to comment.