mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Add a nonzero crate
This commit is contained in:
parent
faff7806b9
commit
c4bf3ec14f
2 changed files with 57 additions and 0 deletions
15
components/nonzero/Cargo.toml
Normal file
15
components/nonzero/Cargo.toml
Normal 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
42
components/nonzero/lib.rs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue