cleanup
This commit is contained in:
parent
b68f4ad5c6
commit
f89e64aa76
3 changed files with 14 additions and 6 deletions
|
|
@ -9,6 +9,7 @@ pub enum Cached<T: Clone + Debug> {
|
|||
}
|
||||
|
||||
impl<T: Clone + Debug> Cached<T> {
|
||||
/// Gets the value if in state `Self::Used`.
|
||||
pub fn get(&self) -> Option<&T> {
|
||||
match self {
|
||||
Self::Empty | Self::Unused(_) => None,
|
||||
|
|
@ -16,6 +17,7 @@ impl<T: Clone + Debug> Cached<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gets the value mutably if in state `Self::Used`.
|
||||
pub fn get_mut(&mut self) -> Option<&mut T> {
|
||||
match self {
|
||||
Self::Empty | Self::Unused(_) => None,
|
||||
|
|
@ -23,6 +25,7 @@ impl<T: Clone + Debug> Cached<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Checks if the value is empty or unused.
|
||||
pub fn is_unused(&self) -> bool {
|
||||
match self {
|
||||
Self::Empty | Self::Unused(_) => true,
|
||||
|
|
@ -30,6 +33,7 @@ impl<T: Clone + Debug> Cached<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Checks if the value is used (i.e. cached for access).
|
||||
pub fn is_used(&self) -> bool {
|
||||
match self {
|
||||
Self::Empty | Self::Unused(_) => false,
|
||||
|
|
@ -37,6 +41,7 @@ impl<T: Clone + Debug> Cached<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Takes the buffered value if in state `Self::Unused`.
|
||||
pub fn take_unused(&mut self) -> Option<T> {
|
||||
if matches!(*self, Self::Unused(_)) {
|
||||
let Self::Unused(val) = std::mem::replace(self, Self::Empty) else {
|
||||
|
|
@ -48,6 +53,7 @@ impl<T: Clone + Debug> Cached<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Takes the cached value if in state `Self::Used`.
|
||||
pub fn take_used(&mut self) -> Option<T> {
|
||||
if matches!(*self, Self::Used(_)) {
|
||||
let Self::Used(val) = std::mem::replace(self, Self::Empty) else {
|
||||
|
|
@ -59,12 +65,14 @@ impl<T: Clone + Debug> Cached<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Moves the value from `Self::Used` to `Self::Unused`.
|
||||
pub fn set_unused(&mut self) {
|
||||
if matches!(*self, Self::Used(_)) {
|
||||
*self = Self::Unused(self.take_used().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the value to `Self::Used`.
|
||||
pub fn set_used(&mut self, val: T) {
|
||||
*self = Self::Used(val);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue