mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Use new std::num::NonZero* types instead of deprecated core::nonzero::NonZero
This commit is contained in:
parent
3d6614e314
commit
52dceb3d5f
5 changed files with 114 additions and 143 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue