static-instance 0.1.1

Create mutable static instances of an object
Documentation
  • Coverage
  • 0%
    0 out of 6 items documented0 out of 4 items with examples
  • Size
  • Source code size: 4.72 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.05 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Stateford/static-instance-rs
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Stateford

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

}