as_is/foreign/
vec.rs

1use crate::BorrowAsIs;
2use core::borrow::{Borrow, BorrowMut};
3use core::cmp::Ordering;
4use core::fmt;
5use core::hash::{Hash, Hasher};
6
7/// A stub for [`Vec<T>`] used in `no_std` environments.
8///
9/// [`Vec<T>`]: https://doc.rust-lang.org/alloc/vec/struct.Vec.html
10pub struct VecStub<T>([T; 0]);
11
12impl<T> VecStub<T> {
13    const fn as_slice(&self) -> &[T] {
14        &self.0
15    }
16
17    fn as_mut_slice(&mut self) -> &mut [T] {
18        &mut self.0
19    }
20}
21
22impl<T> fmt::Debug for VecStub<T>
23where
24    T: fmt::Debug,
25{
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        self.as_slice().fmt(f)
28    }
29}
30
31impl<T> Borrow<[T]> for VecStub<T> {
32    fn borrow(&self) -> &[T] {
33        self.as_slice()
34    }
35}
36
37impl<T> BorrowMut<[T]> for VecStub<T> {
38    fn borrow_mut(&mut self) -> &mut [T] {
39        self.as_mut_slice()
40    }
41}
42
43impl<T> PartialEq for VecStub<T>
44where
45    T: PartialEq,
46{
47    fn eq(&self, other: &Self) -> bool {
48        self.as_slice().eq(other.as_slice())
49    }
50}
51
52impl<T> Eq for VecStub<T> where T: Eq {}
53
54impl<T> PartialOrd for VecStub<T>
55where
56    T: PartialOrd,
57{
58    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
59        self.as_slice().partial_cmp(other.as_slice())
60    }
61}
62
63impl<T> Ord for VecStub<T>
64where
65    T: Ord,
66{
67    fn cmp(&self, other: &Self) -> Ordering {
68        self.as_slice().cmp(other.as_slice())
69    }
70}
71
72impl<T> Hash for VecStub<T>
73where
74    T: Hash,
75{
76    fn hash<H: Hasher>(&self, state: &mut H) {
77        self.as_slice().hash(state);
78    }
79}
80
81impl<T> BorrowAsIs for [T]
82where
83    T: Clone,
84{
85    type Is = [T];
86}
87
88#[cfg(not(feature = "alloc"))]
89impl<T> crate::ToOwned for [T]
90where
91    T: Clone,
92{
93    type Owned = VecStub<T>;
94}
95
96#[cfg(feature = "alloc")]
97mod impl_for_vec {
98    use crate::{AsIs, BorrowAsIs, Is};
99    use alloc::vec::Vec;
100
101    impl<T> BorrowAsIs for Vec<T>
102    where
103        T: Clone,
104    {
105        type Is = Vec<T>;
106    }
107
108    impl<T> AsIs for Vec<T>
109    where
110        T: Clone,
111    {
112        fn as_is<'a>(self) -> Is<'a, Self> {
113            Is::Owned(self)
114        }
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    use char_buf::CharBuf;
122    use fmt::Write;
123    use siphasher::sip::SipHasher;
124
125    macro_rules! format {
126        ($($arg:tt)*) => {
127            {
128                let mut w = CharBuf::<2>::new();
129                write!(w, $($arg)*).unwrap();
130                w
131            }
132        };
133    }
134
135    #[test]
136    fn vec_stub() {
137        let mut v: VecStub<()> = VecStub([]);
138
139        assert_eq!(v.borrow(), [(); 0]);
140        assert_eq!(v.borrow_mut(), [(); 0]);
141        assert_eq!(v < v, [(); 0] < [(); 0]);
142        assert_eq!(v == v, [(); 0] == [(); 0]);
143        assert_eq!(v.cmp(&v), [(); 0].cmp(&[(); 0]));
144        assert_eq!(format!("{:?}", v), "[]");
145
146        let mut h1 = SipHasher::new();
147        v.hash(&mut h1);
148
149        let mut h2 = SipHasher::new();
150        [(); 0].hash(&mut h2);
151
152        assert_eq!(h1.finish(), h2.finish());
153    }
154}