1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::VecShard;
use std::error::Error;
use std::fmt::{self, Debug, Display};

/// A generic merge error.
///
/// This exists because the merge fns take ownership of their input shards, and you may want your shards back upon error.
#[derive(Debug)]
pub struct CantMerge<T, E> {
    pub left: VecShard<T>,
    pub right: VecShard<T>,
    pub reason: E,
}

/// A reason why an in-place merge was unsuccesful.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum WouldMove {
    DifferentAllocations,
    NotAdjacent,
    WrongOrder,
}

/// A reason why a no-alloc merge was unsuccesful.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum WouldAlloc {
    DifferentAllocations,
    OtherShardsLeft,
}

impl Display for WouldMove {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use WouldMove::*;
        write!(
            f,
            "the two shards are {}",
            match self {
                DifferentAllocations => "not from the same memory allocation.",
                NotAdjacent => "not directly adjacent in memory.",
                WrongOrder => "adjacent, but were passed in the reverse order.",
            }
        )
    }
}

impl Display for WouldAlloc {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use WouldAlloc::*;
        write!(
            f,
            "the two shards are {}",
            match self {
                DifferentAllocations => "not from the same memory allocation.",
                OtherShardsLeft => "not directly adjacent in memory and can't be moved around because there are still other shards in the Vec",
            }
        )
    }
}

impl<T, R: Display> Display for CantMerge<T, R> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Can't perform quick merge because {}", self.reason)
    }
}

impl<T: Debug, R: Debug + Display> Error for CantMerge<T, R> {}