Daily Shaarli

All links of one day in a single page.

February 5, 2024

Phantom Types | dsp
thumbnail
struct Id<T> {
    inner: u64,
    _marker: std::marker::PhantomData<T>,
}
impl<T> Id<T> {
    pub fn new(inner: u64) -> Self { 
       Self {
           inner, 
           _marker: std::marker::PhantomData
        }
    }
}
struct UserIdMarker;
struct GroupIdMarker;

pub type UserId = Id<UserIdMarker>;
pub type GroupId = Id<GroupIdMarker>;
//...

fn ping_user(id: UserId) {
    //...
}
ping_user(GroupId::new(12345)); // This triggers an error