Non - crate for compile time guarantees
โ Rust ๐ 2025-10-07 ๐ค surdeus ๐๏ธ 5nonis an all in one compile time guarantees crate. For now it only imlements string guarantees like NonEmpty NonBlank Alphanumericโฆ. There is a lot of crates that do something similar but non adds composite types like NonEmpty<NonBlank<ASCII>> and operations that make working on them easier.
Here is an example:
// NonEmpty rejects empty strings
let valid = NonEmpty::new("hello".to_string()).unwrap();
let invalid = NonEmpty::new(String::new());
assert!(invalid.is_none());
// ExactLength rejects strings that don't have exactly N chars
let valid = ExactLength::<2>::new("hi".to_string()).unwrap();
let invalid = ExactLength::<3>::new("helo world".to_string());
assert!(invalid.is_none());
let non_empty = NonEmpty::new("hello".to_string()).unwrap();
let s: String = non_empty.into();
// The most important feature - composed types
let non_empty = NonEmpty::new("hello".to_string()).unwrap();
let ascii_non_empty = ASCII::new(non_empty).unwrap();
// Type is: ASCII<NonEmpty<String>>
assert_eq!(ascii_non_empty.as_ref(), "hello");
// Use the [`Strip`] trait to remove one layer of wrapping:
let non_empty = NonEmpty::new("hello".to_string()).unwrap();
let ascii_non_empty = ASCII::new(non_empty).unwrap();
// Strip the NonEmpty layer: ASCII<NonEmpty<String>> -> ASCII<String>
let ascii_only = ascii_non_empty.strip();
assert_eq!(ascii_only, ASCII::new("hello".to_string()).unwrap());
I plan on adding many more types and not only for strings.
Here it is: crates.io: Rust Package Registry
1 post - 1 participant
๐ท๏ธ Rust_feed
