diff --git a/components/nonzero/Cargo.toml b/components/nonzero/Cargo.toml new file mode 100644 index 00000000000..15ba28f7eae --- /dev/null +++ b/components/nonzero/Cargo.toml @@ -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" diff --git a/components/nonzero/lib.rs b/components/nonzero/lib.rs new file mode 100644 index 00000000000..a787e647165 --- /dev/null +++ b/components/nonzero/lib.rs @@ -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; +} + +#[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 { + if x != 0 { + Some(NonZeroU32(x)) + } else { + None + } + } + + pub fn get(self) -> u32 { + self.0 + } + } +}