From 392239c7f522acb7ca5847a48dd9ab2480242924 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Tue, 6 Aug 2019 16:31:34 -0400 Subject: Add compatibilty with rust-stable. * Add feature flag for const-fn functionality, which simplifies some things and lets this crate run on architectures where atomic CAS isn't available (e.g., thumbv6). * Remove `const fn` where necessary to compile under rust-stable. * BREAKING CHANGE - Change signature of `Handler::replace` to take a mutable borrow. This is required because the underlying type is now a NonNull, which needs `mut`. The old behavior was never desired, but a consequence of trying to avoid `Option` by using a default handler. * Use lazy_static crate for initialization of Handler/HandlerArray. * Specify Sync for Handler. This is not amazing, since it isn't, but it's necessary for initialization in static contexts inside `lazy_static`. This should be fine, because any actual manipulation of the handler is done in unsafe functions. --- examples/dummy.rs | 11 ++++++++--- examples/scope.rs | 7 ++++++- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/dummy.rs b/examples/dummy.rs index 7e7c237..cfcd717 100644 --- a/examples/dummy.rs +++ b/examples/dummy.rs @@ -1,24 +1,29 @@ use clint::Handler; +#[macro_use] +extern crate lazy_static; + // Wrapper used to call through to `example_handler` via `closure` in // `main`. `Handler::new()` places a do-nothing handler in this at // compile-time, in case the interrupt using this handler is fired // before being `replace`d in `main`. -static mut HANDLER: Handler = Handler::new(); +lazy_static! { + static ref HANDLER: Handler<'static> = Handler::new(); +} fn main() { let mut x: u32 = 0; // Create a closure to take a mutable reference to `x` for use in // `example_handler`. - let closure = move || example_handler(&mut x); + let mut closure = move || example_handler(&mut x); // Swap out the do-nothing handler with our closure that calls // through to `example_handler`. Ideally, the interrupt which uses // this handler would be disabled while this happens, but as this // is a demo, and there aren't any actual interrupts firing, this // is left as an exercise to the reader. - unsafe { HANDLER.replace(&closure) }; + unsafe { HANDLER.replace(&mut closure) }; // Simulate firing the interrupt. dummy_interrupt(); diff --git a/examples/scope.rs b/examples/scope.rs index cd17570..8df0702 100644 --- a/examples/scope.rs +++ b/examples/scope.rs @@ -1,6 +1,11 @@ use clint::HandlerArray; -static HANDLERS: HandlerArray = HandlerArray::new(); +#[macro_use] +extern crate lazy_static; + +lazy_static! { + static ref HANDLERS: HandlerArray<'static> = HandlerArray::new(); +} fn main() { let mut cl = || println!("whoa!"); -- cgit v1.2.3