#testing

macro singletonum-derive

singleton derive macro (this is an internal crate, you don't need to connect it explicitly)

2 unstable releases

Uses old Rust 2015

0.2.0 Nov 14, 2018
0.1.0 Nov 14, 2018

#2053 in #testing

23 downloads per month
Used in singletonum

MIT license

3KB

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

~2MB
~49K SLoC