lpc8xx_hal/swm/
assignment.rs

1use crate::pins::{self, Pin};
2
3use super::{
4    function_kind::{Analog, Input, Output},
5    FunctionTrait,
6};
7
8/// Internal trait used to assign functions to pins
9///
10/// This trait is an internal implementation detail and should neither be
11/// implemented nor used outside of LPC8xx HAL. Any changes to this trait won't
12/// be considered breaking changes.
13///
14/// Please refer to [`Function::assign`] for the public API that uses this
15/// trait.
16///
17/// [`Function::assign`]: crate::swm::Function::assign
18pub trait AssignFunction<Function, Kind> {
19    /// The type of the pin after the function has been assigned
20    type Assigned;
21
22    /// Internal method for assigning a function to a pin
23    fn assign(self) -> Self::Assigned;
24}
25
26/// Internal trait used to unassign functions from pins
27///
28/// This trait is an internal implementation detail and should neither be
29/// implemented nor used outside of LPC8xx HAL. Any changes to this trait won't
30/// be considered breaking changes.
31///
32/// Please refer to [`Function::unassign`] for the public API that uses this
33/// trait.
34///
35/// [`Function::unassign`]: crate::swm::Function::unassign
36pub trait UnassignFunction<Function, Kind> {
37    /// The type of the pin after the function has been unassigned
38    type Unassigned;
39
40    /// Internal method for unassigning a function from a pin
41    fn unassign(self) -> Self::Unassigned;
42}
43
44impl<T, F, O, Is> AssignFunction<F, Input> for Pin<T, pins::state::Swm<O, Is>>
45where
46    T: pins::Trait,
47    F: FunctionTrait<T, Kind = Input>,
48{
49    type Assigned = Pin<T, pins::state::Swm<O, (Is,)>>;
50
51    fn assign(self) -> Self::Assigned {
52        Pin {
53            ty: self.ty,
54            _state: pins::state::Swm::new(),
55        }
56    }
57}
58
59impl<T, F, Is> AssignFunction<F, Output> for Pin<T, pins::state::Swm<(), Is>>
60where
61    T: pins::Trait,
62    F: FunctionTrait<T, Kind = Output>,
63{
64    type Assigned = Pin<T, pins::state::Swm<((),), Is>>;
65
66    fn assign(self) -> Self::Assigned {
67        Pin {
68            ty: self.ty,
69            _state: pins::state::Swm::new(),
70        }
71    }
72}
73
74impl<T, F, O, Is> UnassignFunction<F, Input>
75    for Pin<T, pins::state::Swm<O, (Is,)>>
76where
77    T: pins::Trait,
78    F: FunctionTrait<T, Kind = Input>,
79{
80    type Unassigned = Pin<T, pins::state::Swm<O, Is>>;
81
82    fn unassign(self) -> Self::Unassigned {
83        Pin {
84            ty: self.ty,
85            _state: pins::state::Swm::new(),
86        }
87    }
88}
89
90impl<T, F, Is> UnassignFunction<F, Output>
91    for Pin<T, pins::state::Swm<((),), Is>>
92where
93    T: pins::Trait,
94    F: FunctionTrait<T, Kind = Output>,
95{
96    type Unassigned = Pin<T, pins::state::Swm<(), Is>>;
97
98    fn unassign(self) -> Self::Unassigned {
99        Pin {
100            ty: self.ty,
101            _state: pins::state::Swm::new(),
102        }
103    }
104}
105
106impl<T, F> AssignFunction<F, Analog> for Pin<T, pins::state::Swm<(), ()>>
107where
108    T: pins::Trait,
109    F: FunctionTrait<T, Kind = Analog>,
110{
111    type Assigned = Pin<T, pins::state::Analog>;
112
113    fn assign(self) -> Self::Assigned {
114        Pin {
115            ty: self.ty,
116            _state: pins::state::Analog,
117        }
118    }
119}