Crates.io | static-instance |
lib.rs | static-instance |
version | 0.1.1 |
source | src |
created_at | 2020-10-08 05:56:19.687645 |
updated_at | 2020-10-09 04:31:49.788442 |
description | Create mutable static instances of an object |
homepage | |
repository | https://github.com/Stateford/static-instance-rs |
max_upload_size | |
id | 297160 |
size | 5,092 |
The static-instance
crate provides a macro used to create safe
static instances of objects. This was created in order to replicate
the behavior of a static class member used frequently in C++. This
allows exporting of rust to C via FFI a whole lot easier when the
library needs to keep track of states.
It is still up to you to use the data inside the instance safely. The data inside the instance is not thread safe unless you make it thread safe.
#[macro_use]
use static_instance::{Instance, New};
use std::sync::{Arc, Mutex};
#[derive(Clone)]
struct SimpleCounter {
counter: Arc<Mutex<Box<i32>>>
}
impl SimpleCounter {
fn add(&mut self, value: i32) {
let mut data = self.count.lock().unwrap();
*data += 1;
}
fn get_value(&self) -> i32 {
let data: i32 = self.count.lock().unwrap();
return (*data).clone();
}
fn print(&self) {
let data: i32 = self.count.lock().unwrap();
println!("{}", *data);
}
}
impl New for SimpleCounter {
fn new() -> SimpleCounter {
SimpleCounter {
counter: Arc::new(Mutex::new(Box::new(0)))
}
}
CreateInstance!(SimpleCounter);
fn main() {
SimpleCounter::instance().add(30);
SimpleCounter::instance().print();
}