treerder

Trait Orderable

Source
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§

Source

type Shadow

Type that will represent impl Orderable in internal structures.

For heap allocated types providing raw pointer is sufficient while for stack allocations full reproduction is needed.

Check implementations for &str and String for understandable examples.

Required Methods§

Source

fn chars(&self) -> impl Iterator<Item = char>

Returns Iterator<Item = char> implementation used for ordering.

Source

fn shadow(&self) -> Self::Shadow

Provides backing type to be stored in internal structure.

Source

fn steady(s: Self::Shadow) -> Self

Converts backing back to Self.

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.

Implementations on Foreign Types§

Source§

impl Orderable for String

Source§

fn shadow(&self) -> Self::Shadow

Cloning String is deep copy operation. shadow avoids this.

Source§

type Shadow = String

Source§

fn chars(&self) -> impl Iterator<Item = char>

Source§

fn steady(s: Self::Shadow) -> Self

Source§

impl<'a> Orderable for &'a str

Source§

fn shadow(&self) -> Self::Shadow

It is simple to pass &str around as its cloning is cheap.

Source§

type Shadow = &'a str

Source§

fn chars(&self) -> impl Iterator<Item = char>

Source§

fn steady(s: Self::Shadow) -> Self

Implementors§