Crates.io | nonzero_lit |
lib.rs | nonzero_lit |
version | 0.1.2 |
source | src |
created_at | 2021-05-02 19:30:49.107905 |
updated_at | 2021-05-03 17:11:25.291485 |
description | Easy, safe, and fully zero-cost NonZero constants and literals. |
homepage | https://github.com/thomcc/nonzero_lit |
repository | https://github.com/thomcc/nonzero_lit |
max_upload_size | |
id | 392260 |
size | 46,496 |
nonzero_lit
A small macro crate providing safe, easy, and fully zero-cost way to construct constant or literal instances of the NonZero*
types from core::num
.
Crate fully supports no_std
.
All NonZero
types are supported:
core::num::NonZeroUsize
via the nonzero_lit::usize!
macro.core::num::NonZeroIsize
via the nonzero_lit::isize!
macro.core::num::NonZeroU128
via the nonzero_lit::u128!
macro.core::num::NonZeroI128
via the nonzero_lit::i128!
macro.core::num::NonZeroU64
via the nonzero_lit::u64!
macro.core::num::NonZeroI64
via the nonzero_lit::i64!
macro.core::num::NonZeroU32
via the nonzero_lit::u32!
macro.core::num::NonZeroI32
via the nonzero_lit::i32!
macro.core::num::NonZeroU16
via the nonzero_lit::u16!
macro.core::num::NonZeroI16
via the nonzero_lit::i16!
macro.core::num::NonZeroU8
via the nonzero_lit::u8!
macro.core::num::NonZeroI8
via the nonzero_lit::i8!
macro.Fully zero cost, even for debug builds — we always evaluate the constant as a const
.
Input to the macros can be arbitrary constant expressions. This includes const fn
calls, which would be more difficult to verify the result as non-zero by hand.
Misuse (trying to make a NonZero$Int
with a zero value) is always detected at compile time, even when the macro is not being used to initialize a constant.
No unsafe code.
Add this to your Cargo.toml:
[dependencies]
nonzero_lit = "0.1"
let x = nonzero_lit::i32!(4);
assert_eq!(x.get(), 4);
const FERRIS: core::num::NonZeroU32 = nonzero_lit::u32!(0xf34415);
assert_eq!(FERRIS.get(), 0xf34415);
const FERRIS: core::num::NonZeroU32 = nonzero_lit::u32!(0xf34415);
assert_eq!(FERRIS.get(), 0xf34415);
Public domain, as explained here. If that's unacceptable, it's also available under either the Apache-2.0 or MIT licenses, at your option.