[][src]Macro sortedvec::sortedvec

macro_rules! sortedvec {
    (
    $(#[$attr:meta])*
    $v:vis struct $name:ident {
        fn derive_key($i:ident : & $val:ty) -> $key:ty {
            $keyexpr:expr
        } $(,)?
    }
) => { ... };
}

A macro that defines a sorted vector data structure.

The generated struct is specific to the given keys and value types. To create the struct, four bits are required:

  • a struct name,
  • a value type,
  • a key type. Since we will sort on these internally, this type must implement Ord,
  • a key extraction function of type FnMut(&T) -> K.

It matches the following input:

$(#[$attr:meta])*
$v:vis struct $name:ident {
    fn derive_key($i:ident : & $val:ty) -> $key:ty {
        $keyexpr:expr
    } $(,)?
}

To get an overview of the exposed methods on the generated structure, see the documentation of the example module.

Example

use sortedvec::sortedvec;

/// Example key
#[derive(PartialOrd, Ord, PartialEq, Eq, Debug, Clone, Copy)]
pub struct K;

/// Example value
#[derive(Debug, Clone)]
pub struct T {
    key: K,
}

sortedvec! {
    /// Sorted vector type that provides quick access to `T`s through `K`s.
    #[derive(Debug, Clone)]
    pub struct ExampleSortedVec {
        fn derive_key(t: &T) -> K { t.key }
    }
}

let sv = ExampleSortedVec::default();