diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/dashmap/src/try_result.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/dashmap/src/try_result.rs')
-rw-r--r-- | vendor/dashmap/src/try_result.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/vendor/dashmap/src/try_result.rs b/vendor/dashmap/src/try_result.rs new file mode 100644 index 000000000..aa93d3b2c --- /dev/null +++ b/vendor/dashmap/src/try_result.rs @@ -0,0 +1,46 @@ +/// Represents the result of a non-blocking read from a [DashMap](crate::DashMap). +#[derive(Debug)] +pub enum TryResult<R> { + /// The value was present in the map, and the lock for the shard was successfully obtained. + Present(R), + /// The shard wasn't locked, and the value wasn't present in the map. + Absent, + /// The shard was locked. + Locked, +} + +impl<R> TryResult<R> { + /// Returns `true` if the value was present in the map, and the lock for the shard was successfully obtained. + pub fn is_present(&self) -> bool { + matches!(self, TryResult::Present(_)) + } + + /// Returns `true` if the shard wasn't locked, and the value wasn't present in the map. + pub fn is_absent(&self) -> bool { + matches!(self, TryResult::Absent) + } + + /// Returns `true` if the shard was locked. + pub fn is_locked(&self) -> bool { + matches!(self, TryResult::Locked) + } + + /// If `self` is [Present](TryResult::Present), returns the reference to the value in the map. + /// Panics if `self` is not [Present](TryResult::Present). + pub fn unwrap(self) -> R { + match self { + TryResult::Present(r) => r, + TryResult::Locked => panic!("Called unwrap() on TryResult::Locked"), + TryResult::Absent => panic!("Called unwrap() on TryResult::Absent"), + } + } + + /// If `self` is [Present](TryResult::Present), returns the reference to the value in the map. + /// If `self` is not [Present](TryResult::Present), returns `None`. + pub fn try_unwrap(self) -> Option<R> { + match self { + TryResult::Present(r) => Some(r), + _ => None, + } + } +} |