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. --- src/array.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/array.rs') diff --git a/src/array.rs b/src/array.rs index e01fe74..d460824 100644 --- a/src/array.rs +++ b/src/array.rs @@ -18,7 +18,12 @@ //! use clint::HandlerArray; //! use cortex_m_rt::exception; //! -//! static HANDLERS: HandlerArray = HandlerArray::new(); +//! #[macro_use] +//! extern crate lazy_static; +//! +//! lazy_static! { +//! static ref HANDLERS: HandlerArray<'static> = HandlerArray::new(); +//! } //! //! fn main() { //! // NB: This closure has to be created outside of `with_overrides` to @@ -75,12 +80,28 @@ pub struct HandlerArray<'a> { impl<'a> HandlerArray<'a> { /// Create a new `HandlerArray` filled with no-op handlers. + #[cfg(feature = "const-fn")] pub const fn new() -> Self { Self { h: UnsafeCell::new([Handler::new(); NR_ISR]), } } + #[cfg(not(feature = "const-fn"))] + pub fn new() -> Self { + let h = { + let mut ui_h: [core::mem::MaybeUninit; NR_ISR] = + unsafe { core::mem::MaybeUninit::uninit().assume_init() }; + for h in &mut ui_h[..] { + unsafe { core::ptr::write(h.as_mut_ptr(), Handler::new()) } + } + unsafe { core::mem::transmute(ui_h) } + }; + Self { + h: UnsafeCell::new(h), + } + } + /// Register `f` for entry `nr` in this array using the default /// critical section locker. pub fn register(&self, nr: usize, f: &'a mut F) -- cgit v1.2.3