-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example with a newtype with generics
- Loading branch information
Showing
6 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "any_generics" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
nutype = { path = "../../nutype" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use nutype::nutype; | ||
use std::borrow::Cow; | ||
|
||
#[nutype( | ||
validate(predicate = |vec| !vec.is_empty()), | ||
derive(Debug), | ||
)] | ||
struct NotEmpty<T>(Vec<T>); | ||
|
||
#[nutype( | ||
derive(Debug), | ||
validate(predicate = |s| s.len() > 3), | ||
)] | ||
struct Clarabelle<'b>(Cow<'b, str>); | ||
|
||
fn main() { | ||
{ | ||
let v = NotEmpty::new(vec![1, 2, 3]).unwrap(); | ||
assert_eq!(v.into_inner(), vec![1, 2, 3]); | ||
} | ||
{ | ||
let err = NotEmpty::<i32>::new(vec![]).unwrap_err(); | ||
assert_eq!(err, NotEmptyError::PredicateViolated); | ||
} | ||
|
||
{ | ||
let c1 = Clarabelle::new(Cow::Borrowed("Muu")).unwrap(); | ||
assert_eq!(c1.into_inner(), Cow::Borrowed("Muu")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters