[][src]Trait ironsea_index::Record

pub trait Record<K> {
    fn key(&self) -> K;
}

Record behavior used by Indexed implementations.

This trait provides common methods used by index implementations to retrieve information about a single record. This is provided by the users of indices, for each of their struct they wish to index.

Multiple implementation can be provided, as long as their types are different.

Examples

use ironsea_index::Record;

#[derive(Clone, Debug)]
pub struct MyPair {
    a: i64,
    b: i64,
}

impl Record<String> for MyPair {
   fn key(&self) -> String {
       format!("{}", self.a)
   }
}

impl Record<i64> for MyPair {
   fn key(&self) -> i64 {
       self.a
   }
}

fn main() {

   let table = vec![MyPair{ a: 10, b:34}, MyPair{ a: 1, b:56}, MyPair{ a: 2, b:23}];

   // Example without using an actual index crate, we will simply use
   // the Record<K> trait to sort the array of pairs.
   let mut lex_sort = table.clone();
   lex_sort.sort_unstable_by_key(|e| {let k: String = e.key(); k});

   let mut num_sort = table.clone();
   num_sort.sort_unstable_by_key(|e| {let k: i64 = e.key(); k});

   assert_eq!(format!("unsorted {:?}", table),
        "unsorted [MyPair { a: 10, b: 34 }, MyPair { a: 1, b: 56 }, MyPair { a: 2, b: 23 }]");
   assert_eq!(format!("lex sort {:?}", lex_sort),
        "lex sort [MyPair { a: 1, b: 56 }, MyPair { a: 10, b: 34 }, MyPair { a: 2, b: 23 }]");
   assert_eq!(format!("num sort {:?}", num_sort),
        "num sort [MyPair { a: 1, b: 56 }, MyPair { a: 2, b: 23 }, MyPair { a: 10, b: 34 }]");
}

Required methods

fn key(&self) -> K

Extract the key from the record.

Loading content...

Implementors

Loading content...