Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vonr/nonn

Integer types that are known not to equal any single N.
https://github.com/vonr/nonn

Last synced: about 1 month ago
JSON representation

Integer types that are known not to equal any single N.

Awesome Lists containing this project

README

        

# nonn [![Crates.io](https://img.shields.io/crates/v/nonn)](https://crates.io/crates/nonn)

Integer types that are known not to equal any single N, enabling some memory layout optimization.

Most of nonn's code is lifted from [core::num::nonzero](https://doc.rust-lang.org/src/core/num/nonzero.rs.html)
and slightly adapted.

nonn is backed by `core::num::NonZero*`, ensuring
```rs
use std::mem::size_of;

assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
assert_eq!(size_of::>>(), size_of::());
```

nonn functions in no_std environments.

### Limitations

`NonN*` types will inevitably introduce many branches in most methods. If you require higher performance, it is
advisable to use either [core::num::nonzero](https://doc.rust-lang.org/src/core/num/nonzero.rs.html) or
the [nonmax](https://docs.rs/nonmax/latest/nonmax/) crate. These alternative options are more specialized
and can thus avoid many of the checks nonn uses.

nonn currently does not implement `Neg` for its types, as `Neg` on `NonNU*` should in theory result in `NonNI*<-N>`.
This is currently not possible to my knowledge, even on nightly.

nonn requires at least Rust 1.57.0 to compile, but 1.67.0 or above is required for `ilog2` and `ilog10`.