Skip to content

Commit 33852d8

Browse files
committed
const assert hack
1 parent b0b3a1b commit 33852d8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/gpio.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ impl<MODE, const P: u8, const N: u8> PinExt for PX<MODE, P, N> {
329329
impl<MODE, const P: u8, const N: u8> PX<MODE, P, N> {
330330
/// Configures the pin to operate alternate mode
331331
pub fn into_alternate<const A: u8>(self) -> PX<Alternate<AF<A>>, P, N> {
332-
assert!(A < 16);
332+
less_than_16::<A>();
333333
_set_alternate_mode::<P, N, A>();
334334
PX::new()
335335
}
336336

337337
/// Configures the pin to operate in alternate open drain mode
338338
pub fn into_alternate_open_drain<const A: u8>(self) -> PX<AlternateOD<AF<A>>, P, N> {
339-
assert!(A < 16);
339+
less_than_16::<A>();
340340
_set_alternate_mode::<P, N, A>();
341341
PX::new().set_open_drain()
342342
}
@@ -1209,3 +1209,15 @@ impl<const P: u8> Gpio<P> {
12091209
}
12101210
}
12111211
}
1212+
1213+
#[allow(path_statements)]
1214+
const fn less_than_16<const A: u8>() {
1215+
Assert::<A, 16>::LESS;
1216+
}
1217+
1218+
/// Const assert hack
1219+
struct Assert<const L: u8, const R: u8>;
1220+
1221+
impl<const L: u8, const R: u8> Assert<L, R> {
1222+
pub const LESS: u8 = R - L - 1;
1223+
}

0 commit comments

Comments
 (0)