Wrapping types for consistency - idiom?
⚓ Rust 📅 2026-04-02 👤 surdeus 👁️ 6I think there are better idioms to do what I want than I am using.
In order to avoid having lots of Rc between types, in my rolodex project (and now in the next toy I am trying), I am using identifiers through a parent which owns the long term instances. In the parent, I use a HashMap, and then the lookup methods can jsut return the
Option<T>
to reflect that a caller may have messed up getting a good identifier (even though in the usual case it will have worked. The extra check seems beneficial.)
I am current using a usize for the identifiers. What I would like to do in the new program is somehow have a type BookId, and a type AuthorId, each of which wrap a usize, but which will only be used with the appropriate lookups. So the compiler will make sure I don't try to look up a book as an author.
I think I saw a means to do this. But I am not sure what it was.
Also, if I do that, I presume I should use the wrapper type as the HashMap index, rather than merely using it on the accessor functions? (I will hide the actual implementation in case I decide to use some other mechanism.)
Thank you,
Joel
1 post - 1 participant
🏷️ Rust_feed