Advanced Architecture (ADAR)
Adar is a collection of architectural tools that help you write more readable and performant code.
Disclaimer: This crate uses some
unsafecode. Please refer to the comments in the source code for details. (PRs are welcome to convert it to safe code)
Registry
Registry is a container that lets you control the lifetime of elements through an Entry struct returned after calling Registry::register(). Entry cannot be cloned, but it can be cast to a generic type using Entry::as_generic(), which makes it possible to store entries from multiple registries in a single container. Registry can be cloned and behaves like an Arc. Whenever the data is mutated, an internal RwLock is locked. You can also run code when an element is removed by using the set_remove_callback() callback.
Example
use *;
;
;
// All of the different kind of resources registered by the extension are unloaded here
Original website
Menu:
0: Home
1: About
StyleSheets:
0: website.css
After extension is loaded
Menu:
0: Home
1: About
2: Weather
3: News
StyleSheets:
0: website.css
1: extension.css
After extension is unloaded
Menu:
0: Home
1: About
StyleSheets:
0: website.css
RegistryMap
RegistryMap is similar to Registry. But you need to identify each element in the registry with a key. The key need to be provided during register() and you can later get the elements using get() or get(). RegistryMap uses a BTreeMap internally.
Example
use *;
;
Getting user
Event
Event is a lightweight wrapper around Registry. It provides an implementation of an event/observer architecture.
Please note that during event dispatch the Registry remains locked. This means that you cannot add elements to the registry from the callbacks. Also keep your observers lightweight!
Example
use *;
Observer #1 called: (1, "First event")
Observer #2 called: (1, "First event")
Observer #1 called: (2, "Second event")
TracedRegistry
TracedRegistry is an extension of Registry. It enables you to register multiple observers that handle registering or unregistering elements.
Example
use *;
Register, 0, foo
Register, 1, bar
UnRegister, 0, foo
Register, 2, baz
UnRegister, 1, bar
UnRegister, 2, baz