2 unstable releases

Uses old Rust 2015

0.2.0 Nov 14, 2018
0.1.0 Nov 14, 2018

#36 in #singleton

MIT license

2KB

Singleton Derive

Crates.io

Documentation

This crate provides a convenient API to make a singleton from any struct with just a #[derive(Singleton)]

Example

extern crate singletonum;
use singletonum::{Singleton, SingletonInit};

#[derive(Debug, PartialEq, Singleton)]
struct SampleSingleton {
    inner: String,
}

impl SingletonInit for SampleSingleton {
    type Init = String;
    fn init(init: &String) -> Self {
        SampleSingleton { inner: init.clone() }
    }
}

#[cfg(test)]
mod tests {
    use super::{SampleSingleton, Singleton};

    #[test]
    fn initialize_and_get() {
        SampleSingleton::get_instance(&String::from("hello, world!"));
        let instance = SampleSingleton::get_instance(&String::from("hello, world!"));
        assert_eq!(
            instance,
            &SampleSingleton { inner: String::from("hello, world!") }
        );
    }
}

Contribution

This crate is not yet complete, suggestions for improvements are welcome in issues or via authors email. PRs are welcome also.

Dependencies

~4MB
~74K SLoC