pub trait KeyComponent: Ord + Eq + Clone {}
impl<T: Ord + Eq + Clone> KeyComponent for T {}
pub trait Key: ToOwned {
type Component: KeyComponent;
fn as_slice(&self) -> &[Self::Component];
fn from_vec(v: Vec<Self::Component>) -> Self::Owned;
}
impl Key for str {
type Component = u8;
fn as_slice(&self) -> &[u8] {
self.as_bytes()
}
fn from_vec(v: Vec<u8>) -> String {
unsafe {
String::from_utf8_unchecked(v)
}
}
}
impl<T: KeyComponent> Key for [T] {
type Component = T;
fn as_slice(&self) -> &[T] {
self
}
fn from_vec(v: Vec<T>) -> Vec<T> {
v
}
}
pub trait ExtensibleKey: ToOwned {
type Component: KeyComponent;
fn as_slice(&self) -> &[Self::Component];
fn from_vec(v: Vec<Self::Component>) -> Self::Owned;
}
impl<T: ExtensibleKey> Key for T {
type Component = T::Component;
fn as_slice(&self) -> &[Self::Component] {
self.as_slice()
}
fn from_vec(v: Vec<Self::Component>) -> Self::Owned {
T::from_vec(v)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_can_be_extended() {
#[derive(Clone)]
struct Wrapper(Vec<()>);
impl ExtensibleKey for Wrapper {
type Component = ();
fn as_slice<'a>(&'a self) -> &'a [()] {
&self.0
}
fn from_vec(v: Vec<Self::Component>) -> Self::Owned {
Wrapper(v)
}
}
use set::RadixSet;
let mut s = RadixSet::<Wrapper>::new();
s.insert(&Wrapper(vec![()]));
}
}