Crates.io | singleton-manager |
lib.rs | singleton-manager |
version | 0.1.4 |
source | src |
created_at | 2021-08-16 15:20:05.621698 |
updated_at | 2023-01-23 06:15:10.374742 |
description | A programatical singleton manager |
homepage | |
repository | https://github.com/nebula-technologies/singleton-manager |
max_upload_size | |
id | 437999 |
size | 51,850 |
Is a management service for Singleton Services. If there is a need for a service to keep its state across multiple threads, this singleton service can pull the services from a singleton storage.
Note: Singleton Manager itself is not thread-safe, so you need to make your service threadsafe.
Say we want to use a custom struct
as our singleton:
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()
}
}
if this is our service and we would now want to store it as a singleton, we can do this simply by:
SingletonManager::instance().set(
"my_service",
"MyService",
None,
Some(MyService {
message: "".to_string(),
guard: Mutex::new(()),
}),
)
.ok();
this will set the service and is now retrivable by using:
let service = SingletonManager::instance()
.get::<MyService>("my_service")
.expect("Failed to get service");
service.set("My Message");
let different_service = SingletonManager::instance()
.get::<MyService>("my_service")
.expect("Failed to get service");
assert_eq!("My Message".to_string(), different_service.get());
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()
}
}
fn main() {
SingletonManager::instance().set(
"my_service",
"MyService",
None,
Some(MyService {
message: "".to_string(),
guard: Mutex::new(()),
}),
)
.ok();
let service = SingletonManager::instance()
.get::<MyService>("my_service")
.expect("Failed to get service");
service.set("My Message");
let different_service = SingletonManager::instance()
.get::<MyService>("my_service")
.expect("Failed to get service");
assert_eq!("My Message".to_string(), different_service.get());
}
It's possible to instantiate only on request by using a factory to create the service
SingletonManager::instance()
.set_factory(
"my_service_factory",
Box::new(|| {
Box::new(MyService {
message: "".to_string(),
guard: Mutex::new(()),
})
}),
)
.ok();
this will set the service and is now retrivable by using:
fn main() {
set_service();
let service = SingletonManager::instance()
.get::<MyService>("my_service_factory")
.expect("Failed to get service");
service.set("My Message");
let different_service = SingletonManager::instance()
.get::<MyService>("my_service_factory")
.expect("Failed to get service");
assert_eq!("My Message".to_string(), different_service.get());
}
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()
}
}
fn main() {
SingletonManager::instance()
.set_factory(
"my_service_factory",
Box::new(|| {
Box::new(MyService {
message: "".to_string(),
guard: Mutex::new(()),
})
}),
)
.ok();
let service = SingletonManager::instance()
.get::<MyService>("my_service_factory")
.unwrap();
service.set("My Message");
let different_service = SingletonManager::instance()
.get::<MyService>("my_service_factory")
.unwrap();
assert_eq!("My Message".to_string(), different_service.get());
}
Contributions are currently not opened as this is running from a private server. Issues can be opened at any time with a guest account on gitlab.nebula.technology.