Skip to content

thibran/non_empty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

non_empty

Create around the NonEmpty struct, wrapping a non-empty value.

Version: 0.2.1

Installation

Add this to your Cargo.toml:

[dependencies.non_empty]
git = "https://github.com/thibran/non_empty.git"
tag = "v0.2.1"

In your code add:

extern crate non_empty;

Examples

use non_empty::{NonEmpty, TryNonEmpty};

let s: NonEmpty<&str> = "hello".try_non_empty().unwrap();

// use into_inner() to get the wrapped value out of a NonEmpty<T>
let inner: &str = s.into_inner();

assert_eq!("hello", inner);

Implementing TryNonEmpty for a custom struct is straightforward, just implement the is_empty() function of the IsEmpty trait.

#[derive(Clone)]
struct Point(u32, u32);

impl IsEmpty for Point {
    fn is_empty(&self) -> bool {
        self.0 == 0 && self.1 == 0
    }
}

assert!(Point(0,0).try_non_empty().is_none());

NonEmpty implements the Deref, AsRef, Borrow and Into traits to easily access its inner value.

let s: NonEmpty<String> = "hello".to_string().try_non_empty().unwrap();

// Deref NonEmpty<String> to &str
foobar(&s); 

fn foobar(s: &str) {
    assert_eq!("hello", s);
}

Tip: Use the defined type-aliases to shorten e.g. NonEmpty<String> to StringNE.

use non_empty::{StringNE, TryNonEmpty};

let s: StringNE = "hello".to_string().try_non_empty().unwrap();

Tip2: Use the provided helper functions like try_non_empty2 to convert multiple values at once to a tuple of NonEmpty's.

let (a, b): (NonEmpty<&str>, NonEmpty<i32>) = try_non_empty2("a", 1).unwrap();

assert_eq!("a", *a);
assert_eq!(1, *b);

About

Create around the NonEmpty struct, wrapping a non-empty value.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages