EphemeralOption

Struct EphemeralOption 

Source
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>

Source

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));
Source

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));
Source

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);
Source

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.

Source

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.

Source

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);
Source

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.

Source

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.

Source

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"));
Source

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"));
Source

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);
Source

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));
Source

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));
Source

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.

Trait Implementations§

Source§

impl<T> Clone for EphemeralOption<T>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for EphemeralOption<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for EphemeralOption<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for EphemeralOption<T>

§

impl<T> !RefUnwindSafe for EphemeralOption<T>

§

impl<T> Send for EphemeralOption<T>
where T: Send,

§

impl<T> !Sync for EphemeralOption<T>

§

impl<T> Unpin for EphemeralOption<T>
where T: Unpin,

§

impl<T> UnwindSafe for EphemeralOption<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.