Skip to main content

Entry

Struct Entry 

Source
pub struct Entry<T = ()> { /* private fields */ }
Expand description

Entry controls the lifetime of an entry in the registry. When the entry has its original type definition, you can also use it to access the stored object. See crate::registry::Registry::register().

Implementations§

Source§

impl<T> Entry<T>
where T: Send + Sync,

Source

pub fn as_generic(self) -> Entry

Removes the type definition from the Entry. This method is useful when you want to store different kinds of Entry in one collection.

Examples found in repository?
examples/registry_extension.rs (line 11)
5fn main() {
6    // Original website setup
7    let menu = Registry::<MenuItem>::new();
8    let styles = Registry::<StyleSheet>::new();
9    let mut website_store = vec![];
10
11    website_store.push(menu.register(MenuItem("Home")).as_generic());
12    website_store.push(menu.register(MenuItem("About")).as_generic());
13    website_store.push(styles.register(StyleSheet("website.css")).as_generic());
14
15    print_state("Original website", &menu, &styles);
16
17    // Loading an extension which registers new resources
18    let mut extension_store = vec![];
19    extension_store.push(menu.register(MenuItem("Weather")).as_generic());
20    extension_store.push(menu.register(MenuItem("News")).as_generic());
21    extension_store.push(styles.register(StyleSheet("extension.css")).as_generic());
22
23    print_state("After extension is loaded", &menu, &styles);
24
25    // Unloading the extension
26    drop(extension_store);
27
28    print_state("After extension is unloaded", &menu, &styles);
29}
Source

pub fn write(&self) -> Option<EntryWriteGuard<'_, T>>

Grants mutable access to the entry. It locks the shared RwLock of the crate::registry::Registry. Blocks the current thread until the lock can be acquired!

§Return

None if the crate::registry::Registry no longer exists.

Examples found in repository?
examples/registry_mutation.rs (line 17)
3fn main() {
4    let registry = Registry::<i32>::new();
5    let entry1 = registry.register(0);
6    let entry2 = registry.register(100);
7
8    println!("{:?}", registry); // Prints: {0: 0, 1: 100}
9
10    println!("Mutation via Registry...");
11    for (_, value) in registry.write().iter_mut() {
12        *value += 1;
13    }
14    println!("{:?}", registry); // Prints: {0: 1, 1: 101}
15
16    println!("Mutation via typed Entry...");
17    *entry1.write().unwrap().get_mut() += 10;
18    *entry2.write().unwrap().get_mut() += 10;
19    println!("{:?}", registry); // Prints: {0: 11, 1: 111}
20}
Source

pub fn read(&self) -> Option<EntryReadGuard<'_, T>>

Grants shared read access to the entry. It locks the shared RwLock of the crate::registry::Registry. Blocks the current thread until the lock can be acquired!

§Return

None if the crate::registry::Registry no longer exists.

Source

pub fn get_id(&self) -> EntryId

Gets the underlying id of the entry.

Source

pub unsafe fn leak(self)

Leaks the entry.
⚠️ In production environments you should never use this method. It’s only meant for quick prototyping or debugging.

Trait Implementations§

Source§

impl Debug for Entry

Source§

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

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

impl<T> Drop for Entry<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Entry<T>

§

impl<T> RefUnwindSafe for Entry<T>
where T: RefUnwindSafe,

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for Entry<T>

§

impl<T> UnwindSafe for Entry<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> 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.