pub struct SingletonManager { /* private fields */ }
Expand description
Singleton Manager The container of the singleton managers information. This allows to set aliases to lookup the stored singleton, and allowing for creating a factory function to be set. In the case that the Singleton is never used the factory will stay dormant.
Implementations§
Source§impl SingletonManager
impl SingletonManager
Sourcepub fn instance() -> &'static mut SingletonManager
pub fn instance() -> &'static mut SingletonManager
Getting the instance of the SigneltonManager This will return a static reference to the singleton manager.
use singleton_manager::SingletonManager;
let sm = SingletonManager::instance();
A simple way to get the singleton manager
Sourcepub fn provide(&'static mut self, sp: impl SingletonProvider) -> Result<()>
pub fn provide(&'static mut self, sp: impl SingletonProvider) -> Result<()>
Implementation of provider sets
NOTE: This is currently being implemented and the expectation is that the singleton manager will be moving more and more towards using the provider implementation in the future.
This allows you to implement a SingletonProvider
trait for a struct
and form there just
service the implemented struct to the Singleton Manager.
The Singleton Manager can then on a need to need basis either get the singleton from its own
storage or create the service in its own storage before serving it. Giving the
Singleton Manager total access over when a service should be created and reused.
Usage:
use singleton_manager::{SingletonManager, SingletonProvider};
use std::sync::Mutex;
struct MyService{
message: String,
guard: Mutex<()>
};
impl MyService {
pub fn set(&mut self, msg: &str) {
let mut _guard = self.guard.lock().expect("Failed to get guard");
self.message = msg.to_string();
}
pub fn get(&self) -> String {
let _guard = self.guard.lock();
self.message.clone()
}
}
impl SingletonProvider for MyService {
type Output = MyService;
type Error = String;
fn service() -> Result<&'static mut Self::Output, Self::Error> {
SingletonManager::instance().get::<Self::Output>("my_service").map_err(|_| "err".to_string())
}
fn get_name(&self) -> &'static str {
"my_service"
}
fn get_service(&self) -> Result<Self::Output, Self::Error> {
Ok(MyService{
message: "".to_string(),
guard: Mutex::new(()),
})
}
}
SingletonManager::instance().provide(MyService {
message: "".to_string(),
guard: Mutex::new(()),
});
Sourcepub fn get_default<T: 'static, F>(
&self,
service_name: &str,
factory: F,
) -> Result<&'static mut T>
pub fn get_default<T: 'static, F>( &self, service_name: &str, factory: F, ) -> Result<&'static mut T>
get with default,
This will get a singleton from the singleton manager. If the singleton does not exist it will automatically create it from the default factory function and then store the build singleton.
pub fn has(&self, service_name: &str) -> bool
Sourcepub fn get<T: 'static>(
&'static mut self,
service_name: &str,
) -> Result<&'static mut T>
pub fn get<T: 'static>( &'static mut self, service_name: &str, ) -> Result<&'static mut T>
Getting a singleton from the singleton manager.
This allow you to get a certain singleton from the singleton manager.
This will automatically try to downcast the singleton to the expected object, if the
downcast failes it will return an Error FailedToDowncastRefOfService([Service_name])
to let you know that the downcast failed for the sytsem.
To use this just use the following code:
use singleton_manager::SingletonManager;
use singleton_manager::sm;
struct MyService{};
sm().set("my_service", MyService {}).unwrap();
let service = sm().get::<MyService>("my_service")
.expect("Failed to get service");
this will give you the my_service
that have been set previously.
A full example of its usage can be found here:
Sourcepub fn set<T: 'static>(
&self,
service_name: &str,
service: T,
) -> Result<&'static mut T>
pub fn set<T: 'static>( &self, service_name: &str, service: T, ) -> Result<&'static mut T>
Setting a specific service/object as a singleton. This is used when setting a service or other to a singleton.