[][src]Macro sortedvec::def_sorted_vec

macro_rules! def_sorted_vec {
    ( $(#[$attr:meta])* $v:vis struct $name:ident: $val:ty => $key:ty, $keygen:expr ) => { ... };
}

A macro that defines a sorted vector data collection.

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.

Matches the following input:

( $(#[$attr:meta])* $v:vis struct $name:ident: $val:ty => $key:ty, $keygen:expr )

Example

use sortedvec::def_sorted_vec;
 
/// Example key
#[derive(PartialOrd, Ord, PartialEq, Eq, Debug, Clone, Copy)]
pub struct K;
 
/// Example value
#[derive(Debug, Clone)]
pub struct T {
    key: K,
}
 
fn key(t: &T) -> K { t.key }
 
def_sorted_vec! {
    /// Sorted vector type that provides quick access to `T`s through `K`s.
    #[derive(Debug, Clone)]
    pub struct ExampleSortedVec: T => K, key
}
 
let sv = ExampleSortedVec::default();