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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#![no_std]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![feature(const_raw_ptr_deref)]
#![feature(extern_types)]
extern crate unreachable;
#[cfg(feature = "utf")]
extern crate utf;
#[cfg(test)] extern crate quickcheck;
#[cfg(test)] #[macro_use] extern crate quickcheck_macros;
#[cfg(test)] extern crate std;
use core::{cmp::*, convert::TryFrom, fmt::{self, Debug, Display}, hash::{Hash, Hasher},
marker::PhantomData, mem, ops::*, slice};
extern { type Opaque; }
unsafe impl Send for Opaque {}
unsafe impl Sync for Opaque {}
#[repr(C)]
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 Self { &*(p as *const Nul<A>) }
#[inline]
pub unsafe fn new_unchecked_mut(p: *mut A) -> &'static mut Self { &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)) })
}
}
pub const unsafe fn cast<A, B>(a: &Nul<A>) -> &Nul<B> { &*(a as *const Nul<A> as *const Nul<B>) }
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: Hash> Hash for Nul<A> {
#[inline]
fn hash<H: Hasher>(&self, h: &mut H) { self.iter().for_each(|a| a.hash(h)) }
}
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 { &*(xs.as_ptr() as *const Nul<A>) }) }
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 { &mut *(xs.as_mut_ptr() as *mut Nul<A>) }) }
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(Debug, 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)
}
} }
}
#[derive(Debug)]
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 { f.write_char(x)?; }
Ok(())
}
}
impl<A> AsRef<Nul<A>> for Nul<A> { #[inline] fn as_ref(&self) -> &Self { self } }
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct NulStr(Nul<u8>);
impl Debug for NulStr {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{:?}", &self[..]) }
}
#[cfg(feature = "utf")]
impl fmt::Display for NulStr {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(&self[..]) }
}
impl Index<RangeFull> for NulStr {
type Output = str;
#[inline]
fn index(&self, _: RangeFull) -> &str { unsafe {
::core::str::from_utf8_unchecked(&self.0[..])
} }
}
impl IndexMut<RangeFull> for NulStr {
#[inline]
fn index_mut(&mut self, _: RangeFull) -> &mut str { unsafe {
::core::str::from_utf8_unchecked_mut(&mut self.0[..])
} }
}
impl NulStr {
#[inline]
pub const unsafe fn new_unchecked(p: *const u8) -> &'static Self { &*(p as *mut Self) }
#[inline]
pub unsafe fn new_unchecked_mut(p: *mut u8) -> &'static mut Self { &mut *(p as *mut Self) }
#[inline]
pub fn as_bytes(&self) -> &Nul<u8> { &self.0 }
#[inline]
pub fn as_bytes_mut(&mut self) -> &mut Nul<u8> { &mut self.0 }
#[inline]
pub fn as_ptr(&self) -> *const u8 { self.0.as_ptr() }
#[inline]
pub fn as_mut_ptr(&mut self) -> *const u8 { self.0.as_mut_ptr() }
#[cfg(feature = "utf")]
#[inline]
pub fn chars(&self) -> Chars { Chars(::utf::decode_utf8(self.0.iter().cloned())) }
#[cfg(feature = "utf")]
#[inline]
pub fn char_indices(&self) -> CharIndices { CharIndices(self.chars(), 0) }
#[inline]
pub fn is_char_boundary(&self, k: usize) -> bool { self[..].is_char_boundary(k) }
}
#[cfg(feature = "utf")]
#[derive(Debug, Clone)]
pub struct Chars<'a>(::utf::DecodeUtf8<::core::iter::Cloned<Iter<'a, u8>>>);
#[cfg(feature = "utf")]
impl<'a> Iterator for Chars<'a> {
type Item = char;
#[inline]
fn next(&mut self) -> Option<char> {
use unreachable::UncheckedResultExt;
self.0.next().map(|r| unsafe { r.unchecked_unwrap_ok() })
}
}
#[cfg(feature = "utf")]
#[derive(Debug, Clone)]
pub struct CharIndices<'a>(Chars<'a>, usize);
#[cfg(feature = "utf")]
impl<'a> Iterator for CharIndices<'a> {
type Item = (usize, char);
#[inline]
fn next(&mut self) -> Option<(usize, char)> {
let x = self.0.next()?;
let k = self.1;
self.1 += x.len_utf8();
Some((k, x))
}
}
impl<'a> TryFrom<&'a Nul<u8>> for &'a NulStr {
type Error = ::core::str::Utf8Error;
#[inline]
fn try_from(s: &'a Nul<u8>) -> Result<Self, Self::Error> {
::core::str::from_utf8(&s[..])?;
Ok(unsafe { NulStr::new_unchecked(s.as_ptr()) })
}
}
impl<'a> TryFrom<&'a mut Nul<u8>> for &'a mut NulStr {
type Error = ::core::str::Utf8Error;
#[inline]
fn try_from(s: &'a mut Nul<u8>) -> Result<Self, Self::Error> {
::core::str::from_utf8_mut(&mut s[..])?;
Ok(unsafe { NulStr::new_unchecked_mut(s.as_mut_ptr()) })
}
}
#[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) => (unsafe {
$crate::Nul::<u8>::new_unchecked(concat!($s, "\0").as_ptr() as *mut _)
})
}
#[macro_export]
macro_rules! str0_utf8 {
($s:expr) => (unsafe {
$crate::NulStr::new_unchecked(concat!($s, "\0").as_ptr() as *mut _)
})
}
#[macro_export]
macro_rules! nul_of_ref {
($($reference:expr),* $(,)?) => (unsafe {
enum Opt<T> { Nil, Just(T) }
#[inline(always)]
const unsafe fn cast_helper<'a, 'b, A: ?Sized>(a: &'b Nul<Opt<&'a A>>) -> &'b Nul<&'a A> { $crate::cast(a) }
cast_helper($crate::Nul::new_unchecked([$(Opt::Just($reference),)* Opt::Nil].as_ptr()))
})
}
#[cfg(doctest)]
pub struct NoNulOfRefInvalid;
#[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())
}
}