Trait copse::SortableBy
source · pub trait SortableBy<O: ?Sized + TotalOrder> {
// Required method
fn sort_key(&self) -> &O::OrderedType;
}
Expand description
A type that is sortable by its sort_key
under total orders of type parameter O
.
Note that if you wish to use O::OrderedType
itself with copse’s collections, you
must explicitly implement SortableBy<O>
for it even though that implementation will
typically be a no-op. This case cannot currently be provided for you by way of a
blanket implementation because that would conflict with the blanket borrowing
implementation that is provided for the default OrdTotalOrder
; implementations
for O::OrderedType
that are not no-ops are strongly discouraged, as they are prone
to causing considerable confusion—for any such use-case, consider defining a distinct
TotalOrder
instead.
Example
use copse::{BTreeSet, SortableBy, TotalOrder};
use std::cmp::Ordering;
struct MyOrdering;
impl TotalOrder for MyOrdering {
type OrderedType = str;
fn cmp(&self, this: &str, that: &str) -> Ordering { this.cmp(that) }
}
impl SortableBy<MyOrdering> for String {
fn sort_key(&self) -> &str { self.as_str() }
}
impl SortableBy<MyOrdering> for str {
fn sort_key(&self) -> &str { self }
}
let mut set = BTreeSet::new(MyOrdering);
set.insert(String::from("a"));
assert!(set.contains("a"));
Required Methods§
sourcefn sort_key(&self) -> &O::OrderedType
fn sort_key(&self) -> &O::OrderedType
Extract the sort key by which self
is ordered under total orders of type O
.