pub struct EphemeralOption<T> { /* private fields */ }Expand description
An Option that automatically reverts to None after a certain amount of time
The value in the EphemeralOption is not dropped when time expires,
only when it is overwritten or the EphemeralOption itself is dropped
Implementations§
Source§impl<T> EphemeralOption<T>
impl<T> EphemeralOption<T>
Sourcepub fn new(val: T, max_time: Duration) -> Self
pub fn new(val: T, max_time: Duration) -> Self
Create a new EphemeralOption<T> with a value that expires after a set amount of time.
let opt = EphemeralOption::new("Hello, World!", Duration::from_secs(2));Sourcepub const fn new_empty(max_time: Duration) -> Self
pub const fn new_empty(max_time: Duration) -> Self
Create a new, empty EphemeralOption<T> that will expire a value after a set amount of time.
let opt: EphemeralOption<()> = EphemeralOption::new_empty(Duration::from_secs(2));Sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Get a shared reference to the value of the EphemeralOption.
Will return None if it is empty or if it has expired.
let opt = EphemeralOption::new(3, Duration::from_secs(2));
assert_eq!(opt.get(), Some(&3));
sleep(Duration::from_secs(2));
assert_eq!(opt.get(), None);Sourcepub const unsafe fn get_unchecked(&self) -> &T
pub const unsafe fn get_unchecked(&self) -> &T
Get a shared reference to the value of the EphemeralOption
without checking if it exists or not.
It is almost always a better idea to use get or get_expired instead of this.
§Safety
Calling this function will cause undefined behavior if there is no value inside
of the EphemeralOption.
Sourcepub fn get_expired(&self) -> Option<&T>
pub fn get_expired(&self) -> Option<&T>
Get a shared reference to the value of the EphemeralOption
regardless of whether it has expired or not.
Will only return None if the value does not exist.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Get a mutable, exclusive reference to the value of the EphemeralOption.
Will return None if it is empty or if it has expired.
let mut opt = EphemeralOption::new("hello", Duration::from_secs(2));
let val = opt.get_mut().unwrap();
assert_eq!(val, &mut "hello");
*val = "world";
assert_eq!(val, &mut "world");
sleep(Duration::from_secs(2));
assert_eq!(opt.get_mut(), None);Sourcepub unsafe fn get_mut_unchecked(&mut self) -> &mut T
pub unsafe fn get_mut_unchecked(&mut self) -> &mut T
Get an exclusive, mutable reference to the value of the
EphemeralOption without checking if it exists or not.
It is almost always a better idea to use get_mut or get_mut_expired instead of this.
§Safety
Calling this function will cause undefined behavior if there is no value inside
of the EphemeralOption.
Sourcepub fn get_mut_expired(&mut self) -> Option<&mut T>
pub fn get_mut_expired(&mut self) -> Option<&mut T>
Get a mutable, exclusive reference to the value of the EphemeralOption
regardless of whether it has expired or not.
Will only return None if the value does not exist.
Sourcepub fn insert(&mut self, val: T) -> &mut T
pub fn insert(&mut self, val: T) -> &mut T
Overwrite the value in the EphemeralOption.
This will drop the value currently in the EphemeralOption if it has expired.
This resets the timer for when the value expires.
For convenience, this returns a mutable reference to the inserted value.
let mut opt = EphemeralOption::new("hello", Duration::from_secs(2));
opt.insert("world");
assert_eq!(opt.get(), Some(&"world"));Sourcepub fn get_or_insert(&mut self, val: T) -> &mut T
pub fn get_or_insert(&mut self, val: T) -> &mut T
Overwrite the value in the EphemeralOption if it is currently None.
This will drop the value currently in the EphemeralOption if it has expired.
If a new value is inserted, this resets the timer for when it expires.
For convenience, this returns a mutable reference to the contained value.
let mut opt = EphemeralOption::new("hello", Duration::from_secs(2));
opt.insert("world");
assert_eq!(opt.get(), Some(&"world"));Sourcepub fn take(&mut self) -> Option<T>
pub fn take(&mut self) -> Option<T>
Take the value out of the EphemeralOption, leaving it empty.
This will drop the value currently in the EphemeralOption if it has expired.
let mut opt = EphemeralOption::new(5, Duration::from_secs(2));
let num = opt.take();
assert_eq!(num, Some(5));
assert_eq!(opt.take(), None);Sourcepub fn replace(&mut self, val: T) -> Option<T>
pub fn replace(&mut self, val: T) -> Option<T>
Replaces the value in the EphemeralOption with the new one and
returns the old value if present, without deinitializing either one.
This resets the timer for when the value expires.
This will drop the value currently in the EphemeralOption if it has expired.
let mut opt = EphemeralOption::new(3.14, Duration::from_secs(2));
let num = opt.replace(2.718);
assert_eq!(num, Some(3.14));
assert_eq!(opt.get(), Some(&2.718));Sourcepub fn reset_timer(&self)
pub fn reset_timer(&self)
Reset the timer for when the value expires.
let mut opt = EphemeralOption::new(3, Duration::from_secs(2));
sleep(Duration::from_secs(2));
opt.reset_timer();
assert_eq!(opt.get(), Some(&3));Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Convert an EphemeralOption<T> into an Option<T>.
The Option will be Some(T) only if the value exists and has not expired,
otherwise it will be None.