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>
impl<T> Entry<T>
Sourcepub fn as_generic(self) -> Entry
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?
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}Sourcepub fn write(&self) -> Option<EntryWriteGuard<'_, T>>
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?
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}Sourcepub fn read(&self) -> Option<EntryReadGuard<'_, T>>
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.