tightness

Crates.iotightness
lib.rstightness
version1.0.1
sourcesrc
created_at2021-05-30 10:55:00.88039
updated_at2021-05-30 14:10:18.387776
descriptionDefine types bound by arbitrary invariants and conditions
homepage
repositoryhttps://github.com/PabloMansanet/tightness
max_upload_size
id403797
size21,165
(PabloMansanet)

documentation

README

Tightness

This library provides a convenient way to define types bound by arbitrary conditions.

bound!(pub Letter: char where |c| c.is_alphabetic());
bound!(XorPair: (bool, bool) where |(a, b)| a ^ b);
bound!(Username: String where |s| s.len() < 8);

The above defines three types (Letter, XorPair and Username) that are guaranteed to always fulfill the given conditions. This is enforced by checking the conditions on construction and after every mutation.

Immutably, bounded types get out of the way and act as close as possible to the underlying type, implementing all traits that a typical Newtype wrapper would.

Check out the documentation for more details!

Credit to Orson Peters (orlp in the Rust Discord) for the idea for the Bound trait, which was the seed from which this crate grew.

Caveat Emptor

This crate offers a one-size-fits-all solution. This means it's probably a lot better to use specialized restricted types when possible (e.g. the standard library's NonZeroUSize type). These types will be further specialized and offer performance and size gains, implement more traits, and maximize the number of decisions made at compile time.

If you're not particularly worried about the performance of checking the invariant after every mutation, there's no alternative in the ecosystem for the particular type restriction you want, or you just need something quick and convenient to protect your types, tightness may be for you!

Commit count: 29

cargo fmt