diff options
| author | Luqman Aden <laden@csclub.uwaterloo.ca> | 2014-12-11 22:29:24 -0500 |
|---|---|---|
| committer | Luqman Aden <laden@csclub.uwaterloo.ca> | 2014-12-28 19:40:48 -0500 |
| commit | b44d7cb89c57b1fc0495b337dfddbe5cdc2ed6b2 (patch) | |
| tree | 8b635a7045b5a8c0a153738a665b43a7107d6aa1 /src/libcore/nonzero.rs | |
| parent | e83272b62883f97b8717a8150d894e89d7ae18d6 (diff) | |
| download | rust-b44d7cb89c57b1fc0495b337dfddbe5cdc2ed6b2.tar.gz rust-b44d7cb89c57b1fc0495b337dfddbe5cdc2ed6b2.zip | |
Don't expose NonZero through libstd.
Diffstat (limited to 'src/libcore/nonzero.rs')
| -rw-r--r-- | src/libcore/nonzero.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs new file mode 100644 index 00000000000..f976f08bf84 --- /dev/null +++ b/src/libcore/nonzero.rs @@ -0,0 +1,98 @@ +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Exposes the NonZero lang item which provides optimization hints. + +use cmp::Eq; +use intrinsics; +use kinds::Copy; +use ops::Deref; +use option::Option; +use option::Option::Some; +use ptr::{null, null_mut, RawPtr, RawMutPtr}; + +/// A wrapper type for raw pointers and integers that will never be +/// NULL or 0 that might allow certain optimizations. +#[lang="non_zero"] +#[deriving(Clone, PartialEq, Eq, PartialOrd)] +#[experimental] +pub struct NonZero<T>(T); + +impl<T> NonZero<T> { + /// Create an instance of NonZero with the provided value. + /// You must indeed ensure that the value is actually "non-zero". + #[inline(always)] + pub unsafe fn new(inner: T) -> NonZero<T> { + NonZero(inner) + } +} + +impl<T: Copy> Copy for NonZero<T> {} + +impl<T> Deref<T> for NonZero<T> { + #[inline] + fn deref<'a>(&'a self) -> &'a T { + let NonZero(ref inner) = *self; + inner + } +} + +impl<T> RawPtr<T> for NonZero<*const T> { + #[inline] + fn null() -> NonZero<*const T> { NonZero(null()) } + + #[inline] + fn is_null(&self) -> bool { false } + + #[inline] + fn to_uint(&self) -> uint { + **self as uint + } + + #[inline] + unsafe fn offset(self, count: int) -> NonZero<*const T> { + NonZero(intrinsics::offset(*self, count)) + } + + #[inline] + unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + Some(&***self) + } +} + +impl<T> RawPtr<T> for NonZero<*mut T> { + #[inline] + fn null() -> NonZero<*mut T> { NonZero(null_mut()) } + + #[inline] + fn is_null(&self) -> bool { false } + + #[inline] + fn to_uint(&self) -> uint { + **self as uint + } + + #[inline] + unsafe fn offset(self, count: int) -> NonZero<*mut T> { + NonZero(intrinsics::offset(*self as *const T, count) as *mut T) + } + + #[inline] + unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + Some(&***self) + } +} + +impl<T> RawMutPtr<T> for NonZero<*mut T> { + #[inline] + unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> { + Some(&mut ***self) + } +} |
