From 52dceb3d5f755b0db146707d0aa4dd733537cc32 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 29 Mar 2018 16:26:07 +0200 Subject: [PATCH] Use new std::num::NonZero* types instead of deprecated core::nonzero::NonZero --- components/canvas_traits/webgl.rs | 6 +- components/compositing/compositor.rs | 4 +- components/msg/constellation_msg.rs | 16 +-- components/nonzero/lib.rs | 195 ++++++++++++--------------- components/remutex/lib.rs | 10 +- 5 files changed, 101 insertions(+), 130 deletions(-) diff --git a/components/canvas_traits/webgl.rs b/components/canvas_traits/webgl.rs index 4c7e692e99ef..cbb088e710cb 100644 --- a/components/canvas_traits/webgl.rs +++ b/components/canvas_traits/webgl.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use euclid::Size2D; -use nonzero::NonZero; +use nonzero::NonZeroU32; use offscreen_gl_context::{GLContextAttributes, GLLimits}; use serde_bytes::ByteBuf; use std::fmt; @@ -280,13 +280,13 @@ pub enum WebGLCommand { macro_rules! define_resource_id_struct { ($name:ident) => { #[derive(Clone, Copy, Eq, Hash, PartialEq)] - pub struct $name(NonZero); + pub struct $name(NonZeroU32); impl $name { #[allow(unsafe_code)] #[inline] pub unsafe fn new(id: u32) -> Self { - $name(NonZero::new_unchecked(id)) + $name(NonZeroU32::new_unchecked(id)) } #[inline] diff --git a/components/compositing/compositor.rs b/components/compositing/compositor.rs index b09832ee717a..b102a2a1cffc 100644 --- a/components/compositing/compositor.rs +++ b/components/compositing/compositor.rs @@ -14,7 +14,7 @@ use ipc_channel::ipc::{self, IpcSharedMemory}; use libc::c_void; use msg::constellation_msg::{PipelineId, PipelineIndex, PipelineNamespaceId}; use net_traits::image::base::{Image, PixelFormat}; -use nonzero::NonZero; +use nonzero::NonZeroU32; use profile_traits::time::{self, ProfilerCategory, profile}; use script_traits::{AnimationState, AnimationTickType, ConstellationMsg, LayoutControlMsg}; use script_traits::{MouseButton, MouseEventType, ScrollState, TouchEventType, TouchId}; @@ -64,7 +64,7 @@ impl ConvertPipelineIdFromWebRender for webrender_api::PipelineId { fn from_webrender(&self) -> PipelineId { PipelineId { namespace_id: PipelineNamespaceId(self.0), - index: PipelineIndex(NonZero::new(self.1).expect("Webrender pipeline zero?")), + index: PipelineIndex(NonZeroU32::new(self.1).expect("Webrender pipeline zero?")), } } } diff --git a/components/msg/constellation_msg.rs b/components/msg/constellation_msg.rs index 398b5ace73e4..0bc884350725 100644 --- a/components/msg/constellation_msg.rs +++ b/components/msg/constellation_msg.rs @@ -5,7 +5,7 @@ //! The high-level interface from script to constellation. Using this abstract interface helps //! reduce coupling between these two components. -use nonzero::NonZero; +use nonzero::NonZeroU32; use std::cell::Cell; use std::fmt; use webrender_api; @@ -195,9 +195,9 @@ impl PipelineNamespace { }); } - fn next_index(&mut self) -> NonZero { + fn next_index(&mut self) -> NonZeroU32 { self.index += 1; - NonZero::new(self.index).expect("pipeline id index wrapped!") + NonZeroU32::new(self.index).expect("pipeline id index wrapped!") } fn next_pipeline_id(&mut self) -> PipelineId { @@ -221,7 +221,7 @@ thread_local!(pub static PIPELINE_NAMESPACE: Cell> = C pub struct PipelineNamespaceId(pub u32); #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -pub struct PipelineIndex(pub NonZero); +pub struct PipelineIndex(pub NonZeroU32); malloc_size_of_is_0!(PipelineIndex); #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize)] @@ -252,7 +252,7 @@ impl PipelineId { unsafe { PipelineId { namespace_id: PipelineNamespaceId(namespace_id), - index: PipelineIndex(NonZero::new_unchecked(index)), + index: PipelineIndex(NonZeroU32::new_unchecked(index)), } } } @@ -279,7 +279,7 @@ impl fmt::Display for PipelineId { } #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -pub struct BrowsingContextIndex(pub NonZero); +pub struct BrowsingContextIndex(pub NonZeroU32); malloc_size_of_is_0!(BrowsingContextIndex); #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize)] @@ -355,13 +355,13 @@ impl PartialEq for TopLevelBrowsingContextId { pub const TEST_NAMESPACE: PipelineNamespaceId = PipelineNamespaceId(1234); #[allow(unsafe_code)] #[cfg(feature = "unstable")] -pub const TEST_PIPELINE_INDEX: PipelineIndex = unsafe { PipelineIndex(NonZero::new_unchecked(5678)) }; +pub const TEST_PIPELINE_INDEX: PipelineIndex = unsafe { PipelineIndex(NonZeroU32::new_unchecked(5678)) }; #[cfg(feature = "unstable")] pub const TEST_PIPELINE_ID: PipelineId = PipelineId { namespace_id: TEST_NAMESPACE, index: TEST_PIPELINE_INDEX }; #[allow(unsafe_code)] #[cfg(feature = "unstable")] pub const TEST_BROWSING_CONTEXT_INDEX: BrowsingContextIndex = - unsafe { BrowsingContextIndex(NonZero::new_unchecked(8765)) }; + unsafe { BrowsingContextIndex(NonZeroU32::new_unchecked(8765)) }; #[cfg(feature = "unstable")] pub const TEST_BROWSING_CONTEXT_ID: BrowsingContextId = BrowsingContextId { namespace_id: TEST_NAMESPACE, index: TEST_BROWSING_CONTEXT_INDEX }; diff --git a/components/nonzero/lib.rs b/components/nonzero/lib.rs index b44cf73c7d6e..7d3d36445067 100644 --- a/components/nonzero/lib.rs +++ b/components/nonzero/lib.rs @@ -7,135 +7,106 @@ #![cfg_attr(feature = "unstable", feature(nonzero))] #![cfg_attr(feature = "unstable", feature(const_fn))] -#![cfg_attr(feature = "unstable", feature(const_nonzero_new))] -#[cfg_attr(not(feature = "unstable"), macro_use)] extern crate serde; -pub use imp::*; +use std::fmt; -#[cfg(feature = "unstable")] -mod imp { - extern crate core; - use self::core::nonzero::NonZero as CoreNonZero; - use serde::{Serialize, Serializer, Deserialize, Deserializer}; - - pub use self::core::nonzero::Zeroable; - - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] - pub struct NonZero(CoreNonZero); +macro_rules! impl_nonzero_fmt { + ( ( $( $Trait: ident ),+ ) for $Ty: ident ) => { + $( + impl fmt::$Trait for $Ty { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.get().fmt(f) + } + } + )+ + } +} - impl NonZero { - #[inline] - pub const unsafe fn new_unchecked(x: T) -> Self { - NonZero(CoreNonZero::new_unchecked(x)) - } +macro_rules! nonzero_integers { + ( $( $Ty: ident($Int: ty); )+ ) => { + $( + #[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)] + pub struct $Ty( + #[cfg(feature = "unstable")] std::num::$Ty, + #[cfg(not(feature = "unstable"))] $Int, + ); + + impl $Ty { + #[cfg(feature = "unstable")] + #[inline] + pub const unsafe fn new_unchecked(n: $Int) -> Self { + $Ty(std::num::$Ty::new_unchecked(n)) + } - #[inline] - pub fn new(x: T) -> Option { - CoreNonZero::new(x).map(NonZero) - } + #[cfg(not(feature = "unstable"))] + #[inline] + pub unsafe fn new_unchecked(n: $Int) -> Self { + $Ty(n) + } - #[inline] - pub fn get(self) -> T { - self.0.get() - } - } + #[cfg(feature = "unstable")] + #[inline] + pub fn new(n: $Int) -> Option { + std::num::$Ty::new(n).map($Ty) + } - // Not using derive because of the additional Clone bound required by the inner impl - - impl Serialize for NonZero - where - T: Serialize + Zeroable + Clone, - { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - self.0.serialize(serializer) - } - } + #[cfg(not(feature = "unstable"))] + #[inline] + pub fn new(n: $Int) -> Option { + if n != 0 { + Some($Ty(n)) + } else { + None + } + } - impl<'de, T> Deserialize<'de> for NonZero - where - T: Deserialize<'de> + Zeroable, - { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - CoreNonZero::deserialize(deserializer).map(NonZero) - } - } -} + #[cfg(feature = "unstable")] + #[inline] + pub fn get(self) -> $Int { + self.0.get() + } -#[cfg(not(feature = "unstable"))] -mod imp { - #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] - pub struct NonZero(T); - - impl NonZero { - #[inline] - pub unsafe fn new_unchecked(x: T) -> Self { - NonZero(x) - } - - #[inline] - pub fn new(x: T) -> Option { - if x.is_zero() { - None - } else { - Some(NonZero(x)) + #[cfg(not(feature = "unstable"))] + #[inline] + pub fn get(self) -> $Int { + self.0 + } } - } - - #[inline] - pub fn get(self) -> T { - self.0 - } - } - /// Unsafe trait to indicate what types are usable with the NonZero struct - pub unsafe trait Zeroable { - /// Whether this value is zero - fn is_zero(&self) -> bool; - } + impl_nonzero_fmt! { + (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty + } - macro_rules! impl_zeroable_for_pointer_types { - ( $( $Ptr: ty )+ ) => { - $( - /// For fat pointers to be considered "zero", only the "data" part needs to be null. - unsafe impl Zeroable for $Ptr { - #[inline] - fn is_zero(&self) -> bool { - // Cast because `is_null` is only available on thin pointers - (*self as *mut u8).is_null() - } + impl serde::Serialize for $Ty { + fn serialize(&self, serializer: S) -> Result + where S: serde::Serializer + { + self.get().serialize(serializer) } - )+ - } - } + } - macro_rules! impl_zeroable_for_integer_types { - ( $( $Int: ty )+ ) => { - $( - unsafe impl Zeroable for $Int { - #[inline] - fn is_zero(&self) -> bool { - *self == 0 + impl<'de> serde::Deserialize<'de> for $Ty { + fn deserialize(deserializer: D) -> Result + where D: serde::Deserializer<'de> + { + let value = <$Int>::deserialize(deserializer)?; + match <$Ty>::new(value) { + Some(nonzero) => Ok(nonzero), + None => Err(serde::de::Error::custom("expected a non-zero value")), } } - )+ - } - } - - impl_zeroable_for_pointer_types! { - *const T - *mut T + } + )+ } +} - impl_zeroable_for_integer_types! { - usize u8 u16 u32 u64 - isize i8 i16 i32 i64 - } +nonzero_integers! { + NonZeroU8(u8); + NonZeroU16(u16); + NonZeroU32(u32); + NonZeroU64(u64); + NonZeroUsize(usize); } diff --git a/components/remutex/lib.rs b/components/remutex/lib.rs index 5fbbad40bf02..2589a6d11626 100644 --- a/components/remutex/lib.rs +++ b/components/remutex/lib.rs @@ -14,7 +14,7 @@ extern crate nonzero; #[macro_use] extern crate lazy_static; #[macro_use] extern crate log; -use nonzero::NonZero; +use nonzero::NonZeroUsize; use std::cell::{Cell, UnsafeCell}; use std::ops::Deref; use std::sync::{LockResult, Mutex, MutexGuard, PoisonError, TryLockError, TryLockResult}; @@ -25,7 +25,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; // TODO: can we use the thread-id crate for this? #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct ThreadId(NonZero); +pub struct ThreadId(NonZeroUsize); lazy_static!{ static ref THREAD_COUNT: AtomicUsize = AtomicUsize::new(1); } @@ -33,7 +33,7 @@ impl ThreadId { #[allow(unsafe_code)] fn new() -> ThreadId { let number = THREAD_COUNT.fetch_add(1, Ordering::SeqCst); - ThreadId(NonZero::new(number).unwrap()) + ThreadId(NonZeroUsize::new(number).unwrap()) } pub fn current() -> ThreadId { THREAD_ID.with(|tls| tls.clone()) @@ -57,13 +57,13 @@ impl AtomicOptThreadId { #[allow(unsafe_code)] pub fn load(&self, ordering: Ordering) -> Option { let number = self.0.load(ordering); - NonZero::new(number).map(ThreadId) + NonZeroUsize::new(number).map(ThreadId) } #[allow(unsafe_code)] pub fn swap(&self, value: Option, ordering: Ordering) -> Option { let number = value.map(|id| id.0.get()).unwrap_or(0); let number = self.0.swap(number, ordering); - NonZero::new(number).map(ThreadId) + NonZeroUsize::new(number).map(ThreadId) } }