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
use crate::{AsIs, AsIsMut, Is, IsMut};
use core::borrow::{Borrow, BorrowMut};
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};
#[cfg(not(feature = "alloc"))]
use crate::ToOwned;
#[derive(Clone)]
pub struct VecStub<T>([T; 0]);
impl<T> Deref for VecStub<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> Default for VecStub<T> {
fn default() -> Self {
VecStub([])
}
}
impl<T> DerefMut for VecStub<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&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)
}
}
#[cfg(not(feature = "alloc"))]
impl<T> ToOwned for [T]
where
T: Clone,
{
type Owned = VecStub<T>;
}
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(not(feature = "alloc"))]
use VecStub as Vec;
impl<T> AsIs for Vec<T>
where
T: Clone,
{
type Is = Vec<T>;
fn as_is<'a>(self) -> Is<'a, Self::Is> {
Is::Owned(self)
}
}
impl<T> AsIsMut for Vec<T>
where
T: Clone,
{
fn as_is_mut<'a>(self) -> IsMut<'a, Self::Is> {
IsMut::Owned(self)
}
}
#[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]));
}
}