embassy: add missing docs, add warn(missing_docs).

This commit is contained in:
Dario Nieuwenhuis 2022-06-26 00:53:35 +02:00
parent f4e2686eca
commit 5903e08f4b
9 changed files with 66 additions and 10 deletions

View File

@ -160,11 +160,20 @@ mod thread_mode_mutex {
} }
impl<T: ?Sized> ThreadModeMutex<T> { impl<T: ?Sized> ThreadModeMutex<T> {
/// Lock the `ThreadModeMutex`, granting access to the data.
///
/// # Panics
///
/// This will panic if not currently running in thread mode.
pub fn lock<R>(&self, f: impl FnOnce(&T) -> R) -> R { pub fn lock<R>(&self, f: impl FnOnce(&T) -> R) -> R {
f(self.borrow()) f(self.borrow())
} }
/// Borrows the data /// Borrows the data
///
/// # Panics
///
/// This will panic if not currently running in thread mode.
pub fn borrow(&self) -> &T { pub fn borrow(&self) -> &T {
assert!( assert!(
raw::in_thread_mode(), raw::in_thread_mode(),

View File

@ -44,6 +44,7 @@ unsafe impl Send for CriticalSectionRawMutex {}
unsafe impl Sync for CriticalSectionRawMutex {} unsafe impl Sync for CriticalSectionRawMutex {}
impl CriticalSectionRawMutex { impl CriticalSectionRawMutex {
/// Create a new `CriticalSectionRawMutex`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { _phantom: PhantomData } Self { _phantom: PhantomData }
} }
@ -71,6 +72,7 @@ pub struct NoopRawMutex {
unsafe impl Send for NoopRawMutex {} unsafe impl Send for NoopRawMutex {}
impl NoopRawMutex { impl NoopRawMutex {
/// Create a new `NoopRawMutex`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { _phantom: PhantomData } Self { _phantom: PhantomData }
} }
@ -104,6 +106,7 @@ mod thread_mode {
unsafe impl Sync for ThreadModeRawMutex {} unsafe impl Sync for ThreadModeRawMutex {}
impl ThreadModeRawMutex { impl ThreadModeRawMutex {
/// Create a new `ThreadModeRawMutex`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { _phantom: PhantomData } Self { _phantom: PhantomData }
} }

View File

@ -180,6 +180,7 @@ where
} }
} }
/// Future returned by [`Channel::recv`] and [`Receiver::recv`].
pub struct RecvFuture<'ch, M, T, const N: usize> pub struct RecvFuture<'ch, M, T, const N: usize>
where where
M: RawMutex, M: RawMutex,
@ -201,6 +202,7 @@ where
} }
} }
/// Future returned by [`DynamicReceiver::recv`].
pub struct DynamicRecvFuture<'ch, T> { pub struct DynamicRecvFuture<'ch, T> {
channel: &'ch dyn DynamicChannel<T>, channel: &'ch dyn DynamicChannel<T>,
} }
@ -216,6 +218,7 @@ impl<'ch, T> Future for DynamicRecvFuture<'ch, T> {
} }
} }
/// Future returned by [`Channel::send`] and [`Sender::send`].
pub struct SendFuture<'ch, M, T, const N: usize> pub struct SendFuture<'ch, M, T, const N: usize>
where where
M: RawMutex, M: RawMutex,
@ -246,6 +249,7 @@ where
impl<'ch, M, T, const N: usize> Unpin for SendFuture<'ch, M, T, N> where M: RawMutex {} impl<'ch, M, T, const N: usize> Unpin for SendFuture<'ch, M, T, N> where M: RawMutex {}
/// Future returned by [`DynamicSender::send`].
pub struct DynamicSendFuture<'ch, T> { pub struct DynamicSendFuture<'ch, T> {
channel: &'ch dyn DynamicChannel<T>, channel: &'ch dyn DynamicChannel<T>,
message: Option<T>, message: Option<T>,

View File

@ -4,11 +4,19 @@ use core::future::Future;
use core::mem; use core::mem;
use core::task::{Context, Poll, Waker}; use core::task::{Context, Poll, Waker};
/// Synchronization primitive. Allows creating awaitable signals that may be passed between tasks. /// Single-slot signaling primitive.
/// For a simple use-case where the receiver is only ever interested in the latest value of
/// something, Signals work well. For more advanced use cases, you might want to use [`Channel`](crate::channel::mpmc::Channel) instead..
/// ///
/// Signals are generally declared as being a static const and then borrowed as required. /// This is similar to a [`Channel`](crate::channel::mpmc::Channel) with a buffer size of 1, except
/// "sending" to it (calling [`Signal::signal`]) when full will overwrite the previous value instead
/// of waiting for the receiver to pop the previous value.
///
/// It is useful for sending data between tasks when the receiver only cares about
/// the latest data, and therefore it's fine to "lose" messages. This is often the case for "state"
/// updates.
///
/// For more advanced use cases, you might want to use [`Channel`](crate::channel::mpmc::Channel) instead.
///
/// Signals are generally declared as `static`s and then borrowed as required.
/// ///
/// ``` /// ```
/// use embassy::channel::signal::Signal; /// use embassy::channel::signal::Signal;
@ -34,6 +42,7 @@ unsafe impl<T: Send> Send for Signal<T> {}
unsafe impl<T: Send> Sync for Signal<T> {} unsafe impl<T: Send> Sync for Signal<T> {}
impl<T> Signal<T> { impl<T> Signal<T> {
/// Create a new `Signal`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
state: UnsafeCell::new(State::None), state: UnsafeCell::new(State::None),
@ -42,7 +51,7 @@ impl<T> Signal<T> {
} }
impl<T: Send> Signal<T> { impl<T: Send> Signal<T> {
/// Mark this Signal as completed. /// Mark this Signal as signaled.
pub fn signal(&self, val: T) { pub fn signal(&self, val: T) {
critical_section::with(|_| unsafe { critical_section::with(|_| unsafe {
let state = &mut *self.state.get(); let state = &mut *self.state.get();
@ -52,6 +61,7 @@ impl<T: Send> Signal<T> {
}) })
} }
/// Remove the queued value in this `Signal`, if any.
pub fn reset(&self) { pub fn reset(&self) {
critical_section::with(|_| unsafe { critical_section::with(|_| unsafe {
let state = &mut *self.state.get(); let state = &mut *self.state.get();
@ -59,7 +69,7 @@ impl<T: Send> Signal<T> {
}) })
} }
pub fn poll_wait(&self, cx: &mut Context<'_>) -> Poll<T> { fn poll_wait(&self, cx: &mut Context<'_>) -> Poll<T> {
critical_section::with(|_| unsafe { critical_section::with(|_| unsafe {
let state = &mut *self.state.get(); let state = &mut *self.state.get();
match state { match state {

View File

@ -3,6 +3,7 @@
#![cfg_attr(all(feature = "nightly", target_arch = "xtensa"), feature(asm_experimental_arch))] #![cfg_attr(all(feature = "nightly", target_arch = "xtensa"), feature(asm_experimental_arch))]
#![allow(clippy::new_without_default)] #![allow(clippy::new_without_default)]
#![doc = include_str!("../../README.md")] #![doc = include_str!("../../README.md")]
#![warn(missing_docs)]
// This mod MUST go first, so that the others see its macros. // This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt; pub(crate) mod fmt;

View File

@ -31,6 +31,7 @@ unsafe impl<T> Send for Forever<T> {}
unsafe impl<T> Sync for Forever<T> {} unsafe impl<T> Sync for Forever<T> {}
impl<T> Forever<T> { impl<T> Forever<T> {
/// Create a new `Forever`.
#[inline(always)] #[inline(always)]
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
@ -39,11 +40,15 @@ impl<T> Forever<T> {
} }
} }
/// Gives this `Forever` a value. /// Store a value in this `Forever`, returning a mutable reference to it.
/// ///
/// Panics if this `Forever` already has a value. /// Using this method, the compiler usually constructs `val` in the stack and then moves
/// it into the `Forever`. If `T` is big, this is likely to cause stack overflows.
/// Considering using [`Signal::put_with`] instead, which will construct it in-place inside the `Forever`.
/// ///
/// Returns a mutable reference to the stored value. /// # Panics
///
/// Panics if this `Forever` already has a value stored in it.
#[inline(always)] #[inline(always)]
#[allow(clippy::mut_from_ref)] #[allow(clippy::mut_from_ref)]
pub fn put(&'static self, val: T) -> &'static mut T { pub fn put(&'static self, val: T) -> &'static mut T {
@ -52,7 +57,7 @@ impl<T> Forever<T> {
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
.is_err() .is_err()
{ {
panic!("Forever.put() called multiple times"); panic!("Forever::put() called multiple times");
} }
unsafe { unsafe {
@ -63,6 +68,14 @@ impl<T> Forever<T> {
} }
} }
/// Store the closure return value in this `Forever`, returning a mutable reference to it.
///
/// The advantage over [`Forever::put`] is that this method allows the closure to construct
/// the `T` value in-place directly inside the `Forever`, saving stack space.
///
/// # Panics
///
/// Panics if this `Forever` already has a value stored in it.
#[inline(always)] #[inline(always)]
#[allow(clippy::mut_from_ref)] #[allow(clippy::mut_from_ref)]
pub fn put_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T { pub fn put_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T {

View File

@ -2,9 +2,12 @@ use core::future::Future;
use core::pin::Pin; use core::pin::Pin;
use core::task::{Context, Poll}; use core::task::{Context, Poll};
/// Result for [`select`].
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Either<A, B> { pub enum Either<A, B> {
/// First future finished first.
First(A), First(A),
/// Second future finished first.
Second(B), Second(B),
} }
@ -55,10 +58,14 @@ where
// ==================================================================== // ====================================================================
/// Result for [`select3`].
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Either3<A, B, C> { pub enum Either3<A, B, C> {
/// First future finished first.
First(A), First(A),
/// Second future finished first.
Second(B), Second(B),
/// Third future finished first.
Third(C), Third(C),
} }
@ -109,11 +116,16 @@ where
// ==================================================================== // ====================================================================
/// Result for [`select4`].
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Either4<A, B, C, D> { pub enum Either4<A, B, C, D> {
/// First future finished first.
First(A), First(A),
/// Second future finished first.
Second(B), Second(B),
/// Third future finished first.
Third(C), Third(C),
/// Fourth future finished first.
Fourth(D), Fourth(D),
} }

View File

@ -16,6 +16,7 @@ pub struct WakerRegistration {
} }
impl WakerRegistration { impl WakerRegistration {
/// Create a new `WakerRegistration`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { waker: None } Self { waker: None }
} }
@ -72,6 +73,7 @@ pub struct AtomicWaker {
} }
impl AtomicWaker { impl AtomicWaker {
/// Create a new `AtomicWaker`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
waker: AtomicPtr::new(ptr::null_mut()), waker: AtomicPtr::new(ptr::null_mut()),

View File

@ -12,6 +12,7 @@ pub struct WakerRegistration {
} }
impl WakerRegistration { impl WakerRegistration {
/// Create a new `WakerRegistration`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { waker: None } Self { waker: None }
} }
@ -60,6 +61,7 @@ pub struct AtomicWaker {
} }
impl AtomicWaker { impl AtomicWaker {
/// Create a new `AtomicWaker`.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
waker: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)), waker: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)),