pub trait Orderable {
type Shadow;
// Required methods
fn chars(&self) -> impl Iterator<Item = char>;
fn shadow(&self) -> Self::Shadow;
fn steady(s: Self::Shadow) -> Self;
}Expand description
impl Orderable is type which is ordable by its
char sequence representation.
Careful implementation needed. When wrongly implemented,
various errors can occur including process aborting.
See Shadow associated type for more information.
Craft behing this is that Orderable trait allows for perfomance
optimization by avoiding excessive scenarios like deep cloning and others.
Check this phantasmal implementation for usize.
Orderable implementation cannot work without providing Treerder with
appropriate function for alphabet instantiation same as function for char
index generation. Check english_letters::ab and english_letters::ix.
use treerder::{Orderable, Treerder, Alphabet, ab as ab_fn};
#[derive(Debug, PartialEq)]
struct LocalUsize(usize);
struct LocalUsizeCharIterator {
c: char,
x: bool,
}
impl Iterator for LocalUsizeCharIterator {
type Item = char;
fn next(&mut self) -> Option<char> {
if self.x {
self.x = false;
Some(self.c)
} else {
None
}
}
}
impl Orderable for LocalUsize {
type Shadow = usize;
fn chars(&self) -> impl Iterator<Item = char> {
let c = self.0.to_string().chars().next().unwrap();
LocalUsizeCharIterator { c, x: true }
}
fn shadow(&self) -> Self::Shadow {
self.0
}
fn steady(s: Self::Shadow) -> Self {
LocalUsize(s)
}
}
fn ix(c: char) -> usize {
c.to_digit(10).unwrap() as usize
}
fn ab() -> Alphabet<LocalUsize> {
ab_fn::<LocalUsize>(10)
}
let mut nums = [9, 8, 7, 5, 3, 1].map(|x| LocalUsize(x));
let mut orderer = Treerder::<LocalUsize>::new_with(ix, ab);
orderer.order(&mut nums);
let proof = [1, 3, 5, 7, 8, 9].map(|x| LocalUsize(x));
assert_eq!(proof, nums);Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.