Skip to main content

irox_tools/sync/
mod.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2025 IROX Contributors
3//
4
5//! More complex synchronization primitives than in the STD.
6
7pub use eventual::*;
8pub use flags::*;
9pub use optional::*;
10use std::sync::MutexGuard;
11mod eventual;
12mod once;
13pub use exchange::*;
14mod exchange;
15mod flags;
16mod optional;
17
18pub enum MaybeLocked<'a, T> {
19    Borrowed(&'a T),
20    MutBorrowed(&'a mut T),
21    Locked(MutexGuard<'a, T>),
22}
23
24#[allow(clippy::should_implement_trait)]
25impl<'a, T> MaybeLocked<'a, T> {
26    pub fn deref(&'a self) -> &'a T {
27        match self {
28            MaybeLocked::Borrowed(t) => t,
29            MaybeLocked::MutBorrowed(t) => t,
30            MaybeLocked::Locked(t) => t,
31        }
32    }
33
34    pub fn deref_mut(&'a mut self) -> Option<&'a mut T> {
35        match self {
36            MaybeLocked::MutBorrowed(t) => Some(t),
37            MaybeLocked::Locked(t) => Some(t),
38            _ => None,
39        }
40    }
41    pub fn map<F: FnMut(&'a T) -> R, R>(&'a self, mut func: F) -> R {
42        match self {
43            MaybeLocked::Borrowed(t) => func(t),
44            MaybeLocked::MutBorrowed(t) => func(t),
45            MaybeLocked::Locked(t) => func(t),
46        }
47    }
48}