Struct SingletonManager

Source
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

Source

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

Source

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

pub fn get_default<T: 'static, F>( &self, service_name: &str, factory: F, ) -> Result<&'static mut T>
where F: Fn() -> Box<dyn Any> + 'static,

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.

Source

pub fn has(&self, service_name: &str) -> bool

Source

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:

Source

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.

Source

pub fn set_factory<F: 'static + Fn() -> Box<dyn Any>>( &self, service_name: &str, factory: F, ) -> Result<&'static mut Box<dyn Fn() -> Box<dyn Any>>>

Auto Trait Implementations§

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.