Macro blazemap::define_key_wrapper

source ·
macro_rules! define_key_wrapper {
    (
        $(#[$attrs:meta])*
        $vis:vis
        struct $new_type:ident($orig_type:ty)
        $(; 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)
        $(; 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.

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. Each call to methods on these traits requires an additional .read call on the internal synchronization primitive, so — all other things being equal — their calls may be less optimal than the corresponding calls on instances of the original key’s type. 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. Because methods inferred by this option do not require additional locking on synchronization primitives, they 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};

define_key_wrapper! {
    pub struct Key(&'static str);
    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"}"#)