From 99f08b418d95c8fdaa218e2399aac6b792ac29f4 Mon Sep 17 00:00:00 2001 From: Nettika Date: Thu, 21 Jul 2022 11:33:14 -0700 Subject: [PATCH] Implement unwrapping method --- readme.md | 2 ++ src/lib.rs | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/readme.md b/readme.md index 3cbbee1..f6c0682 100644 --- a/readme.md +++ b/readme.md @@ -65,6 +65,8 @@ fn main() { // Any `Valid` passed is guaranteed // to have met the arbitrary validation checks. fn create_account(username: Valid) { + let username = username.into_inner(); // Unwrap + println!("Account {:?} created", username); } ``` diff --git a/src/lib.rs b/src/lib.rs index 4781b98..f986fd2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,13 @@ mod tests; #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct Valid(T); +impl Valid { + /// Consumes the `Valid` wrapper, returning the wrapped value. + pub fn into_inner(self) -> T { + self.0 + } +} + impl core::ops::Deref for Valid { type Target = T;