]>
Commit | Line | Data |
---|---|---|
064997fb FG |
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 | pub fn is_present(&self) -> bool { | |
15 | matches!(self, TryResult::Present(_)) | |
16 | } | |
17 | ||
18 | /// Returns `true` if the shard wasn't locked, and the value wasn't present in the map. | |
19 | pub fn is_absent(&self) -> bool { | |
20 | matches!(self, TryResult::Absent) | |
21 | } | |
22 | ||
23 | /// Returns `true` if the shard was locked. | |
24 | pub fn is_locked(&self) -> bool { | |
25 | matches!(self, TryResult::Locked) | |
26 | } | |
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 | pub fn unwrap(self) -> R { | |
31 | match self { | |
32 | TryResult::Present(r) => r, | |
33 | TryResult::Locked => panic!("Called unwrap() on TryResult::Locked"), | |
34 | TryResult::Absent => panic!("Called unwrap() on TryResult::Absent"), | |
35 | } | |
36 | } | |
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 | pub fn try_unwrap(self) -> Option<R> { | |
41 | match self { | |
42 | TryResult::Present(r) => Some(r), | |
43 | _ => None, | |
44 | } | |
45 | } | |
46 | } |