diff options
| author | Peter Krull <[email protected]> | 2024-02-29 16:56:52 +0100 |
|---|---|---|
| committer | Peter Krull <[email protected]> | 2024-02-29 16:56:52 +0100 |
| commit | 3208e0fec4717ff1580d1cc0cf93e18cbba2db91 (patch) | |
| tree | 0c776e62266ec0f88facf9cd981dff8724ed966b /embassy-sync/src/watch.rs | |
| parent | ae2f10992149279884ea564b00eb18c8bf1f464e (diff) | |
Use Option instead of Result for receiver creation since it is the only way it can fail.
Diffstat (limited to 'embassy-sync/src/watch.rs')
| -rw-r--r-- | embassy-sync/src/watch.rs | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/embassy-sync/src/watch.rs b/embassy-sync/src/watch.rs index 3e22b1e7b..2bba93915 100644 --- a/embassy-sync/src/watch.rs +++ b/embassy-sync/src/watch.rs | |||
| @@ -208,14 +208,7 @@ impl<M: RawMutex, T: Clone, const N: usize> WatchBehavior<T> for Watch<M, T, N> | |||
| 208 | } | 208 | } |
| 209 | } | 209 | } |
| 210 | 210 | ||
| 211 | #[derive(Debug)] | 211 | impl<M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> { |
| 212 | /// An error that can occur when a `Watch` returns a `Result::Err(_)`. | ||
| 213 | pub enum Error { | ||
| 214 | /// The maximum number of [`Receiver`](crate::watch::Receiver)/[`DynReceiver`](crate::watch::DynReceiver) has been reached. | ||
| 215 | MaximumReceiversReached, | ||
| 216 | } | ||
| 217 | |||
| 218 | impl<'a, M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> { | ||
| 219 | /// Create a new `Watch` channel. | 212 | /// Create a new `Watch` channel. |
| 220 | pub const fn new() -> Self { | 213 | pub const fn new() -> Self { |
| 221 | Self { | 214 | Self { |
| @@ -238,28 +231,30 @@ impl<'a, M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> { | |||
| 238 | DynSender(Snd::new(self)) | 231 | DynSender(Snd::new(self)) |
| 239 | } | 232 | } |
| 240 | 233 | ||
| 241 | /// Create a new [`Receiver`] for the `Watch`. | 234 | /// Try to create a new [`Receiver`] for the `Watch`. If the |
| 242 | pub fn receiver(&self) -> Result<Receiver<'_, M, T, N>, Error> { | 235 | /// maximum number of receivers has been reached, `None` is returned. |
| 236 | pub fn receiver(&self) -> Option<Receiver<'_, M, T, N>> { | ||
| 243 | self.mutex.lock(|state| { | 237 | self.mutex.lock(|state| { |
| 244 | let mut s = state.borrow_mut(); | 238 | let mut s = state.borrow_mut(); |
| 245 | if s.receiver_count < N { | 239 | if s.receiver_count < N { |
| 246 | s.receiver_count += 1; | 240 | s.receiver_count += 1; |
| 247 | Ok(Receiver(Rcv::new(self, 0))) | 241 | Some(Receiver(Rcv::new(self, 0))) |
| 248 | } else { | 242 | } else { |
| 249 | Err(Error::MaximumReceiversReached) | 243 | None |
| 250 | } | 244 | } |
| 251 | }) | 245 | }) |
| 252 | } | 246 | } |
| 253 | 247 | ||
| 254 | /// Create a new [`DynReceiver`] for the `Watch`. | 248 | /// Try to create a new [`DynReceiver`] for the `Watch`. If the |
| 255 | pub fn dyn_receiver(&self) -> Result<DynReceiver<'_, T>, Error> { | 249 | /// maximum number of receivers has been reached, `None` is returned. |
| 250 | pub fn dyn_receiver(&self) -> Option<DynReceiver<'_, T>> { | ||
| 256 | self.mutex.lock(|state| { | 251 | self.mutex.lock(|state| { |
| 257 | let mut s = state.borrow_mut(); | 252 | let mut s = state.borrow_mut(); |
| 258 | if s.receiver_count < N { | 253 | if s.receiver_count < N { |
| 259 | s.receiver_count += 1; | 254 | s.receiver_count += 1; |
| 260 | Ok(DynReceiver(Rcv::new(self, 0))) | 255 | Some(DynReceiver(Rcv::new(self, 0))) |
| 261 | } else { | 256 | } else { |
| 262 | Err(Error::MaximumReceiversReached) | 257 | None |
| 263 | } | 258 | } |
| 264 | }) | 259 | }) |
| 265 | } | 260 | } |
