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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#![no_std]
#![feature(const_fn)]
#![feature(extern_types)]
#![feature(untagged_unions)]
#![cfg_attr(test, feature(custom_attribute))]
#![cfg_attr(test, feature(plugin))]
#![cfg_attr(test, plugin(quickcheck_macros))]
extern crate fallible;
#[cfg(test)] extern crate quickcheck;
#[cfg(test)] #[macro_use]
extern crate std;
use core::cmp::*;
use core::fmt::{self, Debug, Display};
use core::marker::PhantomData;
use core::mem;
use core::ops::*;
use core::slice;
use fallible::*;
extern { type Opaque; }
unsafe impl Send for Opaque {}
unsafe impl Sync for Opaque {}
pub struct Nul<A>([A; 0], Opaque);
impl<A> Nul<A> {
#[inline]
pub const fn as_ptr(&self) -> *const A { self as *const Self as *const A }
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut A { self as *mut Self as *mut A }
#[inline]
pub const fn iter(&self) -> Iter<A> { Iter(self.as_ptr(), PhantomData) }
#[inline]
pub fn iter_mut(&mut self) -> IterMut<A> { IterMut(self.as_mut_ptr(), PhantomData) }
#[inline]
pub const unsafe fn new_unchecked(p: *const A) -> &'static Nul<A> {
union U<A: 'static> { p: *const A, q: &'static Nul<A> }
U { p }.q
}
#[inline]
pub unsafe fn new_unchecked_mut(p: *mut A) -> &'static mut Nul<A> {
&mut *(p as *mut Nul<A>)
}
#[inline]
pub fn len(&self) -> usize { unsafe {
if 0 == mem::size_of::<A>() { return 0; }
let mut p = self.as_ptr();
while !is_null(&*p) { p = p.offset(1); }
ptr_diff(p, self.as_ptr())
} }
#[inline]
pub fn get(&self, i: usize) -> Option<&A> { self[..].get(i) }
#[inline]
pub fn get_mut(&mut self, i: usize) -> Option<&mut A> { self[..].get_mut(i) }
#[inline]
pub fn split_at(&self, i: usize) -> (&[A], &Self) {
self.try_split_at(i).expect("index out of bounds")
}
#[inline]
pub fn split_at_mut(&mut self, i: usize) -> (&mut [A], &mut Self) {
self.try_split_at_mut(i).expect("index out of bounds")
}
#[inline]
pub fn try_split_at(&self, i: usize) -> Option<(&[A], &Self)> {
let mut it = self.iter();
for _ in 0..i { if let None = it.next() { return None; } }
Some(unsafe { (slice::from_raw_parts(self.as_ptr(), i), <&Self>::from(it)) })
}
#[inline]
pub fn try_split_at_mut(&mut self, i: usize) -> Option<(&mut [A], &mut Self)> {
let p = self.as_mut_ptr();
let mut it = self.iter_mut();
for _ in 0..i { if let None = it.next() { return None; } }
Some(unsafe { (slice::from_raw_parts_mut(p, i), <&mut Self>::from(it)) })
}
}
impl<A, I> Index<I> for Nul<A> where [A]: Index<I> {
type Output = <[A] as Index<I>>::Output;
#[inline]
fn index(&self, i: I) -> &Self::Output {
unsafe { slice::from_raw_parts(self.as_ptr(), self.len()).index(i) }
}
}
impl<A, I> IndexMut<I> for Nul<A> where [A]: IndexMut<I> {
#[inline]
fn index_mut(&mut self, i: I) -> &mut Self::Output {
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len()).index_mut(i) }
}
}
impl<A: Debug> Debug for Nul<A> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self[..].fmt(f) }
}
impl<A: PartialEq> PartialEq for Nul<A> {
#[inline]
fn eq(&self, other: &Self) -> bool { self[..] == other[..] }
}
impl<A: Eq> Eq for Nul<A> {}
impl<A: PartialOrd> PartialOrd for Nul<A> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
<[A]>::partial_cmp(&self[..], &other[..])
}
}
impl<A: Ord> Ord for Nul<A> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering { <[A]>::cmp(&self[..], &other[..]) }
}
impl<'a, A> TryFrom<&'a [A]> for &'a Nul<A> {
type Error = ();
#[inline]
fn try_from(xs: &'a [A]) -> Result<Self, ()> {
if xs.last().map_or(false, is_null) { Ok(unsafe { mem::transmute(&xs[0]) }) }
else { Err(()) }
}
}
impl<'a, A> TryFrom<&'a mut [A]> for &'a mut Nul<A> {
type Error = ();
#[inline]
fn try_from(xs: &'a mut [A]) -> Result<Self, ()> {
if xs.last().map_or(false, is_null) { Ok(unsafe { mem::transmute(&mut xs[0]) }) }
else { Err(()) }
}
}
impl<'a, A> IntoIterator for &'a Nul<A> {
type Item = &'a A;
type IntoIter = Iter<'a, A>;
#[inline]
fn into_iter(self) -> Iter<'a, A> { self.iter() }
}
impl<'a, A> IntoIterator for &'a mut Nul<A> {
type Item = &'a mut A;
type IntoIter = IterMut<'a, A>;
#[inline]
fn into_iter(self) -> IterMut<'a, A> { self.iter_mut() }
}
impl<'a, A> From<Iter<'a, A>> for &'a Nul<A> {
#[inline]
fn from(it: Iter<'a, A>) -> Self { unsafe { &*(it.0 as *const Nul<A>) } }
}
impl<'a, A> From<IterMut<'a, A>> for &'a mut Nul<A> {
#[inline]
fn from(it: IterMut<'a, A>) -> Self { unsafe { &mut *(it.0 as *mut Nul<A>) } }
}
#[derive(Clone, Copy)]
pub struct Iter<'a, A: 'a>(*const A, PhantomData<&'a A>);
unsafe impl<'a, T: Sync> Send for Iter<'a, T> {}
unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}
impl<'a, A: 'a> Iterator for Iter<'a, A> {
type Item = &'a A;
#[inline]
fn next(&mut self) -> Option<&'a A> { unsafe {
if is_null(&*self.0) { None } else {
let ptr = self.0;
self.0 = ptr.offset(1);
Some(&*ptr)
}
} }
}
pub struct IterMut<'a, A: 'a>(*mut A, PhantomData<&'a mut A>);
unsafe impl<'a, T: Send> Send for IterMut<'a, T> {}
unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
impl<'a, A: 'a> Iterator for IterMut<'a, A> {
type Item = &'a mut A;
#[inline]
fn next(&mut self) -> Option<&'a mut A> { unsafe {
if is_null(&*self.0) { None } else {
let ptr = self.0;
self.0 = ptr.offset(1);
Some(&mut *ptr)
}
} }
}
impl Display for Nul<char> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use fmt::Write;
for &x in self { try!(f.write_char(x)); }
Ok(())
}
}
impl<A> AsRef<Nul<A>> for Nul<A> { #[inline] fn as_ref(&self) -> &Self { self } }
#[inline]
fn is_null<A>(a: &A) -> bool { unsafe {
let l = mem::size_of_val(a);
let p = a as *const A as *const u8;
slice::from_raw_parts(p, l).iter().all(|&b| 0 == b)
} }
#[inline]
fn ptr_diff<A>(p: *const A, q: *const A) -> usize {
use ::core::num::Wrapping as w;
(w(p as usize) - w(q as usize)).0/mem::size_of::<A>()
}
#[macro_export]
macro_rules! str0 {
($s:expr) => (#[allow(unused_unsafe)] unsafe {
$crate::Nul::<u8>::new_unchecked(concat!($s, "\0").as_ptr() as *mut _)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[quickcheck]
fn test(mut xs: ::std::vec::Vec<usize>) -> bool {
xs.push(0);
let l = xs.iter().take_while(|&&x| 0usize != x).count();
xs[0..l] == <&Nul<_>>::try_from(&xs[..]).unwrap()[..]
}
#[quickcheck]
fn iter(mut xs: ::std::vec::Vec<usize>) -> bool {
xs.push(0);
let l = xs.iter().take_while(|&&x| 0usize != x).count();
Iterator::eq(xs.iter().take(l), <&Nul<_>>::try_from(&xs[..]).unwrap().iter())
}
}