Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a macro for generating wrapper types #768

Open
elichai opened this issue Aug 27, 2024 · 2 comments
Open

Add a macro for generating wrapper types #768

elichai opened this issue Aug 27, 2024 · 2 comments

Comments

@elichai
Copy link

elichai commented Aug 27, 2024

A lot of time you use multiple different UUIDs for different purposes in the same app, in those scenarios you want to wrap the Uuid type with your own, you start with a slim API and slowly it grows and grows for each wrapper type.
if the library could export a macro_rules macro that creates wrapper types with all the API included that could be pretty cool.
(honestly, it is starting to feel like this should be a language feature :) )

Would love to hear other opinions :) (if you're supportive of this I can find some time to draft a PR)

@jacobggman
Copy link
Contributor

jacobggman commented Dec 27, 2024

This would be also useful for me.

I think this can be implemented with generics.

Example use:

pub struct Team {
    pub name: String,
    pub uuid: TypedUuid<Team>,
}

pub struct Player {
    pub name: String,
    pub uuid: TypedUuid<Player>,
}

fn main() -> io::Result<()> {

    let team = Team { name: "Liverpool".to_string(), uuid: TypedUuid::new() };

    let mut teams_map: HashMap<TypedUuid<Team>, Team> = HashMap::new();
    teams_map.insert(team.uuid, team.clone());

    let team = Team { name: "Chelsea".to_string(), uuid: TypedUuid::new() };

    let player = Player { name: "Sadio Mane".to_string(), uuid: TypedUuid::new() };

    // ERROR: mismatched types expected struct `TypedUuid<Team>` found struct `TypedUuid<Player>`
    teams_map.insert(player.uuid, team);
    
    Ok(())
}

Also maybe if we add this feature like that a macro of creating uuid typed field for spasific struct can be useful.
Example use:

#[with_uuid]
pub struct Team {
    pub name: String,
    //  This is automatic added:  
    // pub uuid: TypedUuid<Team>,
}

I can share how I implemented this if someone would like.

Also, I would like to open PR for this if this feature is welcome :)

@KodrAus
Copy link
Member

KodrAus commented Jan 1, 2025

This is something I do as well, usually via a new-type though without many/any methods on them.

I don't think this is something we need to provide first-class support for in uuid itself, but if anyone wanted to create a library that generates wrapped uuid types for you then I think that would be a useful thing to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@elichai @KodrAus @jacobggman and others