1use core::convert::Infallible;
2use core::marker::PhantomData;
3use core::ops::{Deref, DerefMut};
4
5use crate::flavor::Storage;
6use crate::index::{Index, Length};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16#[repr(transparent)]
17pub struct Empty;
18
19impl From<u32> for Empty {
20 fn from(_: u32) -> Self {
21 Empty
22 }
23}
24
25impl From<usize> for Empty {
26 fn from(_: usize) -> Self {
27 Empty
28 }
29}
30
31impl Index for Empty {
32 const EMPTY: Self = Empty;
33
34 type Length = Empty;
35
36 #[inline]
37 fn is_empty(&self) -> bool {
38 true
39 }
40
41 #[inline]
42 fn as_usize(self) -> usize {
43 0
44 }
45
46 #[inline]
47 fn checked_add_len(self, _: Self::Length) -> Option<Self> {
48 Some(Empty)
49 }
50
51 #[inline]
52 fn len_to(self, _: Self) -> Self {
53 Empty
54 }
55
56 #[inline]
57 fn from_usize(_: usize) -> Option<Self> {
58 Some(Empty)
59 }
60}
61
62impl From<Empty> for usize {
63 #[inline]
64 fn from(Empty: Empty) -> Self {
65 0
66 }
67}
68
69impl Length for Empty {
70 const EMPTY: Self = Empty;
71
72 #[inline]
73 fn is_empty(&self) -> bool {
74 true
75 }
76}
77
78pub struct EmptyVec<T>(PhantomData<T>);
84
85impl<T> Default for EmptyVec<T> {
86 fn default() -> Self {
87 EmptyVec(PhantomData)
88 }
89}
90
91impl<T> Storage<T> for EmptyVec<T> {
92 const EMPTY: Self = EmptyVec(PhantomData);
93
94 type Error = Infallible;
95
96 #[inline]
97 fn push(&mut self, _: T) -> Result<(), Self::Error> {
98 Ok(())
99 }
100
101 #[inline]
102 fn with_capacity(_: usize) -> Result<Self, Self::Error> {
103 Ok(Self::EMPTY)
104 }
105
106 #[inline]
107 fn capacity(&self) -> usize {
108 0
109 }
110}
111
112impl<T> Deref for EmptyVec<T> {
113 type Target = [T];
114
115 #[inline]
116 fn deref(&self) -> &Self::Target {
117 &[]
118 }
119}
120
121impl<T> DerefMut for EmptyVec<T> {
122 #[inline]
123 fn deref_mut(&mut self) -> &mut Self::Target {
124 &mut []
125 }
126}