From 4d97f1c6368a1221572888b8a94f5ca808e45059 Mon Sep 17 00:00:00 2001 From: Akash soni <33283321+akashsoni01@users.noreply.github.com> Date: Tue, 1 Jul 2025 23:43:28 +0530 Subject: [PATCH 1/2] #14 resolved --- examples/Display_example.rs | 16 ++++++++++++++++ tagged-core/src/lib.rs | 13 +++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 examples/Display_example.rs 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/tagged-core/src/lib.rs b/tagged-core/src/lib.rs index 4577924..973241a 100644 --- a/tagged-core/src/lib.rs +++ b/tagged-core/src/lib.rs @@ -1,4 +1,5 @@ use std::cmp::Ordering; +use std::fmt; use std::ops::Deref; /// 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. /// @@ -92,6 +93,18 @@ 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) + } +} + // For all common primitive types // macro_rules! impl_from_tagged { // ($($t:ty),*) => { From c7445ade9e4556715187a54d932bf8aa5cf5084e Mon Sep 17 00:00:00 2001 From: Akash soni <33283321+akashsoni01@users.noreply.github.com> Date: Tue, 1 Jul 2025 23:51:45 +0530 Subject: [PATCH 2/2] #13 #14 Added --- examples/Hash_example.rs | 15 +++++++++++++++ tagged-core/src/lib.rs | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 examples/Hash_example.rs 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 973241a..4026ce6 100644 --- a/tagged-core/src/lib.rs +++ b/tagged-core/src/lib.rs @@ -1,6 +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) @@ -105,6 +107,21 @@ impl fmt::Display for Tagged { } } +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),*) => {