pub struct Key<V> { /* private fields */ }
Expand description
A type-safe key used to fetch values from the value store.
Construction
A key can be constructed only by calling State::insert
.
The state assigns a new numerical ID to the inserted value, which is unique throughout
the running of the program.
This ensures type safety, as explained below.
Type Safety
These keys are type-safe in a sense that a key used to insert a value of type T
cannot be
used to access a value of another type U
. An attempt to do so will result in a compile error.
It is achieved by having the key generic over T
. However, T
is just a marker, and no
values of type T
are stored internally.
ⓘ
let mut state = State::default();
let id = state.insert(String::from("1"));
let _: Option<i32> = state.remove(id); // Error!
let _ = state.remove::<i32>(id); // Error!
Trait Implementations
impl<T> Copy for Key<T>
impl<V: Eq> Eq for Key<V>
impl<V> StructuralEq for Key<V>
impl<V> StructuralPartialEq for Key<V>
Auto Trait Implementations
impl<V> RefUnwindSafe for Key<V> where
V: RefUnwindSafe,
impl<V> Send for Key<V> where
V: Send,
impl<V> Sync for Key<V> where
V: Sync,
impl<V> Unpin for Key<V> where
V: Unpin,
impl<V> UnwindSafe for Key<V> where
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more