]> git.lizzy.rs Git - rust.git/blob - src/libcore/nonzero.rs
doc: remove incomplete sentence
[rust.git] / src / libcore / nonzero.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Exposes the NonZero lang item which provides optimization hints.
12
13 use ops::Deref;
14
15 /// Unsafe trait to indicate what types are usable with the NonZero struct
16 pub unsafe trait Zeroable {}
17
18 unsafe impl<T> Zeroable for *const T {}
19 unsafe impl<T> Zeroable for *mut T {}
20 unsafe impl Zeroable for int {}
21 unsafe impl Zeroable for uint {}
22 unsafe impl Zeroable for i8 {}
23 unsafe impl Zeroable for u8 {}
24 unsafe impl Zeroable for i16 {}
25 unsafe impl Zeroable for u16 {}
26 unsafe impl Zeroable for i32 {}
27 unsafe impl Zeroable for u32 {}
28 unsafe impl Zeroable for i64 {}
29 unsafe impl Zeroable for u64 {}
30
31 /// A wrapper type for raw pointers and integers that will never be
32 /// NULL or 0 that might allow certain optimizations.
33 #[lang="non_zero"]
34 #[deriving(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Show)]
35 #[experimental]
36 pub struct NonZero<T: Zeroable>(T);
37
38 impl<T: Zeroable> NonZero<T> {
39     /// Create an instance of NonZero with the provided value.
40     /// You must indeed ensure that the value is actually "non-zero".
41     #[inline(always)]
42     pub unsafe fn new(inner: T) -> NonZero<T> {
43         NonZero(inner)
44     }
45 }
46
47 impl<T: Zeroable> Deref for NonZero<T> {
48     type Target = T;
49
50     #[inline]
51     fn deref<'a>(&'a self) -> &'a T {
52         let NonZero(ref inner) = *self;
53         inner
54     }
55 }