singletonset 0.1.0

This crate provides the `SingletonSet` data structure, which makes it easy to store a single instance each of various types within a single set.
Documentation
  • Coverage
  • 95.24%
    20 out of 21 items documented3 out of 21 items with examples
  • Size
  • Source code size: 32.27 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.09 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 16s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • FlippingBinary

This crate provides the SingletonSet data structure, which makes it easy to store a single instance each of various types within a single set.

This data structure can be used to create a locally-scoped Singleton out of any data types within it. It ensures there is only one instance of any type, similar to a Singleton, without polluting the global scope.

Features

  • Type Safety: Ensures that only one value per type is present in the set, using Rust's type system and std::any::TypeId.
  • Flexible Initialization: Types that implement Default can be automatically initialized, or any type can be initialized from a value or closure for fully customizable initialization.

Example Usage

use singletonset::SingletonSet;

fn main() {
    let mut set = SingletonSet::new();

    // Initialize a u32 value from its default value
    set.get_mut::<u32>();

    // Initialize a String using a closure
    set.get_or_insert_with_mut(|| "Hello".to_string());

    // Access and modify values
    *set.get_mut::<u32>() = 42;
    *set.get_mut::<String>() += ", World!";

    // The type must never be ambiguous, but can be inferred.
    *set.get_mut() = 35.77f64;

    // Initialization functions have no effect on existing values
    set.get_or_insert_with_mut(|| "Goodbye".to_string());

    println!("u32: {}, f64: {}, String: {}",
      set.get::<u32>(),
      set.get::<f64>(),
      set.get::<String>()
    );
}

Installation

Use cargo add singletonset or add the following dependency to your Cargo.toml file:

[dependencies]
singletonset = "0.1"

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any suggestions, bug reports, or feature requests.

License

Licensed under either of the Apache License, Version 2.0 or the MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.