From 457e69081d07346842f3335d567e823c98c05d3a Mon Sep 17 00:00:00 2001 From: Akash soni <33283321+akashsoni01@users.noreply.github.com> Date: Tue, 1 Jul 2025 23:19:46 +0530 Subject: [PATCH 1/4] eq, partialeq, ord and reexported in more ergonomics --- examples/Eq_example.rs | 32 +++++++++++++++++++++++++++ src/lib.rs | 8 ++++--- tagged-core/src/lib.rs | 49 ++++++++++++++++++++++++++++++------------ 3 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 examples/Eq_example.rs diff --git a/examples/Eq_example.rs b/examples/Eq_example.rs new file mode 100644 index 0000000..8efa2f9 --- /dev/null +++ b/examples/Eq_example.rs @@ -0,0 +1,32 @@ +use tagged_core::Tagged; + +#[test] +fn test_eq() { + let a = Tagged::::new(42); + let b = Tagged::::new(42); + let c = Tagged::::new(35); + let d = Tagged::::new(0); + + assert!(a == b); + assert!(a > c); + assert!(a >= d); + +} + +#[test] +fn test_with_struct() { + use tagged_core::Tagged; + + struct UserIdTag { + a: Tagged, + b: Tagged, + } + + + let instance = UserIdTag{a: 1.into(), b: 2.into()}; + + assert!(instance.a < instance.b); + + // println!("{}", a); + +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d0730d9..5ee3a72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ -pub mod rust_tagged{ - pub use tagged_core::*; -} \ No newline at end of file +// pub mod rust_tagged{ +// pub use tagged_core::*; +// } + +pub use tagged_core::*; diff --git a/tagged-core/src/lib.rs b/tagged-core/src/lib.rs index aaaf002..4577924 100644 --- a/tagged-core/src/lib.rs +++ b/tagged-core/src/lib.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; 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. /// @@ -8,7 +9,7 @@ use std::ops::Deref; /// # Example /// /// ``` -/// use rust_tagged::{Tagged}; +/// use tagged_core::{Tagged}; /// /// #[derive(Debug)] /// struct EmailTag; @@ -24,12 +25,12 @@ use std::ops::Deref; /// println!("Raw String: {raw}"); /// } /// ``` -pub struct Tagged { +pub struct Tagged { value: T, - _marker: std::marker::PhantomData, + _marker: std::marker::PhantomData, } -impl Tagged { +impl Tagged { pub fn new(value: T) -> Self { Self { value, @@ -43,41 +44,60 @@ impl Tagged { } -/// Blanket `From` for `Tagged` -impl From for Tagged { +/// Blanket `From` for `Tagged` +impl From for Tagged { fn from(value: T) -> Self { Tagged::new(value) } } -/// Support `From<&str>` → `Tagged` -impl From<&str> for Tagged { +/// Support `From<&str>` → `Tagged` +impl From<&str> for Tagged { fn from(s: &str) -> Self { Tagged::new(s.to_string()) } } -/// Support `From<&String>` → `Tagged` -impl From<&String> for Tagged { +/// Support `From<&String>` → `Tagged` +impl From<&String> for Tagged { fn from(s: &String) -> Self { Tagged::new(s.clone()) } } -impl Deref for Tagged { +impl Deref for Tagged { type Target = T; fn deref(&self) -> &Self::Target { &self.value } } +impl PartialEq for Tagged { + fn eq(&self, other: &Self) -> bool { + self.value == other.value + } +} + +impl Eq for Tagged {} + +impl PartialOrd for Tagged { + fn partial_cmp(&self, other: &Self) -> Option { + self.value.partial_cmp(&other.value) + } +} + +impl Ord for Tagged { + fn cmp(&self, other: &Self) -> Ordering { + self.value.cmp(&other.value) + } +} // For all common primitive types // macro_rules! impl_from_tagged { // ($($t:ty),*) => { // $( -// impl From> for $t { -// fn from(tagged: Tagged<$t, U>) -> Self { +// impl From> for $t { +// fn from(tagged: Tagged<$t, Tag>) -> Self { // tagged.value // } // } @@ -111,4 +131,5 @@ mod tests { assert_eq!(tagged_struct.id.value, 0); } -} \ No newline at end of file +} + From 301427adeba82f35bd4d631c66fbabbb5dea0dc4 Mon Sep 17 00:00:00 2001 From: Akash soni <33283321+akashsoni01@users.noreply.github.com> Date: Tue, 1 Jul 2025 23:22:07 +0530 Subject: [PATCH 2/4] #12 #17 resolved --- tagged-core/examples/Eq_example.rs | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tagged-core/examples/Eq_example.rs diff --git a/tagged-core/examples/Eq_example.rs b/tagged-core/examples/Eq_example.rs new file mode 100644 index 0000000..8efa2f9 --- /dev/null +++ b/tagged-core/examples/Eq_example.rs @@ -0,0 +1,32 @@ +use tagged_core::Tagged; + +#[test] +fn test_eq() { + let a = Tagged::::new(42); + let b = Tagged::::new(42); + let c = Tagged::::new(35); + let d = Tagged::::new(0); + + assert!(a == b); + assert!(a > c); + assert!(a >= d); + +} + +#[test] +fn test_with_struct() { + use tagged_core::Tagged; + + struct UserIdTag { + a: Tagged, + b: Tagged, + } + + + let instance = UserIdTag{a: 1.into(), b: 2.into()}; + + assert!(instance.a < instance.b); + + // println!("{}", a); + +} \ No newline at end of file From 32577231c1ec00be4955ff6895a7631064b05273 Mon Sep 17 00:00:00 2001 From: Akash soni <33283321+akashsoni01@users.noreply.github.com> Date: Tue, 1 Jul 2025 23:26:06 +0530 Subject: [PATCH 3/4] pipeline fixec --- examples/Eq_example.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/Eq_example.rs b/examples/Eq_example.rs index 8efa2f9..fdea5b2 100644 --- a/examples/Eq_example.rs +++ b/examples/Eq_example.rs @@ -1,5 +1,7 @@ use tagged_core::Tagged; +fn main() { +} #[test] fn test_eq() { let a = Tagged::::new(42); From 4029c079771fcc270f13fc26b45e2f3a703ba366 Mon Sep 17 00:00:00 2001 From: Akash soni <33283321+akashsoni01@users.noreply.github.com> Date: Tue, 1 Jul 2025 23:28:06 +0530 Subject: [PATCH 4/4] pipeline fixed --- tagged-core/examples/Eq_example.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tagged-core/examples/Eq_example.rs b/tagged-core/examples/Eq_example.rs index 8efa2f9..0fd41fc 100644 --- a/tagged-core/examples/Eq_example.rs +++ b/tagged-core/examples/Eq_example.rs @@ -1,5 +1,5 @@ use tagged_core::Tagged; - +fn main() {} #[test] fn test_eq() { let a = Tagged::::new(42);