diff options
| author | Peter Krull <[email protected]> | 2024-02-13 23:14:16 +0100 |
|---|---|---|
| committer | Peter Krull <[email protected]> | 2024-02-13 23:14:16 +0100 |
| commit | 37f1c9ac27b0542fdf404392e9bb265fa8ec41d3 (patch) | |
| tree | 4a3ce470d1d817b891447febd25d3addee72d6e2 /embassy-sync | |
| parent | 410c2d440afa2a500ef1398b5b48e746f77815bd (diff) | |
Removed unused lifetime, change most fn -> FnMut
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/multi_signal.rs | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/embassy-sync/src/multi_signal.rs b/embassy-sync/src/multi_signal.rs index bff7ad048..5f724c76b 100644 --- a/embassy-sync/src/multi_signal.rs +++ b/embassy-sync/src/multi_signal.rs | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | //! A synchronization primitive for passing the latest value to **multiple** tasks. | 1 | //! A synchronization primitive for passing the latest value to **multiple** tasks. |
| 2 | use core::cell::RefCell; | 2 | use core::cell::RefCell; |
| 3 | use core::marker::PhantomData; | ||
| 4 | use core::ops::{Deref, DerefMut}; | 3 | use core::ops::{Deref, DerefMut}; |
| 5 | use core::pin::Pin; | 4 | use core::pin::Pin; |
| 6 | use core::task::{Context, Poll}; | 5 | use core::task::{Context, Poll}; |
| @@ -66,9 +65,8 @@ use crate::waitqueue::MultiWakerRegistration; | |||
| 66 | /// }; | 65 | /// }; |
| 67 | /// block_on(f); | 66 | /// block_on(f); |
| 68 | /// ``` | 67 | /// ``` |
| 69 | pub struct MultiSignal<'a, M: RawMutex, T: Clone, const N: usize> { | 68 | pub struct MultiSignal<M: RawMutex, T: Clone, const N: usize> { |
| 70 | mutex: Mutex<M, RefCell<MultiSignalState<N, T>>>, | 69 | mutex: Mutex<M, RefCell<MultiSignalState<N, T>>>, |
| 71 | _phantom: PhantomData<&'a ()>, | ||
| 72 | } | 70 | } |
| 73 | 71 | ||
| 74 | struct MultiSignalState<const N: usize, T: Clone> { | 72 | struct MultiSignalState<const N: usize, T: Clone> { |
| @@ -85,7 +83,7 @@ pub enum Error { | |||
| 85 | MaximumReceiversReached, | 83 | MaximumReceiversReached, |
| 86 | } | 84 | } |
| 87 | 85 | ||
| 88 | impl<'a, M: RawMutex, T: Clone, const N: usize> MultiSignal<'a, M, T, N> { | 86 | impl<'a, M: RawMutex, T: Clone, const N: usize> MultiSignal<M, T, N> { |
| 89 | /// Create a new `MultiSignal` initialized with the given value. | 87 | /// Create a new `MultiSignal` initialized with the given value. |
| 90 | pub const fn new(init: T) -> Self { | 88 | pub const fn new(init: T) -> Self { |
| 91 | Self { | 89 | Self { |
| @@ -95,7 +93,6 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> MultiSignal<'a, M, T, N> { | |||
| 95 | wakers: MultiWakerRegistration::new(), | 93 | wakers: MultiWakerRegistration::new(), |
| 96 | receiver_count: 0, | 94 | receiver_count: 0, |
| 97 | })), | 95 | })), |
| 98 | _phantom: PhantomData, | ||
| 99 | } | 96 | } |
| 100 | } | 97 | } |
| 101 | 98 | ||
| @@ -128,7 +125,7 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> MultiSignal<'a, M, T, N> { | |||
| 128 | } | 125 | } |
| 129 | 126 | ||
| 130 | /// Peek the current value of the `MultiSignal` and check if it satisfies the predicate `f`. | 127 | /// Peek the current value of the `MultiSignal` and check if it satisfies the predicate `f`. |
| 131 | pub fn peek_and(&self, f: fn(&T) -> bool) -> Option<T> { | 128 | pub fn peek_and(&self, mut f: impl FnMut(&T) -> bool) -> Option<T> { |
| 132 | self.mutex.lock(|state| { | 129 | self.mutex.lock(|state| { |
| 133 | let s = state.borrow(); | 130 | let s = state.borrow(); |
| 134 | if f(&s.data) { | 131 | if f(&s.data) { |
| @@ -147,16 +144,16 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> MultiSignal<'a, M, T, N> { | |||
| 147 | } | 144 | } |
| 148 | 145 | ||
| 149 | /// Poll the `MultiSignal` with an optional context. | 146 | /// Poll the `MultiSignal` with an optional context. |
| 150 | fn get_with_context(&'a self, waker: &mut Rcv<'a, M, T, N>, cx: Option<&mut Context>) -> Poll<T> { | 147 | fn get_with_context(&self, rcv: &mut Rcv<'a, M, T, N>, cx: Option<&mut Context>) -> Poll<T> { |
| 151 | self.mutex.lock(|state| { | 148 | self.mutex.lock(|state| { |
| 152 | let mut s = state.borrow_mut(); | 149 | let mut s = state.borrow_mut(); |
| 153 | match (s.current_id > waker.at_id, waker.predicate) { | 150 | match (s.current_id > rcv.at_id, rcv.predicate) { |
| 154 | (true, None) => { | 151 | (true, None) => { |
| 155 | waker.at_id = s.current_id; | 152 | rcv.at_id = s.current_id; |
| 156 | Poll::Ready(s.data.clone()) | 153 | Poll::Ready(s.data.clone()) |
| 157 | } | 154 | } |
| 158 | (true, Some(f)) if f(&s.data) => { | 155 | (true, Some(f)) if f(&s.data) => { |
| 159 | waker.at_id = s.current_id; | 156 | rcv.at_id = s.current_id; |
| 160 | Poll::Ready(s.data.clone()) | 157 | Poll::Ready(s.data.clone()) |
| 161 | } | 158 | } |
| 162 | _ => { | 159 | _ => { |
| @@ -172,7 +169,7 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> MultiSignal<'a, M, T, N> { | |||
| 172 | 169 | ||
| 173 | /// A receiver is able to `.await` a changed `MultiSignal` value. | 170 | /// A receiver is able to `.await` a changed `MultiSignal` value. |
| 174 | pub struct Rcv<'a, M: RawMutex, T: Clone, const N: usize> { | 171 | pub struct Rcv<'a, M: RawMutex, T: Clone, const N: usize> { |
| 175 | multi_sig: &'a MultiSignal<'a, M, T, N>, | 172 | multi_sig: &'a MultiSignal<M, T, N>, |
| 176 | predicate: Option<fn(&T) -> bool>, | 173 | predicate: Option<fn(&T) -> bool>, |
| 177 | at_id: u64, | 174 | at_id: u64, |
| 178 | } | 175 | } |
| @@ -180,7 +177,7 @@ pub struct Rcv<'a, M: RawMutex, T: Clone, const N: usize> { | |||
| 180 | // f: Option<impl FnMut(&T) -> bool> | 177 | // f: Option<impl FnMut(&T) -> bool> |
| 181 | impl<'a, M: RawMutex, T: Clone, const N: usize> Rcv<'a, M, T, N> { | 178 | impl<'a, M: RawMutex, T: Clone, const N: usize> Rcv<'a, M, T, N> { |
| 182 | /// Create a new `Receiver` with a reference the given `MultiSignal`. | 179 | /// Create a new `Receiver` with a reference the given `MultiSignal`. |
| 183 | fn new(multi_sig: &'a MultiSignal<'a, M, T, N>) -> Self { | 180 | fn new(multi_sig: &'a MultiSignal<M, T, N>) -> Self { |
| 184 | Self { | 181 | Self { |
| 185 | multi_sig, | 182 | multi_sig, |
| 186 | predicate: None, | 183 | predicate: None, |
| @@ -195,6 +192,7 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> Rcv<'a, M, T, N> { | |||
| 195 | } | 192 | } |
| 196 | 193 | ||
| 197 | /// Wait for a change to the value of the corresponding `MultiSignal` which matches the predicate `f`. | 194 | /// Wait for a change to the value of the corresponding `MultiSignal` which matches the predicate `f`. |
| 195 | // TODO: How do we make this work with a FnMut closure? | ||
| 198 | pub fn changed_and<'s>(&'s mut self, f: fn(&T) -> bool) -> ReceiverFuture<'s, 'a, M, T, N> { | 196 | pub fn changed_and<'s>(&'s mut self, f: fn(&T) -> bool) -> ReceiverFuture<'s, 'a, M, T, N> { |
| 199 | self.predicate = Some(f); | 197 | self.predicate = Some(f); |
| 200 | ReceiverFuture { subscriber: self } | 198 | ReceiverFuture { subscriber: self } |
| @@ -215,7 +213,7 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> Rcv<'a, M, T, N> { | |||
| 215 | } | 213 | } |
| 216 | 214 | ||
| 217 | /// Try to get a changed value of the corresponding `MultiSignal` which matches the predicate `f`. | 215 | /// Try to get a changed value of the corresponding `MultiSignal` which matches the predicate `f`. |
| 218 | pub fn try_changed_and(&mut self, f: fn(&T) -> bool) -> Option<T> { | 216 | pub fn try_changed_and(&mut self, mut f: impl FnMut(&T) -> bool) -> Option<T> { |
| 219 | self.multi_sig.mutex.lock(|state| { | 217 | self.multi_sig.mutex.lock(|state| { |
| 220 | let s = state.borrow(); | 218 | let s = state.borrow(); |
| 221 | match s.current_id > self.at_id && f(&s.data) { | 219 | match s.current_id > self.at_id && f(&s.data) { |
| @@ -234,7 +232,7 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> Rcv<'a, M, T, N> { | |||
| 234 | } | 232 | } |
| 235 | 233 | ||
| 236 | /// Peek the current value of the corresponding `MultiSignal` and check if it satisfies the predicate `f`. | 234 | /// Peek the current value of the corresponding `MultiSignal` and check if it satisfies the predicate `f`. |
| 237 | pub fn peek_and(&self, f: fn(&T) -> bool) -> Option<T> { | 235 | pub fn peek_and(&self, f: impl FnMut(&T) -> bool) -> Option<T> { |
| 238 | self.multi_sig.peek_and(f) | 236 | self.multi_sig.peek_and(f) |
| 239 | } | 237 | } |
| 240 | 238 | ||
