Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/Display_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use tagged_core::Tagged;


#[derive(Debug)]
struct UserIdTag {
a: Tagged<u32, Self>,
b: Tagged<u32, Self>,
}


fn main() {
let instance = UserIdTag{a: 1.into(), b: 2.into()};

println!("{}", instance.a);
println!("{:?}", instance.b);
}
15 changes: 15 additions & 0 deletions examples/Hash_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

fn main() {
use tagged_core::Tagged;
use std::collections::HashSet;

#[derive(Clone, Hash, Debug, PartialEq, Eq)]
struct User {
id: Tagged<String, Self>
}
let mut s: HashSet<User> = HashSet::new();
let user = User{id: "me@example.com".into()};
s.insert(user.clone());

assert!(s.contains(&user));
}
30 changes: 30 additions & 0 deletions tagged-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -92,6 +95,33 @@ impl<T: Ord, Tag> Ord for Tagged<T, Tag> {
}
}

impl<T: fmt::Debug, Tag> fmt::Debug for Tagged<T, Tag> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
}
}

impl<T: fmt::Display, Tag> fmt::Display for Tagged<T, Tag> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
}
}

impl<T: Clone, Tag> Clone for Tagged<T, Tag> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
_marker: std::marker::PhantomData,
}
}
}

impl<T: Hash, Tag> Hash for Tagged<T, Tag> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state)
}
}

// For all common primitive types
// macro_rules! impl_from_tagged {
// ($($t:ty),*) => {
Expand Down