Skip to content

Commit d8733c7

Browse files
authored
Merge pull request #32 from tomoikey/v0.5.17
Release V0.5.17
2 parents cd2a9a5 + 8bce936 commit d8733c7

File tree

9 files changed

+140
-63
lines changed

9 files changed

+140
-63
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository = "https://github.com/tomoikey/refined_type"
66
readme = "README.md"
77
categories = ["accessibility", "development-tools", "rust-patterns"]
88
license = "MIT"
9-
version = "0.5.16"
9+
version = "0.5.17"
1010
edition = "2021"
1111

1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/refined.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ impl<RULE, T> Refined<RULE>
5757
where
5858
RULE: Rule<Item = T>,
5959
{
60+
/// Creates a new `Refined` instance if the provided value satisfies the rule.
61+
///
62+
/// # Arguments
63+
///
64+
/// * `value` - The value to be refined.
65+
///
66+
/// # Returns
67+
///
68+
/// * `Result<Self, Error<T>>` - A `Refined` instance if the value satisfies the rule, otherwise an error.
69+
///
70+
/// # Example
71+
///
72+
/// ```rust
73+
/// use refined_type::rule::NonEmptyStringRule;
74+
/// use refined_type::Refined;
75+
///
76+
/// let non_empty_string = Refined::<NonEmptyStringRule>::new("Hello".to_string());
77+
/// assert!(non_empty_string.is_ok());
78+
///
79+
/// let empty_string = Refined::<NonEmptyStringRule>::new("".to_string());
80+
/// assert!(empty_string.is_err());
81+
/// ```
6082
pub fn new(value: T) -> Result<Self, Error<T>> {
6183
let value = RULE::validate(value).map_err(|e| {
6284
let message = e.to_string();
@@ -65,6 +87,28 @@ where
6587
Ok(Self { value })
6688
}
6789

90+
/// Creates a new `Refined` instance if the provided value satisfies the rule.
91+
///
92+
/// # Arguments
93+
///
94+
/// * `value` - The value to be refined.
95+
///
96+
/// # Panics
97+
///
98+
/// This function will panic if the value does not satisfy the rule.
99+
///
100+
/// # Example
101+
///
102+
/// ```rust
103+
/// use refined_type::rule::NonEmptyStringRule;
104+
/// use refined_type::Refined;
105+
///
106+
/// let non_empty_string = Refined::<NonEmptyStringRule>::unsafe_new("Hello".to_string());
107+
/// assert_eq!(non_empty_string.into_value(), "Hello");
108+
///
109+
/// // This will panic
110+
/// // let empty_string = Refined::<NonEmptyStringRule>::unsafe_new("".to_string());
111+
/// ```
68112
pub fn unsafe_new(value: T) -> Self
69113
where
70114
T: Debug,
@@ -118,10 +162,40 @@ where
118162
Refined::new(f(self.into_value()))
119163
}
120164

165+
/// Returns a reference to the value inside the `Refined` type.
166+
///
167+
/// # Returns
168+
///
169+
/// * `&RULE::Item` - A reference to the value inside the `Refined` type.
170+
///
171+
/// # Example
172+
///
173+
/// ```rust
174+
/// use refined_type::rule::NonEmptyStringRule;
175+
/// use refined_type::Refined;
176+
///
177+
/// let non_empty_string = Refined::<NonEmptyStringRule>::new("Hello".to_string()).unwrap();
178+
/// assert_eq!(non_empty_string.value(), "Hello");
179+
/// ```
121180
pub fn value(&self) -> &RULE::Item {
122181
&self.value
123182
}
124183

184+
/// Consumes the `Refined` instance and returns the inner value.
185+
///
186+
/// # Returns
187+
///
188+
/// * `RULE::Item` - The value inside the `Refined` type.
189+
///
190+
/// # Example
191+
///
192+
/// ```rust
193+
/// use refined_type::rule::NonEmptyStringRule;
194+
/// use refined_type::Refined;
195+
///
196+
/// let non_empty_string = Refined::<NonEmptyStringRule>::new("Hello".to_string()).unwrap();
197+
/// assert_eq!(non_empty_string.into_value(), "Hello");
198+
/// ```
125199
pub fn into_value(self) -> RULE::Item {
126200
self.value
127201
}

src/rule/collection/exists.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::rule::{ForAllRule, Rule};
55
use crate::Refined;
66

77
/// A type that holds a value satisfying the `ExistsRule`
8-
pub type Exists<RULE, ITERABLE> = Refined<ExistsRule<RULE, ITERABLE, <RULE as Rule>::Item>>;
8+
pub type Exists<RULE, ITERABLE> = Refined<ExistsRule<RULE, ITERABLE>>;
99

1010
/// A type that holds a Vec value satisfying the `ExistsRule`
1111
pub type ExistsVec<RULE> = Refined<ExistsVecRule<RULE>>;
@@ -23,25 +23,22 @@ pub type ExistsHashMap<K, RULE> = Refined<ExistsHashMapRule<K, RULE>>;
2323
pub type ExistsString<RULE> = Refined<ExistsStringRule<RULE>>;
2424

2525
/// Rule where at least one data in the collection satisfies the condition
26-
pub type ExistsRule<RULE, ITERABLE, ITEM> = Not<ForAllRule<Not<RULE>, ITERABLE, ITEM>>;
26+
pub type ExistsRule<RULE, ITERABLE> = Not<ForAllRule<Not<RULE>, ITERABLE>>;
2727

2828
/// Rule where at least one data in the `Vec` satisfies the condition
29-
pub type ExistsVecRule<RULE> = ExistsRule<RULE, Vec<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
29+
pub type ExistsVecRule<RULE> = ExistsRule<RULE, Vec<<RULE as Rule>::Item>>;
3030

3131
/// Rule where at least one data in the `VecDeque` satisfies the condition
32-
pub type ExistsVecDequeRule<RULE> =
33-
ExistsRule<RULE, VecDeque<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
32+
pub type ExistsVecDequeRule<RULE> = ExistsRule<RULE, VecDeque<<RULE as Rule>::Item>>;
3433

3534
/// Rule where at least one data in the `HashSet` satisfies the condition
36-
pub type ExistsHashSetRule<RULE> =
37-
ExistsRule<RULE, HashSet<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
35+
pub type ExistsHashSetRule<RULE> = ExistsRule<RULE, HashSet<<RULE as Rule>::Item>>;
3836

3937
/// Rule where at least one data in the `HashMap` satisfies the condition
40-
pub type ExistsHashMapRule<K, RULE> =
41-
ExistsRule<RULE, HashMap<K, <RULE as Rule>::Item>, <RULE as Rule>::Item>;
38+
pub type ExistsHashMapRule<K, RULE> = ExistsRule<RULE, HashMap<K, <RULE as Rule>::Item>>;
4239

4340
/// Rule where at least one data in the `String` satisfies the condition
44-
pub type ExistsStringRule<RULE> = ExistsRule<RULE, String, char>;
41+
pub type ExistsStringRule<RULE> = ExistsRule<RULE, String>;
4542

4643
#[cfg(test)]
4744
mod tests {

src/rule/collection/for_all.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::collections::{HashMap, HashSet, VecDeque};
22

3-
use crate::rule::{NoSkip, Rule, SkipRule};
3+
use crate::rule::{Iterable, NoSkip, Rule, SkipRule};
44
use crate::Refined;
55

66
/// A type that holds a value satisfying the `ForAllRule`
7-
pub type ForAll<RULE, ITERABLE, ITEM> = Refined<ForAllRule<RULE, ITERABLE, ITEM>>;
7+
pub type ForAll<RULE, ITERABLE> = Refined<ForAllRule<RULE, ITERABLE>>;
88

99
/// A type that holds a Vec value satisfying the `ForAllRule`
1010
pub type ForAllVec<RULE> = Refined<ForAllVecRule<RULE>>;
@@ -22,25 +22,23 @@ pub type ForAllHashMap<K, RULE> = Refined<ForAllHashMapRule<K, RULE>>;
2222
pub type ForAllString<RULE> = Refined<ForAllStringRule<RULE>>;
2323

2424
/// Rule where all the data in the collection satisfies the condition
25-
pub type ForAllRule<RULE, ITERABLE, ITEM> = SkipRule<RULE, ITERABLE, NoSkip<ITEM>>;
25+
pub type ForAllRule<RULE, ITERABLE> =
26+
SkipRule<RULE, ITERABLE, NoSkip<<ITERABLE as Iterable>::Item>>;
2627

2728
/// Rule where all the data in the `Vec` satisfies the condition
28-
pub type ForAllVecRule<RULE> = ForAllRule<RULE, Vec<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
29+
pub type ForAllVecRule<RULE> = ForAllRule<RULE, Vec<<RULE as Rule>::Item>>;
2930

3031
/// Rule where all the data in the `VecDeque` satisfies the condition
31-
pub type ForAllVecDequeRule<RULE> =
32-
ForAllRule<RULE, VecDeque<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
32+
pub type ForAllVecDequeRule<RULE> = ForAllRule<RULE, VecDeque<<RULE as Rule>::Item>>;
3333

3434
/// Rule where all the data in the `HashSet` satisfies the condition
35-
pub type ForAllHashSetRule<RULE> =
36-
ForAllRule<RULE, HashSet<<RULE as Rule>::Item>, <RULE as Rule>::Item>;
35+
pub type ForAllHashSetRule<RULE> = ForAllRule<RULE, HashSet<<RULE as Rule>::Item>>;
3736

3837
/// Rule where all the data in the `HashMap` satisfies the condition
39-
pub type ForAllHashMapRule<K, RULE> =
40-
ForAllRule<RULE, HashMap<K, <RULE as Rule>::Item>, <RULE as Rule>::Item>;
38+
pub type ForAllHashMapRule<K, RULE> = ForAllRule<RULE, HashMap<K, <RULE as Rule>::Item>>;
4139

4240
/// Rule where all the data in the `String` satisfies the condition
43-
pub type ForAllStringRule<RULE> = ForAllRule<RULE, String, char>;
41+
pub type ForAllStringRule<RULE> = ForAllRule<RULE, String>;
4442

4543
#[cfg(test)]
4644
mod tests {

src/rule/collection/init.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ use crate::Refined;
33
use std::collections::VecDeque;
44

55
/// A type that holds a value satisfying the `InitRule`
6-
pub type Init<'a, RULE, ITERABLE> = Refined<InitRule<'a, RULE, ITERABLE>>;
6+
pub type Init<'a, RULE, ITERABLE> = Refined<InitRule<RULE, ITERABLE>>;
77

88
/// A type that holds a Vec value satisfying the `InitRule`
9-
pub type InitVec<'a, RULE> = Refined<InitVecRule<'a, RULE>>;
9+
pub type InitVec<RULE> = Refined<InitVecRule<RULE>>;
1010

1111
/// A type that holds a VecDeque value satisfying the `InitRule`
12-
pub type InitVecDeque<'a, RULE> = Refined<InitVecDequeRule<'a, RULE>>;
12+
pub type InitVecDeque<RULE> = Refined<InitVecDequeRule<RULE>>;
1313

1414
/// A type that holds a String value satisfying the `InitRule`
15-
pub type InitString<'a, RULE> = Refined<InitStringRule<'a, RULE>>;
15+
pub type InitString<'a, RULE> = Refined<InitStringRule<RULE>>;
1616

1717
/// Rule that applies to the initialization of a collection
18-
pub type InitRule<'a, RULE, ITERABLE> =
19-
ReverseRule<'a, SkipRule<RULE, ITERABLE, SkipFirst<<ITERABLE as Iterable<'a>>::Item>>>;
18+
pub type InitRule<RULE, ITERABLE> =
19+
ReverseRule<SkipRule<RULE, ITERABLE, SkipFirst<<ITERABLE as Iterable>::Item>>>;
2020

2121
/// Rule that applies to the initialization of a `Vec`
22-
pub type InitVecRule<'a, RULE> = InitRule<'a, RULE, Vec<<RULE as Rule>::Item>>;
22+
pub type InitVecRule<RULE> = InitRule<RULE, Vec<<RULE as Rule>::Item>>;
2323

2424
/// Rule that applies to the initialization of a `VecDeque`
25-
pub type InitVecDequeRule<'a, RULE> = InitRule<'a, RULE, VecDeque<<RULE as Rule>::Item>>;
25+
pub type InitVecDequeRule<RULE> = InitRule<RULE, VecDeque<<RULE as Rule>::Item>>;
2626

2727
/// Rule that applies to the initialization of a `String`
28-
pub type InitStringRule<'a, RULE> = InitRule<'a, RULE, String>;
28+
pub type InitStringRule<RULE> = InitRule<RULE, String>;
2929

3030
#[cfg(test)]
3131
mod tests {

src/rule/collection/iterable.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
use std::collections::VecDeque;
22

3-
pub trait Iterable<'a> {
4-
type Item: 'a;
3+
pub trait Iterable {
4+
type Item;
55

6-
fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>;
6+
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
7+
where
8+
Self: 'a;
79
fn length(&self) -> usize;
810
}
911

10-
impl<'a, T> Iterable<'a> for Vec<T>
11-
where
12-
T: 'a,
13-
{
12+
impl<T> Iterable for Vec<T> {
1413
type Item = T;
1514

16-
fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
15+
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
16+
where
17+
Self: 'a,
18+
{
1719
Box::new(self.into_iter())
1820
}
1921

@@ -22,13 +24,13 @@ where
2224
}
2325
}
2426

25-
impl<'a, T> Iterable<'a> for VecDeque<T>
26-
where
27-
T: 'a,
28-
{
27+
impl<T> Iterable for VecDeque<T> {
2928
type Item = T;
3029

31-
fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
30+
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
31+
where
32+
Self: 'a,
33+
{
3234
Box::new(self.into_iter())
3335
}
3436

@@ -37,10 +39,13 @@ where
3739
}
3840
}
3941

40-
impl<'a> Iterable<'a> for String {
42+
impl Iterable for String {
4143
type Item = char;
4244

43-
fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
45+
fn into_iterator<'a>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a>
46+
where
47+
Self: 'a,
48+
{
4449
Box::new(self.chars().collect::<Vec<_>>().into_iter())
4550
}
4651

@@ -49,10 +54,13 @@ impl<'a> Iterable<'a> for String {
4954
}
5055
}
5156

52-
impl<'a> Iterable<'a> for &'a str {
57+
impl<'a> Iterable for &'a str {
5358
type Item = char;
5459

55-
fn into_iterator(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'a> {
60+
fn into_iterator<'b>(self) -> Box<dyn DoubleEndedIterator<Item = Self::Item> + 'b>
61+
where
62+
Self: 'b,
63+
{
5664
Box::new(self.chars())
5765
}
5866

src/rule/collection/last.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ use crate::rule::{IndexRule, ReverseRule, Rule};
44
use crate::Refined;
55

66
/// A type that holds a value satisfying the `LastRule`
7-
pub type Last<'a, RULE, ITERABLE> = Refined<LastRule<'a, RULE, ITERABLE>>;
7+
pub type Last<RULE, ITERABLE> = Refined<LastRule<RULE, ITERABLE>>;
88

99
/// A type that holds a Vec value satisfying the `LastRule`
10-
pub type LastVec<'a, RULE> = Refined<LastVecRule<'a, RULE>>;
10+
pub type LastVec<RULE> = Refined<LastVecRule<RULE>>;
1111

1212
/// A type that holds a VecDeque value satisfying the `LastRule`
13-
pub type LastVecDeque<'a, RULE> = Refined<LastVecDequeRule<'a, RULE>>;
13+
pub type LastVecDeque<RULE> = Refined<LastVecDequeRule<RULE>>;
1414

1515
/// A type that holds a String value satisfying the `LastRule`
16-
pub type LastString<'a, RULE> = Refined<LastStringRule<'a, RULE>>;
16+
pub type LastString<RULE> = Refined<LastStringRule<RULE>>;
1717

1818
/// Rule where the last element satisfies the condition
19-
pub type LastRule<'a, RULE, ITERABLE> = ReverseRule<'a, IndexRule<0, RULE, ITERABLE>>;
19+
pub type LastRule<RULE, ITERABLE> = ReverseRule<IndexRule<0, RULE, ITERABLE>>;
2020

2121
/// Rule where the last element in the `Vec` satisfies the condition
22-
pub type LastVecRule<'a, RULE> = LastRule<'a, RULE, Vec<<RULE as Rule>::Item>>;
22+
pub type LastVecRule<RULE> = LastRule<RULE, Vec<<RULE as Rule>::Item>>;
2323

2424
/// Rule where the last element in the `VecDeque` satisfies the condition
25-
pub type LastVecDequeRule<'a, RULE> = LastRule<'a, RULE, VecDeque<<RULE as Rule>::Item>>;
25+
pub type LastVecDequeRule<RULE> = LastRule<RULE, VecDeque<<RULE as Rule>::Item>>;
2626

2727
/// Rule where the last element in the `String` satisfies the condition
28-
pub type LastStringRule<'a, RULE> = LastRule<'a, RULE, String>;
28+
pub type LastStringRule<RULE> = LastRule<RULE, String>;
2929

3030
#[cfg(test)]
3131
mod tests {

src/rule/collection/reverse.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ use crate::Refined;
44
use std::marker::PhantomData;
55

66
/// A type that holds a value satisfying the `ReverseRule`
7-
pub type Reverse<'a, RULE> = Refined<ReverseRule<'a, RULE>>;
7+
pub type Reverse<RULE> = Refined<ReverseRule<RULE>>;
88

99
/// Rule where the data in the collection satisfies the condition after reversing
10-
pub struct ReverseRule<'a, RULE>
10+
pub struct ReverseRule<RULE>
1111
where
1212
RULE: Rule,
1313
{
14-
_phantom_data: PhantomData<&'a RULE>,
14+
_phantom_data: PhantomData<RULE>,
1515
}
1616

17-
impl<'a, RULE, ITERABLE> Rule for ReverseRule<'a, RULE>
17+
impl<RULE, ITERABLE> Rule for ReverseRule<RULE>
1818
where
1919
RULE: Rule<Item = ITERABLE>,
20-
ITERABLE: Iterable<'a> + FromIterator<ITERABLE::Item>,
20+
ITERABLE: Iterable + FromIterator<ITERABLE::Item>,
2121
{
2222
type Item = RULE::Item;
2323

src/rule/collection/skip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ where
2929
_phantom_data: PhantomData<(RULE, ITERABLE, OPTION)>,
3030
}
3131

32-
impl<'a, RULE, ITERABLE, OPTION> Rule for SkipRule<RULE, ITERABLE, OPTION>
32+
impl<RULE, ITERABLE, OPTION> Rule for SkipRule<RULE, ITERABLE, OPTION>
3333
where
3434
RULE: Rule,
35-
ITERABLE: Iterable<'a, Item = RULE::Item> + FromIterator<RULE::Item>,
35+
ITERABLE: Iterable<Item = RULE::Item> + FromIterator<RULE::Item>,
3636
OPTION: SkipOption<Item = RULE::Item>,
3737
{
3838
type Item = ITERABLE;

0 commit comments

Comments
 (0)