Skip to content

Commit 51dbbbe

Browse files
author
bors-servo
authored
Auto merge of #20474 - servo:nonzero, r=nox
Use new std::num::NonZero* types instead of deprecated core::nonzero::NonZero <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20474) <!-- Reviewable:end -->
2 parents d232705 + 970519f commit 51dbbbe

File tree

7 files changed

+102
-133
lines changed

7 files changed

+102
-133
lines changed

components/canvas_traits/webgl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
use euclid::Size2D;
6-
use nonzero::NonZero;
6+
use nonzero::NonZeroU32;
77
use offscreen_gl_context::{GLContextAttributes, GLLimits};
88
use serde_bytes::ByteBuf;
99
use std::fmt;
@@ -281,13 +281,13 @@ pub enum WebGLCommand {
281281
macro_rules! define_resource_id_struct {
282282
($name:ident) => {
283283
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
284-
pub struct $name(NonZero<u32>);
284+
pub struct $name(NonZeroU32);
285285

286286
impl $name {
287287
#[allow(unsafe_code)]
288288
#[inline]
289289
pub unsafe fn new(id: u32) -> Self {
290-
$name(NonZero::new_unchecked(id))
290+
$name(NonZeroU32::new_unchecked(id))
291291
}
292292

293293
#[inline]

components/compositing/compositor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ipc_channel::ipc::{self, IpcSharedMemory};
1414
use libc::c_void;
1515
use msg::constellation_msg::{PipelineId, PipelineIndex, PipelineNamespaceId};
1616
use net_traits::image::base::{Image, PixelFormat};
17-
use nonzero::NonZero;
17+
use nonzero::NonZeroU32;
1818
use profile_traits::time::{self, ProfilerCategory, profile};
1919
use script_traits::{AnimationState, AnimationTickType, ConstellationMsg, LayoutControlMsg};
2020
use script_traits::{MouseButton, MouseEventType, ScrollState, TouchEventType, TouchId};
@@ -64,7 +64,7 @@ impl ConvertPipelineIdFromWebRender for webrender_api::PipelineId {
6464
fn from_webrender(&self) -> PipelineId {
6565
PipelineId {
6666
namespace_id: PipelineNamespaceId(self.0),
67-
index: PipelineIndex(NonZero::new(self.1).expect("Webrender pipeline zero?")),
67+
index: PipelineIndex(NonZeroU32::new(self.1).expect("Webrender pipeline zero?")),
6868
}
6969
}
7070
}

components/msg/constellation_msg.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! The high-level interface from script to constellation. Using this abstract interface helps
66
//! reduce coupling between these two components.
77
8-
use nonzero::NonZero;
8+
use nonzero::NonZeroU32;
99
use std::cell::Cell;
1010
use std::fmt;
1111
use webrender_api;
@@ -195,9 +195,9 @@ impl PipelineNamespace {
195195
});
196196
}
197197

198-
fn next_index(&mut self) -> NonZero<u32> {
198+
fn next_index(&mut self) -> NonZeroU32 {
199199
self.index += 1;
200-
NonZero::new(self.index).expect("pipeline id index wrapped!")
200+
NonZeroU32::new(self.index).expect("pipeline id index wrapped!")
201201
}
202202

203203
fn next_pipeline_id(&mut self) -> PipelineId {
@@ -221,7 +221,7 @@ thread_local!(pub static PIPELINE_NAMESPACE: Cell<Option<PipelineNamespace>> = C
221221
pub struct PipelineNamespaceId(pub u32);
222222

223223
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
224-
pub struct PipelineIndex(pub NonZero<u32>);
224+
pub struct PipelineIndex(pub NonZeroU32);
225225
malloc_size_of_is_0!(PipelineIndex);
226226

227227
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
@@ -252,7 +252,7 @@ impl PipelineId {
252252
unsafe {
253253
PipelineId {
254254
namespace_id: PipelineNamespaceId(namespace_id),
255-
index: PipelineIndex(NonZero::new_unchecked(index)),
255+
index: PipelineIndex(NonZeroU32::new_unchecked(index)),
256256
}
257257
}
258258
}
@@ -279,7 +279,7 @@ impl fmt::Display for PipelineId {
279279
}
280280

281281
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
282-
pub struct BrowsingContextIndex(pub NonZero<u32>);
282+
pub struct BrowsingContextIndex(pub NonZeroU32);
283283
malloc_size_of_is_0!(BrowsingContextIndex);
284284

285285
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
@@ -355,13 +355,13 @@ impl PartialEq<BrowsingContextId> for TopLevelBrowsingContextId {
355355
pub const TEST_NAMESPACE: PipelineNamespaceId = PipelineNamespaceId(1234);
356356
#[allow(unsafe_code)]
357357
#[cfg(feature = "unstable")]
358-
pub const TEST_PIPELINE_INDEX: PipelineIndex = unsafe { PipelineIndex(NonZero::new_unchecked(5678)) };
358+
pub const TEST_PIPELINE_INDEX: PipelineIndex = unsafe { PipelineIndex(NonZeroU32::new_unchecked(5678)) };
359359
#[cfg(feature = "unstable")]
360360
pub const TEST_PIPELINE_ID: PipelineId = PipelineId { namespace_id: TEST_NAMESPACE, index: TEST_PIPELINE_INDEX };
361361
#[allow(unsafe_code)]
362362
#[cfg(feature = "unstable")]
363363
pub const TEST_BROWSING_CONTEXT_INDEX: BrowsingContextIndex =
364-
unsafe { BrowsingContextIndex(NonZero::new_unchecked(8765)) };
364+
unsafe { BrowsingContextIndex(NonZeroU32::new_unchecked(8765)) };
365365
#[cfg(feature = "unstable")]
366366
pub const TEST_BROWSING_CONTEXT_ID: BrowsingContextId =
367367
BrowsingContextId { namespace_id: TEST_NAMESPACE, index: TEST_BROWSING_CONTEXT_INDEX };

components/nonzero/lib.rs

Lines changed: 83 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -7,135 +7,106 @@
77
88
#![cfg_attr(feature = "unstable", feature(nonzero))]
99
#![cfg_attr(feature = "unstable", feature(const_fn))]
10-
#![cfg_attr(feature = "unstable", feature(const_nonzero_new))]
1110

12-
#[cfg_attr(not(feature = "unstable"), macro_use)]
1311
extern crate serde;
1412

15-
pub use imp::*;
13+
use std::fmt;
1614

17-
#[cfg(feature = "unstable")]
18-
mod imp {
19-
extern crate core;
20-
use self::core::nonzero::NonZero as CoreNonZero;
21-
use serde::{Serialize, Serializer, Deserialize, Deserializer};
22-
23-
pub use self::core::nonzero::Zeroable;
24-
25-
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
26-
pub struct NonZero<T: Zeroable>(CoreNonZero<T>);
15+
macro_rules! impl_nonzero_fmt {
16+
( ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
17+
$(
18+
impl fmt::$Trait for $Ty {
19+
#[inline]
20+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21+
self.get().fmt(f)
22+
}
23+
}
24+
)+
25+
}
26+
}
2727

28-
impl<T: Zeroable> NonZero<T> {
29-
#[inline]
30-
pub const unsafe fn new_unchecked(x: T) -> Self {
31-
NonZero(CoreNonZero::new_unchecked(x))
32-
}
28+
macro_rules! nonzero_integers {
29+
( $( $Ty: ident($Int: ty); )+ ) => {
30+
$(
31+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
32+
pub struct $Ty(
33+
#[cfg(feature = "unstable")] std::num::$Ty,
34+
#[cfg(not(feature = "unstable"))] $Int,
35+
);
36+
37+
impl $Ty {
38+
#[cfg(feature = "unstable")]
39+
#[inline]
40+
pub const unsafe fn new_unchecked(n: $Int) -> Self {
41+
$Ty(std::num::$Ty::new_unchecked(n))
42+
}
3343

34-
#[inline]
35-
pub fn new(x: T) -> Option<Self> {
36-
CoreNonZero::new(x).map(NonZero)
37-
}
44+
#[cfg(not(feature = "unstable"))]
45+
#[inline]
46+
pub unsafe fn new_unchecked(n: $Int) -> Self {
47+
$Ty(n)
48+
}
3849

39-
#[inline]
40-
pub fn get(self) -> T {
41-
self.0.get()
42-
}
43-
}
50+
#[cfg(feature = "unstable")]
51+
#[inline]
52+
pub fn new(n: $Int) -> Option<Self> {
53+
std::num::$Ty::new(n).map($Ty)
54+
}
4455

45-
// Not using derive because of the additional Clone bound required by the inner impl
46-
47-
impl<T> Serialize for NonZero<T>
48-
where
49-
T: Serialize + Zeroable + Clone,
50-
{
51-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
52-
where
53-
S: Serializer,
54-
{
55-
self.0.serialize(serializer)
56-
}
57-
}
56+
#[cfg(not(feature = "unstable"))]
57+
#[inline]
58+
pub fn new(n: $Int) -> Option<Self> {
59+
if n != 0 {
60+
Some($Ty(NonZero(n)))
61+
} else {
62+
None
63+
}
64+
}
5865

59-
impl<'de, T> Deserialize<'de> for NonZero<T>
60-
where
61-
T: Deserialize<'de> + Zeroable,
62-
{
63-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
64-
where
65-
D: Deserializer<'de>,
66-
{
67-
CoreNonZero::deserialize(deserializer).map(NonZero)
68-
}
69-
}
70-
}
66+
#[cfg(feature = "unstable")]
67+
#[inline]
68+
pub fn get(self) -> $Int {
69+
self.0.get()
70+
}
7171

72-
#[cfg(not(feature = "unstable"))]
73-
mod imp {
74-
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
75-
pub struct NonZero<T: Zeroable>(T);
76-
77-
impl<T: Zeroable> NonZero<T> {
78-
#[inline]
79-
pub unsafe fn new_unchecked(x: T) -> Self {
80-
NonZero(x)
81-
}
82-
83-
#[inline]
84-
pub fn new(x: T) -> Option<Self> {
85-
if x.is_zero() {
86-
None
87-
} else {
88-
Some(NonZero(x))
72+
#[cfg(not(feature = "unstable"))]
73+
#[inline]
74+
pub fn get(self) -> $Int {
75+
self.0
76+
}
8977
}
90-
}
91-
92-
#[inline]
93-
pub fn get(self) -> T {
94-
self.0
95-
}
96-
}
9778

98-
/// Unsafe trait to indicate what types are usable with the NonZero struct
99-
pub unsafe trait Zeroable {
100-
/// Whether this value is zero
101-
fn is_zero(&self) -> bool;
102-
}
79+
impl_nonzero_fmt! {
80+
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
81+
}
10382

104-
macro_rules! impl_zeroable_for_pointer_types {
105-
( $( $Ptr: ty )+ ) => {
106-
$(
107-
/// For fat pointers to be considered "zero", only the "data" part needs to be null.
108-
unsafe impl<T: ?Sized> Zeroable for $Ptr {
109-
#[inline]
110-
fn is_zero(&self) -> bool {
111-
// Cast because `is_null` is only available on thin pointers
112-
(*self as *mut u8).is_null()
113-
}
83+
impl serde::Serialize for $Ty {
84+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
85+
where S: serde::Serializer
86+
{
87+
self.get().serialize(serializer)
11488
}
115-
)+
116-
}
117-
}
89+
}
11890

119-
macro_rules! impl_zeroable_for_integer_types {
120-
( $( $Int: ty )+ ) => {
121-
$(
122-
unsafe impl Zeroable for $Int {
123-
#[inline]
124-
fn is_zero(&self) -> bool {
125-
*self == 0
91+
impl<'de> serde::Deserialize<'de> for $Ty {
92+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
93+
where D: serde::Deserializer<'de>
94+
{
95+
let value = <$Int>::deserialize(deserializer)?;
96+
match <$Ty>::new(value) {
97+
Some(nonzero) => Ok(nonzero),
98+
None => Err(serde::de::Error::custom("expected a non-zero value")),
12699
}
127100
}
128-
)+
129-
}
130-
}
131-
132-
impl_zeroable_for_pointer_types! {
133-
*const T
134-
*mut T
101+
}
102+
)+
135103
}
104+
}
136105

137-
impl_zeroable_for_integer_types! {
138-
usize u8 u16 u32 u64
139-
isize i8 i16 i32 i64
140-
}
106+
nonzero_integers! {
107+
NonZeroU8(u8); NonZeroI8(i8);
108+
NonZeroU16(u16); NonZeroI16(i16);
109+
NonZeroU32(u32); NonZeroI32(i32);
110+
NonZeroU64(u64); NonZeroI64(i64);
111+
NonZeroUsize(usize); NonZeroIsize(isize);
141112
}

components/remutex/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern crate nonzero;
1414
#[macro_use] extern crate lazy_static;
1515
#[macro_use] extern crate log;
1616

17-
use nonzero::NonZero;
17+
use nonzero::NonZeroUsize;
1818
use std::cell::{Cell, UnsafeCell};
1919
use std::ops::Deref;
2020
use std::sync::{LockResult, Mutex, MutexGuard, PoisonError, TryLockError, TryLockResult};
@@ -25,15 +25,15 @@ use std::sync::atomic::{AtomicUsize, Ordering};
2525
// TODO: can we use the thread-id crate for this?
2626

2727
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
28-
pub struct ThreadId(NonZero<usize>);
28+
pub struct ThreadId(NonZeroUsize);
2929

3030
lazy_static!{ static ref THREAD_COUNT: AtomicUsize = AtomicUsize::new(1); }
3131

3232
impl ThreadId {
3333
#[allow(unsafe_code)]
3434
fn new() -> ThreadId {
3535
let number = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
36-
ThreadId(NonZero::new(number).unwrap())
36+
ThreadId(NonZeroUsize::new(number).unwrap())
3737
}
3838
pub fn current() -> ThreadId {
3939
THREAD_ID.with(|tls| tls.clone())
@@ -57,13 +57,13 @@ impl AtomicOptThreadId {
5757
#[allow(unsafe_code)]
5858
pub fn load(&self, ordering: Ordering) -> Option<ThreadId> {
5959
let number = self.0.load(ordering);
60-
NonZero::new(number).map(ThreadId)
60+
NonZeroUsize::new(number).map(ThreadId)
6161
}
6262
#[allow(unsafe_code)]
6363
pub fn swap(&self, value: Option<ThreadId>, ordering: Ordering) -> Option<ThreadId> {
6464
let number = value.map(|id| id.0.get()).unwrap_or(0);
6565
let number = self.0.swap(number, ordering);
66-
NonZero::new(number).map(ThreadId)
66+
NonZeroUsize::new(number).map(ThreadId)
6767
}
6868
}
6969

components/script/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]
66
#![cfg_attr(feature = "unstable", feature(on_unimplemented))]
7-
#![feature(ascii_ctype)]
8-
#![feature(conservative_impl_trait)]
97
#![feature(const_fn)]
108
#![feature(mpsc_select)]
119
#![feature(plugin)]

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2018-01-27
1+
nightly-2018-03-29

0 commit comments

Comments
 (0)