add static assertion
This commit is contained in:
parent
b284e2ebab
commit
62825d5d28
|
@ -11,5 +11,4 @@ path = "src/lib.rs"
|
|||
|
||||
[dependencies]
|
||||
serialport = "4.2.0"
|
||||
rand = "0.8.5"
|
||||
|
||||
rand = "0.8.5"
|
|
@ -16,6 +16,6 @@ fn main() {
|
|||
direction *= -1;
|
||||
}
|
||||
|
||||
sleep(Duration::from_millis(50));
|
||||
sleep(Duration::from_millis(40));
|
||||
}
|
||||
}
|
9
src/assert.rs
Normal file
9
src/assert.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
struct AssertInRange<const VALUE: usize, const MIN: usize, const MAX: usize>;
|
||||
|
||||
impl<const VALUE: usize, const MIN: usize, const MAX: usize> AssertInRange<VALUE, MIN, MAX> {
|
||||
const OK: () = assert!(MIN <= VALUE && VALUE <= MAX);
|
||||
}
|
||||
|
||||
pub fn assert_in_range<const VALUE: usize, const MIN: usize, const MAX: usize> () {
|
||||
let () = AssertInRange::<VALUE, MIN, MAX>::OK;
|
||||
}
|
11
src/b15f.rs
11
src/b15f.rs
|
@ -5,9 +5,8 @@
|
|||
use std::{process::Command, time::Duration, fmt::Debug, thread::sleep};
|
||||
use rand::Rng;
|
||||
use serialport::SerialPort;
|
||||
use crate::error::Error;
|
||||
|
||||
use crate::{request::Request, build_request};
|
||||
use crate::{assert::assert_in_range, error::Error, request::Request, build_request};
|
||||
|
||||
macro_rules! log {
|
||||
($text: literal, $($arg:tt)*) => (println!(concat!("[B15F] ", $text), $($arg)*));
|
||||
|
@ -125,11 +124,11 @@ impl B15F {
|
|||
///
|
||||
/// # Examples
|
||||
///
|
||||
pub fn digital_write<const port: u8> (&mut self, value: u8) -> Result<(), Error> {
|
||||
assert!(port == 0 || port == 1);
|
||||
pub fn digital_write<const PORT: usize> (&mut self, value: u8) -> Result<(), Error> {
|
||||
assert_in_range::<PORT, 0, 1>();
|
||||
|
||||
let reversed = value.reverse_bits();
|
||||
let request = if port == 0 { Request::DigitalWrite0 } else { Request::DigitalWrite1 };
|
||||
let request = if PORT == 0 { Request::DigitalWrite0 } else { Request::DigitalWrite1 };
|
||||
|
||||
self.usart.write(build_request![request, reversed])?;
|
||||
|
||||
|
@ -137,7 +136,7 @@ impl B15F {
|
|||
self.usart.read(&mut aw)?;
|
||||
|
||||
if aw[0] != B15F::MSG_OK {
|
||||
return Err(format!("Setting Port {} failed", port).into());
|
||||
return Err(format!("Setting Port {} failed", PORT).into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -16,5 +16,6 @@
|
|||
pub mod b15f;
|
||||
mod request;
|
||||
mod error;
|
||||
mod assert;
|
||||
|
||||
pub use crate::b15f::B15F;
|
Loading…
Reference in a new issue