[][src]Trait shardio::SortKey

pub trait SortKey<T> {
    type Key: Ord + Clone;
    fn sort_key(t: &T) -> Cow<Self::Key>;
}

Specify a key function from data items of type T to a sort key of type Key. Impelment this trait to create a custom sort order. The function sort_key returns a Cow so that we abstract over Owned or Borrowed data.

 
extern crate shardio;
use shardio::*;
use std::borrow::Cow;
 
// The default sort order for DataStruct will be by field1.
#[derive(Ord, PartialOrd, Eq, PartialEq)]
struct DataStruct {
    field1: usize,
    field2: String,
}
 
// Define a marker struct for your new sort order
struct Field2SortKey;
 
// Define the new sort key by extracting field2 from DataStruct
impl SortKey<DataStruct> for Field2SortKey {
    type Key = String;
 
    fn sort_key(t: &DataStruct) -> Cow<String> {
        Cow::Borrowed(&t.field2)
    }
}

Associated Types

type Key: Ord + Clone

Loading content...

Required methods

fn sort_key(t: &T) -> Cow<Self::Key>

Loading content...

Implementors

impl<T> SortKey<T> for DefaultSort where
    T: Ord + Clone
[src]

type Key = T

Loading content...