Macro blazemap::define_key_wrapper_bounded

source ·
macro_rules! define_key_wrapper_bounded {
    (
        $(#[$attrs:meta])*
        $vis:vis
        struct $new_type:ident($orig_type:ty);
        MAX_CAP = $capacity:literal
        $(; Derive(as for Original Type): {$($to_derive_orig:ident),+ $(,)?} )?
        $(; Derive(as for usize):         {$(  $to_derive_sn:ident),+ $(,)?} )?
        $(;)?
    ) => { ... };
    (
        $(#[$attrs:meta])*
        $vis:vis
        struct $new_type:ident($orig_type:ty);
        MAX_CAP = $capacity:literal
        $(; Derive(as for usize):         {$(  $to_derive_sn:ident),+ $(,)?} )?
        $(; Derive(as for Original Type): {$($to_derive_orig:ident),+ $(,)?} )?
        $(;)?
    ) => { ... };
}
Expand description

Creates a new type that acts as an usize-based replacement for the old type that can be used as a key for blazemap collections. Being an analogue of define_key_wrapper for the case when the user could statically guarantee that the number of unique keys doesn’t exceed MAX_CAP, it’s optimized for read operations so that they don’t create any multi-thread contention.

This macro supports optional inference of standard traits using the following syntax:

  • Derive(as for Original Type) — derives traits as for the original type for which blazemap_key is being registered. This method supports inference of the following traits:
    • Default
    • PartialOrd (mutually exclusive with Ord)
    • Ord (also derives PartialOrd, so mutually exclusive with PartialOrd)
    • Debug
    • Display
    • Serialize (with serde feature only)
    • Deserialize (with serde feature only)
  • Derive(as for usize) — derives traits in the same way as for the serial number assigned when registering an instance of the original type the first time BlazeMapIdWrapper::new was called. Methods inferred by this option do not incur any additional overhead compared to methods inferred for plain usize. This method supports inference of the following traits:
    • PartialOrd (mutually exclusive with Ord)
    • Ord (also derives PartialOrd, so mutually exclusive with PartialOrd)

§Example

use blazemap::prelude::{BlazeMap, define_key_wrapper_bounded};

define_key_wrapper_bounded! {
    pub struct Key(&'static str);
    MAX_CAP = 40_000;
    Derive(as for Original Type): {  // Optional section
        Debug,
        Display,
    };
    Derive(as for usize): {          // Optional section
        Ord,
    }
}

let key_1 = Key::new("first");
let key_2 = Key::new("second");
let key_3 = Key::new("third");

let mut map = BlazeMap::new();
map.insert(key_2, "2");
map.insert(key_1, "1");
map.insert(key_3, "3");

assert_eq!(format!("{map:?}"), r#"{"first": "1", "second": "2", "third": "3"}"#)