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>);
83
84impl<T> Default for EmptyVec<T> {
85 fn default() -> Self {
86 EmptyVec(PhantomData)
87 }
88}
89
90impl<T> Storage<T> for EmptyVec<T> {
91 const EMPTY: Self = EmptyVec(PhantomData);
92
93 type Error = Infallible;
94
95 #[inline]
96 fn push(&mut self, _: T) -> Result<(), Self::Error> {
97 Ok(())
98 }
99
100 #[inline]
101 fn with_capacity(_: usize) -> Result<Self, Self::Error> {
102 Ok(Self::EMPTY)
103 }
104
105 #[inline]
106 fn capacity(&self) -> usize {
107 0
108 }
109}
110
111impl<T> Deref for EmptyVec<T> {
112 type Target = [T];
113
114 #[inline]
115 fn deref(&self) -> &Self::Target {
116 &[]
117 }
118}
119
120impl<T> DerefMut for EmptyVec<T> {
121 #[inline]
122 fn deref_mut(&mut self) -> &mut Self::Target {
123 &mut []
124 }
125}