Skip to content

(Rust) Thin semantic wrapper around a collection that is known to be non-empty

Notifications You must be signed in to change notification settings

benjaminhottell/nonempty_wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

nonempty_wrapper

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

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

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();

About

(Rust) Thin semantic wrapper around a collection that is known to be non-empty

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages