Trait shardio::SortKey [−][src]
Expand description
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)
}
}