diff --git a/CHANGELOG.md b/CHANGELOG.md index d5040881..e877202d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- Add `--skip-peripherals-struct` flag to skip generating the `Peripherals` + struct, its `take`/`steal` impl and the `DEVICE_PERIPHERALS` static + ## [v0.37.1] - 2025-10-17 - Change `feature(doc_auto_cfg)` to `feature(doc_cfg)` to allow nightly docs to build. diff --git a/src/config.rs b/src/config.rs index e7b224fc..81d331be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,6 +21,7 @@ pub struct Config { pub generic_mod: bool, pub make_mod: bool, pub skip_crate_attributes: bool, + pub skip_peripherals_struct: bool, pub ignore_groups: bool, pub keep_list: bool, pub strict: bool, diff --git a/src/generate/device.rs b/src/generate/device.rs index ddd57359..c16a8eea 100644 --- a/src/generate/device.rs +++ b/src/generate/device.rs @@ -295,64 +295,66 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result= RustEdition::E2024 { - quote!(#[unsafe(no_mangle)]) - } else { - quote!(#[no_mangle]) - }; - - let set_device_peripherals_true = if config.edition >= RustEdition::E2024 { - quote!(unsafe { DEVICE_PERIPHERALS = true }) - } else { - quote!(DEVICE_PERIPHERALS = true;) - }; - - out.extend(quote! { - // NOTE `no_mangle` is used here to prevent linking different minor versions of the device - // crate as that would let you `take` the device peripherals more than once (one per minor - // version) - #nomangle - static mut DEVICE_PERIPHERALS: bool = false; - - /// All the peripherals. - #[allow(non_snake_case)] - pub struct Peripherals { - #fields - } + if !config.skip_peripherals_struct { + let nomangle = if config.edition >= RustEdition::E2024 { + quote!(#[unsafe(no_mangle)]) + } else { + quote!(#[no_mangle]) + }; - impl Peripherals { - /// Returns all the peripherals *once*. - #[cfg(feature = "critical-section")] - #[inline] - pub fn take() -> Option { - critical_section::with(|_| { - // SAFETY: We are in a critical section, so we have exclusive access - // to `DEVICE_PERIPHERALS`. - if unsafe { DEVICE_PERIPHERALS } { - return None - } + let set_device_peripherals_true = if config.edition >= RustEdition::E2024 { + quote!(unsafe { DEVICE_PERIPHERALS = true }) + } else { + quote!(DEVICE_PERIPHERALS = true;) + }; - // SAFETY: `DEVICE_PERIPHERALS` is set to `true` by `Peripherals::steal`, - // ensuring the peripherals can only be returned once. - Some(unsafe { Peripherals::steal() }) - }) + out.extend(quote! { + // NOTE `no_mangle` is used here to prevent linking different minor versions of the device + // crate as that would let you `take` the device peripherals more than once (one per minor + // version) + #nomangle + static mut DEVICE_PERIPHERALS: bool = false; + + /// All the peripherals. + #[allow(non_snake_case)] + pub struct Peripherals { + #fields } - /// Unchecked version of `Peripherals::take`. - /// - /// # Safety - /// - /// Each of the returned peripherals must be used at most once. - #[inline] - pub unsafe fn steal() -> Self { - #set_device_peripherals_true - - Peripherals { - #exprs + impl Peripherals { + /// Returns all the peripherals *once*. + #[cfg(feature = "critical-section")] + #[inline] + pub fn take() -> Option { + critical_section::with(|_| { + // SAFETY: We are in a critical section, so we have exclusive access + // to `DEVICE_PERIPHERALS`. + if unsafe { DEVICE_PERIPHERALS } { + return None + } + + // SAFETY: `DEVICE_PERIPHERALS` is set to `true` by `Peripherals::steal`, + // ensuring the peripherals can only be returned once. + Some(unsafe { Peripherals::steal() }) + }) + } + + /// Unchecked version of `Peripherals::take`. + /// + /// # Safety + /// + /// Each of the returned peripherals must be used at most once. + #[inline] + pub unsafe fn steal() -> Self { + #set_device_peripherals_true + + Peripherals { + #exprs + } } } - } - }); + }); + } Ok(out) } diff --git a/src/main.rs b/src/main.rs index 65a9fe2b..07de10cb 100755 --- a/src/main.rs +++ b/src/main.rs @@ -245,6 +245,13 @@ Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake` .action(ArgAction::SetTrue) .help("Do not generate crate attributes"), ) + .arg( + Arg::new("skip_peripherals_struct") + .long("skip-peripherals-struct") + .alias("skip_peripherals_struct") + .action(ArgAction::SetTrue) + .help("Do not generate the `Peripherals` struct, its `take`/`steal` impl, and the `DEVICE_PERIPHERALS` static"), + ) .arg( Arg::new("strict") .long("strict")