diff --git a/examples/Display_example.rs b/examples/Display_example.rs new file mode 100644 index 0000000..d6fffd6 --- /dev/null +++ b/examples/Display_example.rs @@ -0,0 +1,16 @@ +use tagged_core::Tagged; + + +#[derive(Debug)] +struct UserIdTag { + a: Tagged, + b: Tagged, +} + + +fn main() { + let instance = UserIdTag{a: 1.into(), b: 2.into()}; + + println!("{}", instance.a); + println!("{:?}", instance.b); +} diff --git a/examples/Hash_example.rs b/examples/Hash_example.rs new file mode 100644 index 0000000..abd4c1d --- /dev/null +++ b/examples/Hash_example.rs @@ -0,0 +1,15 @@ + +fn main() { + use tagged_core::Tagged; + use std::collections::HashSet; + + #[derive(Clone, Hash, Debug, PartialEq, Eq)] + struct User { + id: Tagged + } + let mut s: HashSet = HashSet::new(); + let user = User{id: "me@example.com".into()}; + s.insert(user.clone()); + + assert!(s.contains(&user)); +} \ No newline at end of file diff --git a/tagged-core/src/lib.rs b/tagged-core/src/lib.rs index 4577924..4026ce6 100644 --- a/tagged-core/src/lib.rs +++ b/tagged-core/src/lib.rs @@ -1,5 +1,8 @@ use std::cmp::Ordering; +use std::fmt; use std::ops::Deref; +use std::hash::{Hash, Hasher}; + /// rust-tagged provides a simple way to define strongly typed wrappers over primitive types like String, i32, Uuid, chrono::DateTime, etc. It helps eliminate bugs caused by misusing raw primitives for conceptually distinct fields such as UserId, Email, ProductId, and more. /// /// Eliminate accidental mixups between similar types (e.g. OrgId vs UserId) @@ -92,6 +95,33 @@ impl Ord for Tagged { } } +impl fmt::Debug for Tagged { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.value.fmt(f) + } +} + +impl fmt::Display for Tagged { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.value.fmt(f) + } +} + +impl Clone for Tagged { + fn clone(&self) -> Self { + Self { + value: self.value.clone(), + _marker: std::marker::PhantomData, + } + } +} + +impl Hash for Tagged { + fn hash(&self, state: &mut H) { + self.value.hash(state) + } +} + // For all common primitive types // macro_rules! impl_from_tagged { // ($($t:ty),*) => {