fixed_capacity_vec/
safe.rs

1//! impls that don't need `unsafe` and aren't relied on by unsafe
2//!
3//! Also, note that the methods here are ordered *after* the ones in `lib.rs`.
4
5#![forbid(unsafe_code)]
6
7use super::*;
8
9// import this here, to avoid confusion with atomics in the unsafe parts
10use core::cmp::Ordering;
11
12impl<T, const N: usize> Default for FixedCapacityVec<T, N> {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl<T, const N: usize> Debug for FixedCapacityVec<T, N>
19where
20    T: Debug,
21{
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        self.deref().fmt(f)
24    }
25}
26
27impl<T, const N: usize> Clone for FixedCapacityVec<T, N>
28where
29    T: Clone,
30{
31    fn clone(&self) -> Self {
32        let mut out = Self::new();
33        for item in &**self {
34            out.push(item.clone())
35        }
36        out
37    }
38}
39
40impl<T, const N: usize> Hash for FixedCapacityVec<T, N>
41where
42    T: Hash,
43{
44    fn hash<H>(&self, h: &mut H)
45    where
46        H: std::hash::Hasher,
47    {
48        (**self).hash(h)
49    }
50}
51
52macro_rules! impl_comparison_trait { { $Trait:ident $( $fn:ident $result:ty )? } => {
53    impl<T, const N: usize> $Trait for FixedCapacityVec<T, N>
54    where
55        T: $Trait,
56    {
57        $(
58            fn $fn(&self, other: &Self) -> $result {
59                (**self).$fn(&**other)
60            }
61        )?
62    }
63} }
64
65// Limited version, with just Self as RHS, mostly so we can be `Ord`
66impl_comparison_trait! { PartialEq eq bool }
67impl_comparison_trait! { PartialOrd partial_cmp Option<Ordering> }
68impl_comparison_trait! { Ord cmp Ordering }
69impl_comparison_trait! { Eq }
70
71/// Error returned by [`try_push_or_discard()`](FixedCapacityVec::try_push_or_discard)
72#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
73pub struct FullError;
74
75impl Display for FullError {
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        Display::fmt("pushed to a full FixedCapacityVec", f)
78    }
79}