-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DashMap::try_get, DashMap::try_get_mut, DashMap::try_entry
- Loading branch information
1 parent
a2cff0e
commit e3de359
Showing
3 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
/// 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. | ||
#[inline] | ||
pub fn is_present(&self) -> bool { | ||
match self { | ||
TryResult::Present(_) => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Returns `true` if the shard wasn't locked, and the value wasn't present in the map. | ||
#[inline] | ||
pub fn is_absent(&self) -> bool { | ||
match self { | ||
TryResult::Absent => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Returns `true` if the shard was locked. | ||
#[inline] | ||
pub fn is_locked(&self) -> bool { | ||
match self { | ||
TryResult::Locked => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
/// 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, | ||
} | ||
} | ||
} |