Skip to content

Commit

Permalink
Avoid using PhantomData<()> to make constructor private
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Nov 17, 2024
1 parent 6af8dcc commit b524806
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 74 deletions.
19 changes: 9 additions & 10 deletions src/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ impl EnabledState for Locked {}

macro_rules! impl_comp {
($($t:ident: $reg:ident,)+) => {$(
pub struct $t {
_rb: PhantomData<()>,
}
#[non_exhaustive]
pub struct $t;

impl $t {
pub fn csr(&self) -> &$crate::stm32::comp::CCSR {
Expand Down Expand Up @@ -548,31 +547,31 @@ pub fn split(_comp: COMP, rcc: &mut Rcc) -> Comparators {
rcc.rb.apb2rstr().modify(|_, w| w.syscfgrst().clear_bit());

(
COMP1 { _rb: PhantomData },
COMP2 { _rb: PhantomData },
COMP3 { _rb: PhantomData },
COMP4 { _rb: PhantomData },
COMP1,
COMP2,
COMP3,
COMP4,
#[cfg(any(
feature = "stm32g473",
feature = "stm32g483",
feature = "stm32g474",
feature = "stm32g484"
))]
COMP5 { _rb: PhantomData },
COMP5,
#[cfg(any(
feature = "stm32g473",
feature = "stm32g483",
feature = "stm32g474",
feature = "stm32g484"
))]
COMP6 { _rb: PhantomData },
COMP6,
#[cfg(any(
feature = "stm32g473",
feature = "stm32g483",
feature = "stm32g474",
feature = "stm32g484"
))]
COMP7 { _rb: PhantomData },
COMP7,
)
}

Expand Down
66 changes: 29 additions & 37 deletions src/hrtim/control.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::marker::PhantomData;

