Expand description

Singleton Manager

A singleton manger for handling and holding singletons in a system

build Purpose

This was build because of multiple minor libraries that was either using lazy_static! or other macros to handle late initializations of singletons in system.

Example of such libraries

  1. Database Pool Manager, that though out the entire services lifetime needs to continue running to track the number of active connections.
  2. Logging, a logging library that collects and tracks calls through out the entire execution of a single thread, for then later to flush all the logs as a collection of a single request.
  3. Worker Queue system, a worker queue system that needs to track each individual execution, and where new executions can be dynamically added to it while running.

Previously the applications were using lazy_static! but lazy_static! is using unsafe modify on each activation. To reduce the failurepoints this system is also using unsafe but only in one place to minimize impact, on top of that it is programmatically accessible and modifiable. Allowing you to create object on the fly when needed.

A full example of how to use this:

use singleton_manager::sm;
use std::sync::Mutex;

pub 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()
    }
}

sm().set("my_service",
    MyService {
        message: "".to_string(),
        guard: Mutex::new(()),
    }).ok();

let service = sm()
    .get::<MyService>("my_service")
    .expect("Failed to get service");
service.set("My Message");

let different_service = sm()
    .get::<MyService>("my_service")
    .expect("Failed to get service");

assert_eq!("My Message".to_string(), different_service.get());

Structs

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.

Enums

Traits

Functions

Type Definitions

Common Result used in the library.