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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::{AsIs, BorrowIs, Is, ToOwned};
use core::borrow::{Borrow, BorrowMut};
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// A stub for [`Vec<T>`] used in a `no_std` environment.
///
/// [`Vec<T>`]: https://doc.rust-lang.org/alloc/vec/struct.Vec.html
#[derive(Clone)]
pub struct VecStub<T>([T; 0]);

impl<T> Default for VecStub<T> {
    fn default() -> Self {
        VecStub([])
    }
}

impl<T> Deref for VecStub<T> {
    type Target = [T];

    fn deref(&self) -> &[T] {
        &self.0
    }
}

impl<T> DerefMut for VecStub<T> {
    fn deref_mut(&mut self) -> &mut [T] {
        &mut self.0
    }
}

impl<T> Borrow<[T]> for VecStub<T> {
    fn borrow(&self) -> &[T] {
        self
    }
}

impl<T> BorrowMut<[T]> for VecStub<T> {
    fn borrow_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<T> PartialEq for VecStub<T>
where
    T: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        (**self).eq(&**other)
    }
}

impl<T> Eq for VecStub<T> where T: Eq {}

impl<T> PartialOrd for VecStub<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        (**self).partial_cmp(&**other)
    }
}

impl<T> Ord for VecStub<T>
where
    T: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        (**self).cmp(&**other)
    }
}

impl<T> Hash for VecStub<T>
where
    T: Hash,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        (**self).hash(state)
    }
}

impl<T> fmt::Debug for VecStub<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (**self).fmt(f)
    }
}

impl<T> BorrowIs for VecStub<T>
where
    T: Clone,
{
    type Is = Self;
}

impl<T> AsIs for VecStub<T>
where
    T: Clone,
{
    fn as_is<'a>(self) -> Is<'a, Self> {
        Is::Owned(self)
    }
}

#[cfg(feature = "alloc")]
impl<T> BorrowIs for Vec<T>
where
    T: Clone,
{
    type Is = Self;
}

#[cfg(feature = "alloc")]
impl<T> AsIs for Vec<T>
where
    T: Clone,
{
    fn as_is<'a>(self) -> Is<'a, Self> {
        Is::Owned(self)
    }
}

impl<T> BorrowIs for [T]
where
    T: Clone,
{
    type Is = Self;
}

impl<T> ToOwned for [T]
where
    T: Clone,
{
    #[cfg(feature = "alloc")]
    type Owned = Vec<T>;

    #[cfg(not(feature = "alloc"))]
    type Owned = VecStub<T>;

    #[cfg(feature = "alloc")]
    fn to_owned(&self) -> Self::Owned {
        alloc::borrow::ToOwned::to_owned(self)
    }

    #[cfg(feature = "alloc")]
    fn clone_into(&self, target: &mut Self::Owned) {
        alloc::borrow::ToOwned::clone_into(self, target)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn vec_stub() {
        let v: VecStub<()> = VecStub::default();

        assert_eq!(v.clone(), v);
        assert_eq!(v < v, [true; 0] < [true; 0]);
        assert_eq!(v.cmp(&v), [true; 0].cmp(&[true; 0]));
    }
}