diff --git a/Cargo.lock b/Cargo.lock index d078757..99a27d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -162,6 +162,13 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "new_unchecked_example" +version = "0.1.0" +dependencies = [ + "nutype", +] + [[package]] name = "num-traits" version = "0.2.16" diff --git a/examples/new_unchecked_example/Cargo.toml b/examples/new_unchecked_example/Cargo.toml new file mode 100644 index 0000000..25f5010 --- /dev/null +++ b/examples/new_unchecked_example/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "new_unchecked_example" +version = "0.1.0" +edition = "2021" +publish = false +authors = ["Serhii Potapov "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nutype = { path = "../../nutype", features = ["new_unchecked"] } diff --git a/examples/new_unchecked_example/src/main.rs b/examples/new_unchecked_example/src/main.rs new file mode 100644 index 0000000..4fb561d --- /dev/null +++ b/examples/new_unchecked_example/src/main.rs @@ -0,0 +1,17 @@ +//! This example illustrates usage of `new_unchecked` feature that allows to create a value of +//! the type, bypassing the sanitization and validation checks. +//! +//! Use it carefully. + +use nutype::nutype; + +#[nutype(new_unchecked, sanitize(trim), validate(not_empty))] +pub struct Name(String); + +fn main() { + // Yes, you're forced to use `unsafe` here, so everyone will point fingers at YOU. + let name = unsafe { Name::new_unchecked(" ".to_string()) }; + + // `name` violates the sanitization and validation rules!!! + assert_eq!(name.into_inner(), " "); +}