/rust/registry/src/index.crates.io-1949cf8c6b5b557f/dashmap-6.1.0/src/try_result.rs
Line | Count | Source |
1 | | /// Represents the result of a non-blocking read from a [DashMap](crate::DashMap). |
2 | | #[derive(Debug)] |
3 | | pub enum TryResult<R> { |
4 | | /// The value was present in the map, and the lock for the shard was successfully obtained. |
5 | | Present(R), |
6 | | /// The shard wasn't locked, and the value wasn't present in the map. |
7 | | Absent, |
8 | | /// The shard was locked. |
9 | | Locked, |
10 | | } |
11 | | |
12 | | impl<R> TryResult<R> { |
13 | | /// Returns `true` if the value was present in the map, and the lock for the shard was successfully obtained. |
14 | 0 | pub fn is_present(&self) -> bool { |
15 | 0 | matches!(self, TryResult::Present(_)) |
16 | 0 | } |
17 | | |
18 | | /// Returns `true` if the shard wasn't locked, and the value wasn't present in the map. |
19 | 0 | pub fn is_absent(&self) -> bool { |
20 | 0 | matches!(self, TryResult::Absent) |
21 | 0 | } |
22 | | |
23 | | /// Returns `true` if the shard was locked. |
24 | 0 | pub fn is_locked(&self) -> bool { |
25 | 0 | matches!(self, TryResult::Locked) |
26 | 0 | } |
27 | | |
28 | | /// If `self` is [Present](TryResult::Present), returns the reference to the value in the map. |
29 | | /// Panics if `self` is not [Present](TryResult::Present). |
30 | 0 | pub fn unwrap(self) -> R { |
31 | 0 | match self { |
32 | 0 | TryResult::Present(r) => r, |
33 | 0 | TryResult::Locked => panic!("Called unwrap() on TryResult::Locked"), |
34 | 0 | TryResult::Absent => panic!("Called unwrap() on TryResult::Absent"), |
35 | | } |
36 | 0 | } |
37 | | |
38 | | /// If `self` is [Present](TryResult::Present), returns the reference to the value in the map. |
39 | | /// If `self` is not [Present](TryResult::Present), returns `None`. |
40 | 0 | pub fn try_unwrap(self) -> Option<R> { |
41 | 0 | match self { |
42 | 0 | TryResult::Present(r) => Some(r), |
43 | 0 | _ => None, |
44 | | } |
45 | 0 | } |
46 | | } |