Add a nonzero crate

This commit is contained in:
Simon Sapin 2017-10-12 00:28:31 +02:00
parent faff7806b9
commit c4bf3ec14f
2 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,15 @@
[package]
name = "nonzero"
version = "0.0.1"
authors = ["The Servo Project Developers"]
license = "MPL-2.0"
publish = false
[lib]
path = "lib.rs"
[features]
unstable = ["serde/unstable"]
[dependencies]
serde = "1.0.14"

42
components/nonzero/lib.rs Normal file
View file

@ -0,0 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! `NonZero*` types that are either `core::nonzero::NonZero<_>`
//! or some stable types with an equivalent API (but no memory layout optimization).
#![cfg_attr(feature = "unstable", feature(nonzero))]
#[cfg(not(feature = "unstable"))]
#[macro_use]
extern crate serde;
pub use imp::*;
#[cfg(feature = "unstable")]
mod imp {
extern crate core;
use self::core::nonzero::NonZero;
pub type NonZeroU32 = NonZero<u32>;
}
#[cfg(not(feature = "unstable"))]
mod imp {
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct NonZeroU32(u32);
impl NonZeroU32 {
pub fn new(x: u32) -> Option<Self> {
if x != 0 {
Some(NonZeroU32(x))
} else {
None
}
}
pub fn get(self) -> u32 {
self.0
}
}
}