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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright © 2015 Michael Howell <michael@notriddle.com>
// This library is released under the same terms as Rust itself.

//! Empty collection and iterator.

extern crate void;
use void::Void;
use std::borrow::{Borrow, BorrowMut};
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::slice;

/// Generate a empty slice from nothing.
pub fn slice<'a, T: 'a>() -> &'a [T] {
    unsafe { slice::from_raw_parts(0x1 as *const T, 0) }
}

/// Generate a empty slice from nothing.
pub fn slice_mut<'a, T: 'a>() -> &'a mut [T] {
    unsafe { slice::from_raw_parts_mut(0x1 as *mut T, 0) }
}

/// A collection that is empty at creation and cannot be modified.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct List;

/// An iterator that never yields anything.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct Iter;

impl<T> Borrow<[T]> for List {
    #[inline]
    fn borrow(&self) -> &[T] {
        unsafe { slice::from_raw_parts(0x1 as *const T, 0) }
    }
}

impl<T> BorrowMut<[T]> for List {
    #[inline]
    fn borrow_mut(&mut self) -> &mut [T] {
        unsafe { slice::from_raw_parts_mut(0x1 as *mut T, 0) }
    }
}

impl<'a> From<List> for &'a [Void] {
    #[inline]
    fn from(_: List) -> &'a [Void] {
        unsafe { slice::from_raw_parts(0x1 as *const Void, 0) }
    }
}

impl<'a, 'b> From<&'a List> for &'b [Void] {
    #[inline]
    fn from(_: &'a List) -> &'b [Void] {
        unsafe { slice::from_raw_parts(0x1 as *const Void, 0) }
    }
}

impl<'a> From<&'a [Void]> for List {
    #[inline]
    fn from(_: &'a [Void]) -> List {
        List
    }
}

impl FromIterator<Void> for List {
    #[inline]
    fn from_iter<I: IntoIterator<Item=Void>>(_: I) -> List {
        List
    }
}

impl<'a> FromIterator<&'a Void> for List {
    #[inline]
    fn from_iter<I: IntoIterator<Item=&'a Void>>(_: I) -> List {
        List
    }
}

impl Hash for List {
    #[inline]
    fn hash<H: Hasher>(&self, _: &mut H) {
        // This body intentionally left blank
    }
}

impl<U> Index<U> for List {
    type Output = Void;
    #[inline]
    fn index(&self, _: U) -> &Void {
        panic!("index into empty::List")
    }
}

impl<U> IndexMut<U> for List {
    #[inline]
    fn index_mut(&mut self, _: U) -> &mut Void {
        panic!("index_mut into empty::List")
    }
}

impl Deref for List {
    type Target = [Void];
    #[inline]
    fn deref(&self) -> &[Void] {
        self.borrow()
    }
}

impl DerefMut for List {
    #[inline]
    fn deref_mut(&mut self) -> &mut [Void] {
        self.borrow_mut()
    }
}

impl AsRef<List> for List {
    fn as_ref(&self) -> &List {
        self
    }
}

impl<T> AsRef<[T]> for List {
    fn as_ref(&self) -> &[T] {
        self.borrow()
    }
}

impl<'a> IntoIterator for &'a List {
    type Item = Void;
    type IntoIter = Iter;
    #[inline]
    fn into_iter(self) -> Iter {
        Iter
    }
}

impl IntoIterator for List {
    type Item = Void;
    type IntoIter = Iter;
    #[inline]
    fn into_iter(self) -> Iter {
        Iter
    }
}

impl Iterator for Iter {
    type Item = Void;
    #[inline]
    fn next(&mut self) -> Option<Void> {
        None
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(0))
    }
    #[inline]
    fn count(self) -> usize {
        0
    }
    #[inline]
    fn last(self) -> Option<Void> {
        None
    }
    #[inline]
    fn nth(&mut self, _: usize) -> Option<Void> {
        None
    }
}

impl DoubleEndedIterator for Iter {
    #[inline]
    fn next_back(&mut self) -> Option<Void> {
        None
    }
}

impl ExactSizeIterator for Iter {
    #[inline]
    fn len(&self) -> usize {
        0
    }
}

#[cfg(test)]
mod test {
    use void;
    #[test]
    fn it_works() {
        for i in super::List {
            void::unreachable(i);
        }
    }
    #[test]
    fn it_works_2() {
        let l = super::List;
        for _ in &*l {
            panic!();
        }
    }
    #[test]
    fn it_works_iterator() {
        for i in super::Iter {
            void::unreachable(i);
        }
    }
    #[test]
    fn len() {
        let l = super::List;
        assert_eq!(l.len(), 0);
    }
}