use crate::{
hrtim::fault::{
FltMonitor1, FltMonitor2, FltMonitor3, FltMonitor4, FltMonitor5, FltMonitor6, FltMonitorSys,
Expand Down Expand Up @@ -140,7 +138,7 @@ impl HrTimOngoingCalibration {
unsafe { self.init() };

(
HrTimCalibrated { _x: PhantomData },
HrTimCalibrated,
unsafe { FaultInputs::new() },
unsafe { EevInputs::new() },
)
Expand Down Expand Up @@ -180,33 +178,31 @@ impl HrTimOngoingCalibration {
/// This object may be used for things that needs to be done before any timers have been started but after the calibration has been completed. Its existence is proof that no timers have started.
///
/// Once done with setup, use the `constrain` to get a `HrPwmControl` which can be used to start the timers.
pub struct HrTimCalibrated {
_x: PhantomData<()>,
}
#[non_exhaustive]
pub struct HrTimCalibrated;

impl HrTimCalibrated {
pub fn constrain(self) -> HrPwmControl {
HrPwmControl {
_x: PhantomData,
control: HrPwmCtrl { _x: PhantomData },
fault_sys: FltMonitorSys { _x: PhantomData },
fault_1: FltMonitor1 { _x: PhantomData },
fault_2: FltMonitor2 { _x: PhantomData },
fault_3: FltMonitor3 { _x: PhantomData },
fault_4: FltMonitor4 { _x: PhantomData },
fault_5: FltMonitor5 { _x: PhantomData },
fault_6: FltMonitor6 { _x: PhantomData },

adc_trigger1: Adc1Trigger { _x: PhantomData },
adc_trigger2: Adc2Trigger { _x: PhantomData },
adc_trigger3: Adc3Trigger { _x: PhantomData },
adc_trigger4: Adc4Trigger { _x: PhantomData },
adc_trigger5: Adc5Trigger { _x: PhantomData },
adc_trigger6: Adc6Trigger { _x: PhantomData },
adc_trigger7: Adc7Trigger { _x: PhantomData },
adc_trigger8: Adc8Trigger { _x: PhantomData },
adc_trigger9: Adc9Trigger { _x: PhantomData },
adc_trigger10: Adc10Trigger { _x: PhantomData },
control: HrPwmCtrl,
fault_sys: FltMonitorSys,
fault_1: FltMonitor1,
fault_2: FltMonitor2,
fault_3: FltMonitor3,
fault_4: FltMonitor4,
fault_5: FltMonitor5,
fault_6: FltMonitor6,

adc_trigger1: Adc1Trigger,
adc_trigger2: Adc2Trigger,
adc_trigger3: Adc3Trigger,
adc_trigger4: Adc4Trigger,
adc_trigger5: Adc5Trigger,
adc_trigger6: Adc6Trigger,
adc_trigger7: Adc7Trigger,
adc_trigger8: Adc8Trigger,
adc_trigger9: Adc9Trigger,
adc_trigger10: Adc10Trigger,
}
}
}
Expand All @@ -220,14 +216,12 @@ impl<'a> From<&'a mut HrPwmControl> for &'a mut HrPwmCtrl {
/// Used as a token to guarantee unique access to resources common to multiple timers
///
/// An instance of this object can be obtained from [`HrPwmControl`].control
pub struct HrPwmCtrl {
_x: PhantomData<()>,
}
#[non_exhaustive]
pub struct HrPwmCtrl;

/// Used as a token to guarantee unique access to resources common to multiple timers
#[non_exhaustive]
pub struct HrPwmControl {
_x: PhantomData<()>,

pub control: HrPwmCtrl,

pub fault_sys: FltMonitorSys,
Expand All @@ -253,9 +247,8 @@ pub struct HrPwmControl {

macro_rules! impl_adc1234_trigger {
($($t:ident: [$trait_:ident, $adcXr:ident, $variant345:ident $(, $variant12:ident)*]),*) => {$(
pub struct $t {
_x: PhantomData<()>,
}
#[non_exhaustive]
pub struct $t;

impl $t {
pub fn enable_source<T: $trait_>(&mut self, _trigger: &T) {
Expand All @@ -282,9 +275,8 @@ macro_rules! impl_adc1234_trigger {

macro_rules! impl_adc5678910_trigger {
($($t:ident: [$trait_:ident, $adcXtrg:ident, $variant345:ident, $variant12:ident]),*) => {$(
pub struct $t {
_x: PhantomData<()>,
}
#[non_exhaustive]
pub struct $t;

impl $t {
pub fn enable_source<T: $trait_>(&mut self, _trigger: &T) {
Expand Down
11 changes: 4 additions & 7 deletions src/hrtim/external_event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::marker::PhantomData;

use crate::comparator::{COMP1, COMP2, COMP3, COMP4, COMP5, COMP6, COMP7};
use crate::gpio::gpiob::{PB3, PB4, PB5, PB6, PB7, PB8, PB9};
use crate::gpio::gpioc::{PC11, PC12, PC5, PC6};
Expand All @@ -9,10 +7,9 @@ use crate::stm32::HRTIM_COMMON;

use super::control::HrTimCalibrated;

#[non_exhaustive]
#[derive(Copy, Clone, PartialEq)]
pub struct ExternalEventSource<const N: u8, const IS_FAST: bool> {
_x: PhantomData<()>,
}
pub struct ExternalEventSource<const N: u8, const IS_FAST: bool>;

pub struct EevInputs {
pub eev_input1: EevInput<1>,
Expand Down Expand Up @@ -292,7 +289,7 @@ macro_rules! impl_eev1_5_to_es {
});
}

ExternalEventSource { _x: PhantomData }
ExternalEventSource
}
}

Expand Down Expand Up @@ -334,7 +331,7 @@ macro_rules! impl_eev6_10_to_es {
common.eecr3().modify(|_r, w| w.$eeXf().bits(filter_bits));
}

ExternalEventSource { _x: PhantomData }
ExternalEventSource
}
}

Expand Down
33 changes: 13 additions & 20 deletions src/hrtim/fault.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::marker::PhantomData;

use crate::comparator::{COMP1, COMP2, COMP3, COMP4, COMP5, COMP6};
use crate::gpio::gpioa::{PA12, PA15};
use crate::gpio::gpiob::{PB0, PB10, PB11};
Expand Down Expand Up @@ -75,14 +73,12 @@ macro_rules! impl_faults {
)+) => {$(

// This should NOT be Copy/Clone
pub struct $input {
pub(crate) _x: PhantomData<()>
}
#[non_exhaustive]
pub struct $input;

#[non_exhaustive]
#[derive(Copy, Clone)]
pub struct $source {
_x: PhantomData<()>
}
pub struct $source;

impl $input {
pub fn bind_pin<IM>(self, pin: $pin<gpio::Input<IM>>) -> SourceBuilder<$input> {
Expand Down Expand Up @@ -127,9 +123,7 @@ macro_rules! impl_faults {
common.$fltinrZ().modify(|_r, w| w.$fltWlck().set_bit());
}

$source {
_x: PhantomData
}
$source
}

pub fn polarity(mut self, polarity: super::Polarity) -> Self {
Expand Down Expand Up @@ -173,12 +167,12 @@ pub struct FaultInputs {
impl FaultInputs {
pub(crate) unsafe fn new() -> Self {
FaultInputs {
fault_input1: FaultInput1 { _x: PhantomData },
fault_input2: FaultInput2 { _x: PhantomData },
fault_input3: FaultInput3 { _x: PhantomData },
fault_input4: FaultInput4 { _x: PhantomData },
fault_input5: FaultInput5 { _x: PhantomData },
fault_input6: FaultInput6 { _x: PhantomData },
fault_input1: FaultInput1,
fault_input2: FaultInput2,
fault_input3: FaultInput3,
fault_input4: FaultInput4,
fault_input5: FaultInput5,
fault_input6: FaultInput6,
}
}
}
Expand Down Expand Up @@ -243,9 +237,8 @@ pub enum FaultSamplingFilter {

macro_rules! impl_flt_monitor {
($($t:ident: ($fltx:ident, $fltxc:ident, $fltxie:ident),)+) => {$(
pub struct $t {
pub(crate) _x: PhantomData<()>
}
#[non_exhaustive]
pub struct $t;

impl FaultMonitor for $t {
fn enable_interrupt(&mut self, _hr_control: &mut HrPwmCtrl) {
Expand Down

0 comments on commit b524806

Please sign in to comment.