static-instance

Crates.iostatic-instance
lib.rsstatic-instance
version0.1.1
sourcesrc
created_at2020-10-08 05:56:19.687645
updated_at2020-10-09 04:31:49.788442
descriptionCreate mutable static instances of an object
homepage
repositoryhttps://github.com/Stateford/static-instance-rs
max_upload_size
id297160
size5,092
Dimitri Apostal (Stateford)

documentation

README

static-instance

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.

NOTE

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

}

Commit count: 17

cargo fmt