-
Notifications
You must be signed in to change notification settings - Fork 195
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
Comments
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. #[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 :) |
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 |
Just coming back through some triage. This is a common usecase, but isn't aligned with the UUID domain enough to be something we'll support in |
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)
The text was updated successfully, but these errors were encountered: