Skip to content

Commit b44d7cb

Browse files
committed
Don't expose NonZero through libstd.
1 parent e83272b commit b44d7cb

File tree

7 files changed

+109
-92
lines changed

7 files changed

+109
-92
lines changed

src/liballoc/arc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ use core::default::Default;
7676
use core::kinds::{Sync, Send};
7777
use core::mem::{min_align_of, size_of, drop};
7878
use core::mem;
79+
use core::nonzero::NonZero;
7980
use core::ops::{Drop, Deref};
8081
use core::option::Option;
8182
use core::option::Option::{Some, None};
82-
use core::ptr::{mod, NonZero, RawPtr};
83+
use core::ptr::{mod, RawPtr};
8384
use heap::deallocate;
8485

8586
/// An atomically reference counted wrapper for shared state.

src/liballoc/rc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ use core::fmt;
150150
use core::hash::{mod, Hash};
151151
use core::kinds::marker;
152152
use core::mem::{transmute, min_align_of, size_of, forget};
153+
use core::nonzero::NonZero;
153154
use core::ops::{Deref, Drop};
154155
use core::option::Option;
155156
use core::option::Option::{Some, None};
156-
use core::ptr::{mod, NonZero, RawPtr};
157+
use core::ptr::{mod, RawPtr};
157158
use core::result::Result;
158159
use core::result::Result::{Ok, Err};
159160

src/libcollections/vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ use core::hash::{mod, Hash};
5656
use core::iter::repeat;
5757
use core::kinds::marker::{ContravariantLifetime, InvariantType};
5858
use core::mem;
59+
use core::nonzero::NonZero;
5960
use core::num::{Int, UnsignedInt};
6061
use core::ops;
61-
use core::ptr::{mod, NonZero};
62+
use core::ptr;
6263
use core::raw::Slice as RawSlice;
6364
use core::uint;
6465

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub mod prelude;
9393

9494
pub mod intrinsics;
9595
pub mod mem;
96+
pub mod nonzero;
9697
pub mod ptr;
9798

9899
/* Core language traits */

src/libcore/nonzero.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 cmp::Eq;
14+
use intrinsics;
15+
use kinds::Copy;
16+
use ops::Deref;
17+
use option::Option;
18+
use option::Option::Some;
19+
use ptr::{null, null_mut, RawPtr, RawMutPtr};
20+
21+
/// A wrapper type for raw pointers and integers that will never be
22+
/// NULL or 0 that might allow certain optimizations.
23+
#[lang="non_zero"]
24+
#[deriving(Clone, PartialEq, Eq, PartialOrd)]
25+
#[experimental]
26+
pub struct NonZero<T>(T);
27+
28+
impl<T> NonZero<T> {
29+
/// Create an instance of NonZero with the provided value.
30+
/// You must indeed ensure that the value is actually "non-zero".
31+
#[inline(always)]
32+
pub unsafe fn new(inner: T) -> NonZero<T> {
33+
NonZero(inner)
34+
}
35+
}
36+
37+
impl<T: Copy> Copy for NonZero<T> {}
38+
39+
impl<T> Deref<T> for NonZero<T> {
40+
#[inline]
41+
fn deref<'a>(&'a self) -> &'a T {
42+
let NonZero(ref inner) = *self;
43+
inner
44+
}
45+
}
46+
47+
impl<T> RawPtr<T> for NonZero<*const T> {
48+
#[inline]
49+
fn null() -> NonZero<*const T> { NonZero(null()) }
50+
51+
#[inline]
52+
fn is_null(&self) -> bool { false }
53+
54+
#[inline]
55+
fn to_uint(&self) -> uint {
56+
**self as uint
57+
}
58+
59+
#[inline]
60+
unsafe fn offset(self, count: int) -> NonZero<*const T> {
61+
NonZero(intrinsics::offset(*self, count))
62+
}
63+
64+
#[inline]
65+
unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
66+
Some(&***self)
67+
}
68+
}
69+
70+
impl<T> RawPtr<T> for NonZero<*mut T> {
71+
#[inline]
72+
fn null() -> NonZero<*mut T> { NonZero(null_mut()) }
73+
74+
#[inline]
75+
fn is_null(&self) -> bool { false }
76+
77+
#[inline]
78+
fn to_uint(&self) -> uint {
79+
**self as uint
80+
}
81+
82+
#[inline]
83+
unsafe fn offset(self, count: int) -> NonZero<*mut T> {
84+
NonZero(intrinsics::offset(*self as *const T, count) as *mut T)
85+
}
86+
87+
#[inline]
88+
unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
89+
Some(&***self)
90+
}
91+
}
92+
93+
impl<T> RawMutPtr<T> for NonZero<*mut T> {
94+
#[inline]
95+
unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> {
96+
Some(&mut ***self)
97+
}
98+
}

src/libcore/ptr.rs

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@
9090
use mem;
9191
use clone::Clone;
9292
use intrinsics;
93-
use kinds::{Copy, Send, Sync};
94-
use ops::Deref;
93+
use kinds::{Send, Sync};
9594
use option::Option;
9695
use option::Option::{Some, None};
9796

@@ -111,32 +110,6 @@ pub use intrinsics::copy_memory;
111110
pub use intrinsics::set_memory;
112111

113112

114-
/// A wrapper type for raw pointers and integers that will never be
115-
/// NULL or 0 that might allow certain optimizations.
116-
#[lang="non_zero"]
117-
#[deriving(Clone, PartialEq, Eq, PartialOrd)]
118-
#[experimental]
119-
pub struct NonZero<T>(T);
120-
121-
impl<T> NonZero<T> {
122-
/// Create an instance of NonZero with the provided value.
123-
/// You must indeed ensure that the value is actually "non-zero".
124-
#[inline(always)]
125-
pub unsafe fn new(inner: T) -> NonZero<T> {
126-
NonZero(inner)
127-
}
128-
}
129-
130-
impl<T> Deref<T> for NonZero<T> {
131-
#[inline]
132-
fn deref<'a>(&'a self) -> &'a T {
133-
let NonZero(ref inner) = *self;
134-
inner
135-
}
136-
}
137-
138-
impl<T: Copy> Copy for NonZero<T> {}
139-
140113
/// Creates a null raw pointer.
141114
///
142115
/// # Examples
@@ -341,32 +314,6 @@ impl<T> RawPtr<T> for *const T {
341314
}
342315
}
343316

344-
impl<T> RawPtr<T> for NonZero<*const T> {
345-
#[inline]
346-
fn null() -> NonZero<*const T> { NonZero(null()) }
347-
348-
#[inline]
349-
fn is_null(&self) -> bool { false }
350-
351-
#[inline]
352-
fn to_uint(&self) -> uint {
353-
let NonZero(p) = *self;
354-
p as uint
355-
}
356-
357-
#[inline]
358-
unsafe fn offset(self, count: int) -> NonZero<*const T> {
359-
let NonZero(p) = self;
360-
NonZero(intrinsics::offset(p, count))
361-
}
362-
363-
#[inline]
364-
unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
365-
let NonZero(p) = *self;
366-
Some(&*p)
367-
}
368-
}
369-
370317
impl<T> RawPtr<T> for *mut T {
371318
#[inline]
372319
fn null() -> *mut T { null_mut() }
@@ -392,32 +339,6 @@ impl<T> RawPtr<T> for *mut T {
392339
}
393340
}
394341

395-
impl<T> RawPtr<T> for NonZero<*mut T> {
396-
#[inline]
397-
fn null() -> NonZero<*mut T> { NonZero(null_mut()) }
398-
399-
#[inline]
400-
fn is_null(&self) -> bool { false }
401-
402-
#[inline]
403-
fn to_uint(&self) -> uint {
404-
let NonZero(p) = *self;
405-
p as uint
406-
}
407-
408-
#[inline]
409-
unsafe fn offset(self, count: int) -> NonZero<*mut T> {
410-
let NonZero(p) = self;
411-
NonZero(intrinsics::offset(p as *const T, count) as *mut T)
412-
}
413-
414-
#[inline]
415-
unsafe fn as_ref<'a>(&self) -> Option<&'a T> {
416-
let NonZero(p) = *self;
417-
Some(&*p)
418-
}
419-
}
420-
421342
impl<T> RawMutPtr<T> for *mut T {
422343
#[inline]
423344
unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> {
@@ -429,14 +350,6 @@ impl<T> RawMutPtr<T> for *mut T {
429350
}
430351
}
431352

432-
impl<T> RawMutPtr<T> for NonZero<*mut T> {
433-
#[inline]
434-
unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> {
435-
let NonZero(p) = *self;
436-
Some(&mut *p)
437-
}
438-
}
439-
440353
// Equality for pointers
441354
impl<T> PartialEq for *const T {
442355
#[inline]

src/test/run-pass/enum-null-pointer-opt.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
// except according to those terms.
1010

1111

12+
extern crate core;
13+
14+
use core::nonzero::NonZero;
1215
use std::mem::size_of;
13-
use std::ptr::NonZero;
1416
use std::rc::Rc;
1517
use std::sync::Arc;
1618

0 commit comments

Comments
 (0)