Struct singleton_manager::SingletonManager[][src]

pub struct SingletonManager { /* fields omitted */ }
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

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

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(()),
});

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.

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:

Setting a specific service/object as a singleton. This is used when setting a service or other to a singleton.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.