[go: up one dir, main page]

Enum Entry

Source
pub enum Entry<'a, V> {
    Occupied(OccupiedEntry<'a, V>),
    Vacant(VacantEntry<'a, V>),
}
Expand description

A view into a single entry in a table, which may either be vacant or occupied.

This enum is constructed from ExtractMap::entry.

Variants§

§

Occupied(OccupiedEntry<'a, V>)

An occupied entry.

§

Vacant(VacantEntry<'a, V>)

A vacant entry.

Implementations§

Source§

impl<'a, V> Entry<'a, V>

Source

pub fn insert(self, value: V) -> OccupiedEntry<'a, V>

Sets the value of the entry, replacing any existing value if there is one, and returns an OccupiedEntry.

§Example
use extract_map::ExtractMap;

let mut map: ExtractMap<u64, User> = ExtractMap::new();
map.insert(User { id: 1, name: "Cat" });

let entry = map.entry(&1).insert(User { id: 1, name: "Fox" });
assert_eq!(entry.get(), &User { id: 1, name: "Fox" });
Source

pub fn or_insert(self, default: V) -> OccupiedEntry<'a, V>

Ensures a value is in the entry by inserting if it was vacant.

Returns an OccupiedEntry pointing to the now-occupied entry.

§Example
use extract_map::ExtractMap;

let mut map: ExtractMap<u64, User> = ExtractMap::new();

// Inserts new entry, as the map is empty.
let entry = map.entry(&1).or_insert(User { id: 1, name: "Fox" });
assert_eq!(entry.get(), &User { id: 1, name: "Fox" });

// Does not insert new entry, as there is already a user with ID 1.
let entry = map.entry(&1).or_insert(User { id: 1, name: "Cat" });
assert_eq!(entry.get(), &User { id: 1, name: "Fox" });
Source

pub fn or_insert_with(self, default: impl FnOnce() -> V) -> OccupiedEntry<'a, V>

Ensures a value is in the entry by inserting the result of the function if it was vacant.

Returns an OccupiedEntry pointing to the now-occupied entry.

§Example
use extract_map::ExtractMap;

let mut map: ExtractMap<u64, User> = ExtractMap::new();

// Inserts new entry, as the map is empty.
let entry = map.entry(&1).or_insert_with(|| User { id: 1, name: "Fox" });
assert_eq!(entry.get(), &User { id: 1, name: "Fox" });

// Does not insert new entry, as there is already a user with ID 1.
let entry = map.entry(&1).or_insert_with(|| User { id: 1, name: "Cat" });
assert_eq!(entry.get(), &User { id: 1, name: "Fox" });
Source

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

Provides in-place mutable access to an occupied entry, does nothing for a vacant entry.

§Example
use extract_map::ExtractMap;

let mut map: ExtractMap<u64, User> = ExtractMap::new();

map.insert(User { id: 1, name: "Cat"});
map.entry(&1).and_modify(|user| user.name = "Fox");

assert_eq!(map.get(&1), Some(&User { id: 1, name: "Fox"}));

Trait Implementations§

Source§

impl<'a, V: Debug> Debug for Entry<'a, V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

impl<'a, V> RefUnwindSafe for Entry<'a, V>
where V: RefUnwindSafe,

§

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

§

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

§

impl<'a, V> Unpin for Entry<'a, V>

§

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

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

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.