You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Say I have a EmailAddress newtype that I use to validate incoming requests before inserting them to the database. Now I want to read them back from the database, I have three choices:
Use the new_unchecked method. This will litter all my code with misleadingly scary unsafe { ... } blocks.
Use the try_new method. This means that additional, wasteful overhead will be incurred for every read. This also introduces a new error path that I have to take into account and propagate in all my function signatures.
Use raw types (e.g. String, u32, etc.). This is what I do right now, but I definitely don't feel satisfied with it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Say I have a
EmailAddress
newtype that I use to validate incoming requests before inserting them to the database. Now I want to read them back from the database, I have three choices:new_unchecked
method. This will litter all my code with misleadingly scaryunsafe { ... }
blocks.try_new
method. This means that additional, wasteful overhead will be incurred for every read. This also introduces a new error path that I have to take into account and propagate in all my function signatures.String
,u32
, etc.). This is what I do right now, but I definitely don't feel satisfied with it.There's a similar discussion about it in the rust-lang forum: https://users.rust-lang.org/t/a-newtype-that-has-a-constructor-but-also-a-trusted-way-to-bypass-the-constructor/68593
I'm also thinking of having a separate struct (e.g.
EmailAddressUnchecked
?) that performs no validation, but it doesn't feel idiomatic.Beta Was this translation helpful? Give feedback.
All reactions