Work around Option::take with static muts no longer working on new toolchain
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 68c463d..1caeca5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md
@@ -45,6 +45,7 @@ [ OK ] Dynamic dispatch [ OK ] Formatting [ OK ] Heap +[ OK ] Drivers only instantiable once [ OK ] Callbacks [ OK ] GPIO initialization [ OK ] GPIO activation
diff --git a/examples-alloc/libtock_test.rs b/examples-alloc/libtock_test.rs index d764af3..26cccb4 100644 --- a/examples-alloc/libtock_test.rs +++ b/examples-alloc/libtock_test.rs
@@ -46,6 +46,7 @@ test.dynamic_dispatch()?; test.formatting()?; test.heap()?; + test.drivers_only_instantiable_once()?; test.callbacks(timer).await?; test.gpio(gpio)?; Ok(()) @@ -100,6 +101,13 @@ self.check_if_true(string == "foobar", "Heap") } + fn drivers_only_instantiable_once(&mut self) -> TockResult<()> { + self.check_if_true( + libtock::retrieve_drivers().is_err(), + "Drivers only instantiable once", + ) + } + async fn callbacks(&mut self, timer_context: &mut DriverContext) -> TockResult<()> { let mut callback_hit = false; let mut with_callback = timer_context.with_callback(|_, _| callback_hit = true);
diff --git a/src/drivers.rs b/src/drivers.rs index a8e1e98..4e7eff8 100644 --- a/src/drivers.rs +++ b/src/drivers.rs
@@ -38,9 +38,15 @@ /// Retrieve [Drivers] struct. Returns struct only once. pub fn retrieve_drivers() -> TockResult<Drivers> { - match unsafe { DRIVERS_SINGLETON.take() } { - Some(drivers) => Ok(drivers), - None => Err(TockError::Other(OtherError::DriverAlreadyTaken)), + static mut DRIVER_TAKEN: bool = false; + + unsafe { + if DRIVER_TAKEN { + Err(TockError::Other(OtherError::DriverAlreadyTaken)) + } else { + DRIVER_TAKEN = true; + Ok(retrieve_drivers_unsafe()) + } } } @@ -73,24 +79,3 @@ humidity_sensor: HumiditySensor, ninedof: NinedofDriver, }; - -static mut DRIVERS_SINGLETON: Option<Drivers> = Some(DRIVERS); - -#[cfg(test)] -mod test { - use super::*; - - #[test] - pub fn can_be_retrieved_once() { - reset_drivers_singleton(); - - assert!(retrieve_drivers().is_ok()); - assert!(retrieve_drivers().is_err()); - } - - fn reset_drivers_singleton() { - unsafe { - DRIVERS_SINGLETON = Some(DRIVERS); - }; - } -}