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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! impls that don't need `unsafe` and aren't relied on by unsafe
//!
//! Also, note that the methods here are ordered *after* the ones in `lib.rs`.

#![forbid(unsafe_code)]

use super::*;

// import this here, to avoid confusion with atomics in the unsafe parts
use core::cmp::Ordering;

impl<T, const N: usize> Default for FixedCapacityVec<T, N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T, const N: usize> Debug for FixedCapacityVec<T, N>
where
    T: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.deref().fmt(f)
    }
}

impl<T, const N: usize> Clone for FixedCapacityVec<T, N>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        let mut out = Self::new();
        for item in &**self {
            out.push(item.clone())
        }
        out
    }
}

impl<T, const N: usize> Hash for FixedCapacityVec<T, N>
where
    T: Hash,
{
    fn hash<H>(&self, h: &mut H)
    where
        H: std::hash::Hasher,
    {
        (**self).hash(h)
    }
}

macro_rules! impl_comparison_trait { { $Trait:ident $( $fn:ident $result:ty )? } => {
    impl<T, const N: usize> $Trait for FixedCapacityVec<T, N>
    where
        T: $Trait,
    {
        $(
            fn $fn(&self, other: &Self) -> $result {
                (**self).$fn(&**other)
            }
        )?
    }
} }

// Limited version, with just Self as RHS, mostly so we can be `Ord`
impl_comparison_trait! { PartialEq eq bool }
impl_comparison_trait! { PartialOrd partial_cmp Option<Ordering> }
impl_comparison_trait! { Ord cmp Ordering }
impl_comparison_trait! { Eq }

/// Error returned by [`try_push_or_discard()`](FixedCapacityVec::try_push_or_discard)
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct FullError;

impl Display for FullError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt("pushed to a full FixedCapacityVec", f)
    }
}