A Rust crate that provides a thin semantic wrapper for collections that are known to be non-empty, NonEmpty.
Using this wrapper can enhance code clarity and reduce boilerplate.
NonEmpty provides a thin wrapper around a collection object. This can improve code clarity by making a data requirement explicit in the function signature.
use nonempty_wrapper::NonEmpty;
// Implementation before NonEmpty is introduced...
fn function1(data: &[u32]) -> Result<(), ()> {
// stub implementation
if data.is_empty() {
return Err(());
}
Ok(())
}
// Now error handling is simplified and clarity is improved
fn function2(data: NonEmpty<&[u32]>) {
// stub implementation
}IsEmpty is a trait that unifies the concept of 'emptiness' in container objects. It is the semantic foundation for the prior wrapper types.
Default implementations are provided for standard types such as &str and Vec.
You may also implement IsEmpty for a custom type.
use nonempty_wrapper::{IsEmpty, NonEmpty};
struct MyType { /* ... */ }
impl IsEmpty for MyType {
fn is_empty(&self) -> bool {
// stub implementation
false
}
}
let x = NonEmpty::new(MyType {}).unwrap();