Skip to content

Commit 450e915

Browse files
committed
update description
1 parent 699958b commit 450e915

7 files changed

+77
-2
lines changed

src/rule/empty.rs

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ use std::iter::Map;
88
use std::marker::PhantomData;
99
use std::ops::Add;
1010

11+
/// Empty is a type that indicates that its subject is empty.
12+
/// The definition of empty is defined by EmptyDefinition.
13+
///
14+
/// # Example
15+
/// ```rust
16+
/// # use refined_type::rule::Empty;
17+
/// let empty_1 = Empty::new(0).unwrap();
18+
/// let empty_2 = Empty::new(0).unwrap();
19+
/// let empty = empty_1 + empty_2;
20+
///
21+
/// assert_eq!(empty.into_value(), 0);
22+
/// ```
1123
pub type Empty<T> = Refined<EmptyRule<T>>;
1224

1325
impl<T> Add for Empty<T>

src/rule/non_empty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use non_empty_map::*;
1313
pub use non_empty_set::*;
1414
pub use non_empty_string::*;
1515
pub use non_empty_vec::*;
16+
pub use non_empty_vec_deque::*;
1617

1718
pub type NonEmpty<T> = Refined<NonEmptyRule<T>>;
1819

src/rule/non_empty/non_empty_map.rs

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ use std::collections::hash_map::{IntoKeys, IntoValues, Keys, Values};
55
use std::collections::HashMap;
66
use std::hash::{BuildHasher, Hash, RandomState};
77

8+
/// `NonEmptyHashMap` is a `HashMap` that follows the `NonEmptyRule`
9+
/// # Example
10+
/// ```rust
11+
/// # use std::collections::{HashMap, HashSet};
12+
/// # use refined_type::rule::{NonEmptyHashMap, NonEmptyVec};
13+
/// let mut map = HashMap::new();
14+
/// map.insert("1", 1);
15+
/// map.insert("2", 2);
16+
///
17+
/// let map = NonEmptyHashMap::new(map).unwrap().insert("3", 3);
18+
/// let vec: NonEmptyVec<(&str, i32)> = map.into_iter().collect();
19+
///
20+
/// assert_eq!(
21+
/// vec.into_value().into_iter().collect::<HashSet<_>>(),
22+
/// vec![("1", 1), ("2", 2), ("3", 3)].into_iter().collect()
23+
/// );
24+
/// ```
825
pub type NonEmptyHashMap<K, V, S = RandomState> = Refined<NonEmptyHashMapRule<K, V, S>>;
926
pub type NonEmptyHashMapRule<K, V, S = RandomState> = NonEmptyRule<HashMap<K, V, S>>;
1027

src/rule/non_empty/non_empty_set.rs

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ use std::collections::hash_set::Difference;
66
use std::collections::HashSet;
77
use std::hash::{BuildHasher, Hash, RandomState};
88

9+
/// `NonEmptyHashSet` is a `HashSet` that follows the `NonEmptyRule`
10+
/// # Example
11+
/// ```rust
12+
/// # use std::collections::HashSet;
13+
/// # use refined_type::rule::NonEmptyHashSet;
14+
///
15+
/// let mut set_origin = HashSet::new();
16+
/// set_origin.insert(1);
17+
///
18+
/// let set = NonEmptyHashSet::new(set_origin.clone()).unwrap().insert(2);
19+
/// set_origin.insert(2);
20+
///
21+
/// assert_eq!(set.into_value(), set_origin);
22+
/// ```
923
pub type NonEmptyHashSet<T, S = RandomState> = Refined<NonEmptyRule<HashSet<T, S>>>;
1024
pub type NonEmptyHashSetRule<T, S = RandomState> = NonEmptyRule<HashSet<T, S>>;
1125

src/rule/non_empty/non_empty_string.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ use std::ops::Add;
55
use std::str::FromStr;
66

77
/// This is a predicate type representing a non-empty string
8+
/// # Example
9+
/// ```rust
10+
/// # use refined_type::rule::NonEmptyString;
11+
///
12+
/// let non_empty_string_1 = NonEmptyString::new("Hello".to_string()).unwrap();
13+
/// let non_empty_string_2 = NonEmptyString::new("World".to_string()).unwrap();
14+
/// let non_empty_string = non_empty_string_1 + non_empty_string_2;
15+
///
16+
/// assert_eq!(non_empty_string.into_value(), "HelloWorld");
17+
/// ```
818
pub type NonEmptyString = Refined<NonEmptyStringRule>;
19+
pub type NonEmptyStringRule = NonEmptyRule<String>;
920

1021
impl FromStr for NonEmptyString {
1122
type Err = Error<String>;
@@ -23,8 +34,6 @@ impl Add for NonEmptyString {
2334
}
2435
}
2536

26-
pub type NonEmptyStringRule = NonEmptyRule<String>;
27-
2837
#[cfg(test)]
2938
mod test {
3039
use crate::rule::{NonEmptyString, NonEmptyStringRule, Rule};

src/rule/non_empty/non_empty_vec.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ use crate::Refined;
33

44
use std::ops::Add;
55

6+
/// `NonEmptyHashMap` is a `HashMap` that follows the `NonEmptyRule`
7+
///
8+
/// # Example
9+
/// ```rust
10+
/// # use refined_type::rule::NonEmptyVec;
11+
/// let vec = NonEmptyVec::new(vec![1]).unwrap().push(2).push(3);
12+
///
13+
/// assert_eq!(vec.into_value(), vec![1, 2, 3]);
14+
/// ```
615
pub type NonEmptyVec<T> = Refined<NonEmptyVecRule<T>>;
716
pub type NonEmptyVecRule<T> = NonEmptyRule<Vec<T>>;
817

src/rule/non_empty/non_empty_vec_deque.rs

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ use crate::Refined;
33
use std::collections::VecDeque;
44
use std::ops::Add;
55

6+
/// `NonEmptyVecDeque` is a `VecDeque` that follows the `NonEmptyRule`
7+
///
8+
/// # Example
9+
/// ```rust
10+
/// # use std::collections::VecDeque;
11+
/// # use refined_type::rule::NonEmptyVecDeque;
12+
///
13+
/// let mut deque = VecDeque::new();
14+
/// deque.push_front(1);
15+
/// let deque = NonEmptyVecDeque::new(deque).unwrap().push_front(2).push_back(3);
16+
///
17+
/// assert_eq!(deque.into_value(), vec![2, 1, 3]);
18+
/// ```
619
pub type NonEmptyVecDeque<T> = Refined<NonEmptyVecDequeRule<T>>;
720
pub type NonEmptyVecDequeRule<T> = NonEmptyRule<VecDeque<T>>;
821

0 commit comments

Comments
 (0)