Enum type_set::entry::Entry

source ·
pub enum Entry<'a, T> {
    Vacant(VacantEntry<'a, T>),
    Occupied(OccupiedEntry<'a, T>),
}
Expand description

A view into a single type in the TypeSet, which may be either vacant or occupied.

This type is constructed by TypeSet::entry

§Examples

This is a somewhat contrived example that demonstrates matching on the Entry. Often, Entry::or_insert, Entry::or_insert_with, and Entry::and_modify can achieve comparable results. See those functions for further usage examples.

use type_set::{TypeSet, entry::Entry};
let mut set = TypeSet::new().with("hello");
let (previous, current) = match set.entry::<&'static str>() {
    Entry::Vacant(vacant_entry) => {
        let current = vacant_entry.insert("entry was vacant");
        (None, current)
    }

    Entry::Occupied(mut occupied_entry) => {
        let previous = occupied_entry.insert("entry was occupied");
        (Some(previous), occupied_entry.into_mut())
    }
};
assert_eq!(previous, Some("hello"));
assert_eq!(*current, "entry was occupied");

Variants§

§

Vacant(VacantEntry<'a, T>)

A view into the location a T would be stored in the TypeSet. See VacantEntry

§

Occupied(OccupiedEntry<'a, T>)

A view into the location a T is currently stored in the TypeSet. See OccupiedEntry

Implementations§

source§

impl<'a, T: Send + Sync + 'static> Entry<'a, T>

source

pub fn or_insert(self, default: T) -> &'a mut T

Ensures a value is in the Entry by inserting the provided default value if the Entry was previously vacant. Returns a mutable reference to the value.

Prefer Entry::or_insert_with if constructing a T is expensive.

§Example
let mut set = type_set::TypeSet::new();
assert_eq!(*set.entry().or_insert("hello"), "hello");
assert_eq!(set.get::<&'static str>(), Some(&"hello"));
assert_eq!(*set.entry().or_insert("world"), "hello");
assert_eq!(set.get::<&'static str>(), Some(&"hello"));
source

pub fn or_insert_with(self, default: impl FnOnce() -> T) -> &'a mut T

Ensures a value is in the Entry by inserting the provided value returned by the default function if the Entry was previously vacant. Returns a mutable reference to the value.

Prefer this to Entry::or_insert if constructing a T is expensive.

§Example
let mut set = type_set::TypeSet::new();
assert_eq!(*set.entry().or_insert_with(|| String::from("hello")), "hello");
assert_eq!(set.get::<String>(), Some(&String::from("hello")));
assert_eq!(*set.entry::<String>().or_insert_with(|| panic!("never called")), "hello");
assert_eq!(set.get::<String>(), Some(&String::from("hello")));
source

pub fn and_modify(self, f: impl FnOnce(&mut T)) -> Self

Provides in-place mutable access to an occupied entry before any potential inserts into the set using Entry::or_insert or Entry::or_insert_with.

§Example
let mut set = type_set::TypeSet::new().with(String::from("hello"));
let value = set.entry::<String>()
    .and_modify(|s| s.push_str(" world"))
    .or_insert_with(|| String::from("greetings"));
assert_eq!(value, "hello world");

set.take::<String>();
let value = set.entry::<String>()
    .and_modify(|s| s.push_str(" world"))
    .or_insert_with(|| String::from("greetings"));
assert_eq!(value, "greetings");
source

pub fn take(self) -> Option<T>

Remove and return a value from this entry, if occupied.

source

pub fn unwrap_occupied(self) -> OccupiedEntry<'a, T>

Returns an OccupiedEntry or panic

§Panics

This function will panic if the entry is vacant

source

pub fn unwrap_vacant(self) -> VacantEntry<'a, T>

Returns a VacantEntry or panic

§Panics

This function will panic if the entry is occupied

source

pub fn into_mut(self) -> Option<&'a mut T>

Returns a mutable reference to the contained type, if this entry is occupied

source

pub fn into_occupied(self) -> Option<OccupiedEntry<'a, T>>

Returns an OccupiedEntry or None if this entry is vacant.

source

pub fn into_vacant(self) -> Option<VacantEntry<'a, T>>

Returns a VacantEntry or None if this entry is occupied.

source

pub fn is_empty(&self) -> bool

Returns whether this Entry is a VacantEntry

source

pub fn insert(self, value: T) -> Option<T>

Insert a value into this Entry.

If the Entry is already an OccupiedEntry, the previously contained value will be returned

source§

impl<'a, T: Default + Send + Sync + 'static> Entry<'a, T>

source

pub fn or_default(self) -> &'a mut T

Ensures a value is in the Entry by inserting the default value if vacant, and returns a mutable reference to the value.

Equivalent to .or_insert_with(Default::default)

§Example
let mut set = type_set::TypeSet::new();
assert_eq!(*set.entry::<&'static str>().or_default(), "");
set.insert("hello");
assert_eq!(*set.entry::<&'static str>().or_default(), "hello");

Trait Implementations§

source§

impl<'a, T: Debug + Any + Send + Sync + 'static> Debug for Entry<'a, T>

source§

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

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

impl<'a, T: Send + Sync + 'static> From<OccupiedEntry<'a, T>> for Entry<'a, T>

source§

fn from(occupied_entry: OccupiedEntry<'a, T>) -> Self

Converts to this type from the input type.
source§

impl<'a, T: Send + Sync + 'static> From<VacantEntry<'a, T>> for Entry<'a, T>

source§

fn from(vacant_entry: VacantEntry<'a, T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, T> Freeze for Entry<'a, T>

§

impl<'a, T> !RefUnwindSafe for Entry<'a, T>

§

impl<'a, T> Send for Entry<'a, T>
where T: Send,

§

impl<'a, T> Sync for Entry<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Entry<'a, T>
where T: Unpin,

§

impl<'a, T> !UnwindSafe for Entry<'a, T>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.