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 -->
This commit is contained in:
bors-servo 2018-04-09 15:44:16 -04:00 committed by GitHub
commit bfb9fe6159
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 124 additions and 157 deletions

12
Cargo.lock generated
View file

@ -205,7 +205,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "blurmac"
version = "0.1.0"
source = "git+https://github.com/servo/devices#1069d67cbacb28b77a3d5dd7f211171c05f32c62"
source = "git+https://github.com/servo/devices#eeb6eaddb79c019bb5c85558b3410b836da57a57"
dependencies = [
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"objc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -688,7 +688,7 @@ dependencies = [
[[package]]
name = "device"
version = "0.0.1"
source = "git+https://github.com/servo/devices#1069d67cbacb28b77a3d5dd7f211171c05f32c62"
source = "git+https://github.com/servo/devices#eeb6eaddb79c019bb5c85558b3410b836da57a57"
dependencies = [
"blurdroid 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"blurmac 0.1.0 (git+https://github.com/servo/devices)",
@ -1110,7 +1110,7 @@ dependencies = [
"shared_library 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-client 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
"x11-dl 2.17.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -2737,7 +2737,7 @@ dependencies = [
"tinyfiledialogs 3.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"user32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winit 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
"winres 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"x11 2.17.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -3664,7 +3664,7 @@ dependencies = [
[[package]]
name = "winit"
version = "0.11.2"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -4082,7 +4082,7 @@ dependencies = [
"checksum winapi-i686-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ec6667f60c23eca65c561e63a13d81b44234c2e38a6b6c959025ee907ec614cc"
"checksum winapi-x86_64-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98f12c52b2630cd05d2c3ffd8e008f7f48252c042b4871c72aed9dc733b96668"
"checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767"
"checksum winit 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "396f0350e661940359e3c8c7d58ff847f67997943e2c80ecac374c5aa8287f63"
"checksum winit 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f1a29847ed6928d6cbabe6b2d5b11dd0ce63380af53a8dcd41775d27d104d285"
"checksum winres 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "27d9192d6356d7efe8405dec6c5506b67543cf64b6049968f39f4c4623b4f25d"
"checksum ws 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "89c48c53bf9dee34411a08993c10b879c36e105d609b46e25673befe3a5c1320"
"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"

View file

@ -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<u32>);
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]

View file

@ -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?")),
}
}
}

View file

@ -7,7 +7,6 @@
extern crate proc_macro;
use proc_macro::{TokenStream, quote};
use std::iter;
#[proc_macro_attribute]
pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream {
@ -21,7 +20,7 @@ pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream {
};
// Work around https://github.com/rust-lang/rust/issues/46489
let attributes = attributes.to_string().parse().unwrap();
let attributes: TokenStream = attributes.to_string().parse().unwrap();
iter::once(attributes).chain(iter::once(input)).collect()
attributes.into_iter().chain(input.into_iter()).collect()
}

View file

@ -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<u32> {
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<Option<PipelineNamespace>> = C
pub struct PipelineNamespaceId(pub u32);
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PipelineIndex(pub NonZero<u32>);
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<u32>);
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<BrowsingContextId> 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 };

View file

@ -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<T: Zeroable>(CoreNonZero<T>);
impl<T: Zeroable> NonZero<T> {
#[inline]
pub const unsafe fn new_unchecked(x: T) -> Self {
NonZero(CoreNonZero::new_unchecked(x))
}
#[inline]
pub fn new(x: T) -> Option<Self> {
CoreNonZero::new(x).map(NonZero)
}
#[inline]
pub fn get(self) -> T {
self.0.get()
}
}
// Not using derive because of the additional Clone bound required by the inner impl
impl<T> Serialize for NonZero<T>
where
T: Serialize + Zeroable + Clone,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(serializer)
}
}
impl<'de, T> Deserialize<'de> for NonZero<T>
where
T: Deserialize<'de> + Zeroable,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
CoreNonZero::deserialize(deserializer).map(NonZero)
}
}
}
#[cfg(not(feature = "unstable"))]
mod imp {
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct NonZero<T: Zeroable>(T);
impl<T: Zeroable> NonZero<T> {
#[inline]
pub unsafe fn new_unchecked(x: T) -> Self {
NonZero(x)
}
#[inline]
pub fn new(x: T) -> Option<Self> {
if x.is_zero() {
None
} else {
Some(NonZero(x))
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)
}
}
}
#[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;
}
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<T: ?Sized> 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()
}
}
)+
}
}
macro_rules! impl_zeroable_for_integer_types {
( $( $Int: ty )+ ) => {
$(
unsafe impl Zeroable for $Int {
#[inline]
fn is_zero(&self) -> bool {
*self == 0
}
}
)+
}
}
impl_zeroable_for_pointer_types! {
*const T
*mut T
}
impl_zeroable_for_integer_types! {
usize u8 u16 u32 u64
isize i8 i16 i32 i64
)+
}
}
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))
}
#[cfg(not(feature = "unstable"))]
#[inline]
pub unsafe fn new_unchecked(n: $Int) -> Self {
$Ty(n)
}
#[cfg(feature = "unstable")]
#[inline]
pub fn new(n: $Int) -> Option<Self> {
std::num::$Ty::new(n).map($Ty)
}
#[cfg(not(feature = "unstable"))]
#[inline]
pub fn new(n: $Int) -> Option<Self> {
if n != 0 {
Some($Ty(n))
} else {
None
}
}
#[cfg(feature = "unstable")]
#[inline]
pub fn get(self) -> $Int {
self.0.get()
}
#[cfg(not(feature = "unstable"))]
#[inline]
pub fn get(self) -> $Int {
self.0
}
}
impl_nonzero_fmt! {
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
}
impl serde::Serialize for $Ty {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer
{
self.get().serialize(serializer)
}
}
impl<'de> serde::Deserialize<'de> for $Ty {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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")),
}
}
}
)+
}
}
nonzero_integers! {
NonZeroU8(u8);
NonZeroU16(u16);
NonZeroU32(u32);
NonZeroU64(u64);
NonZeroUsize(usize);
}

View file

@ -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<usize>);
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<ThreadId> {
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<ThreadId>, ordering: Ordering) -> Option<ThreadId> {
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)
}
}

View file

@ -208,7 +208,7 @@ impl DOMString {
let first_non_whitespace = self.0.find(|ref c| !char::is_ascii_whitespace(c)).unwrap();
self.0.truncate(last_non_whitespace);
let _ = self.0.splice(0..first_non_whitespace, "");
let _ = self.0.replace_range(0..first_non_whitespace, "");
}
/// Validates this `DOMString` is a time string according to

View file

@ -4,13 +4,10 @@
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]
#![cfg_attr(feature = "unstable", feature(on_unimplemented))]
#![feature(ascii_ctype)]
#![feature(conservative_impl_trait)]
#![feature(const_fn)]
#![feature(mpsc_select)]
#![feature(plugin)]
#![feature(proc_macro)]
#![feature(splice)]
#![feature(string_retain)]
#![deny(unsafe_code)]

View file

@ -1 +1 @@
nightly-2018-01-27
nightly-2018-04-08