Skip to content

Use CriticalSection<'cs> instead of &'cs CriticalSection. #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![no_std]

use core::cell::UnsafeCell;
use core::marker::PhantomData;

/// A peripheral
#[derive(Debug)]
Expand All @@ -26,7 +27,7 @@ impl<T> Peripheral<T> {
}

/// Borrows the peripheral for the duration of a critical section
pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
pub fn borrow<'cs>(&self, _ctxt: CriticalSection<'cs>) -> &'cs T {
unsafe { &*self.get() }
}

Expand All @@ -39,17 +40,18 @@ impl<T> Peripheral<T> {
/// Critical section token
///
/// Indicates that you are executing code within a critical section
pub struct CriticalSection {
_0: (),
#[derive(Clone, Copy)]
pub struct CriticalSection<'cs> {
_0: PhantomData<&'cs ()>,
}

impl CriticalSection {
impl<'cs> CriticalSection<'cs> {
/// Creates a critical section token
///
/// This method is meant to be used to create safe abstractions rather than
/// meant to be directly used in applications.
pub unsafe fn new() -> Self {
CriticalSection { _0: () }
CriticalSection { _0: PhantomData }
}
}

Expand All @@ -75,13 +77,13 @@ impl<T> Mutex<T> {

impl<T> Mutex<T> {
/// Borrows the data for the duration of the critical section
pub fn borrow<'cs>(&'cs self, _cs: &'cs CriticalSection) -> &'cs T {
pub fn borrow<'cs>(&'cs self, _cs: CriticalSection<'cs>) -> &'cs T {
unsafe { &*self.inner.get() }
}
}

/// ``` compile_fail
/// fn bad(cs: &bare_metal::CriticalSection) -> &u32 {
/// fn bad(cs: bare_metal::CriticalSection) -> &u32 {
/// let x = bare_metal::Mutex::new(42u32);
/// x.borrow(cs)
/// }
Expand Down