Replies: 1 comment
-
@musjj Sorry, I've missed your message before. I would highly recommend using If performance is really so critical for you (often it's not a case for rust web apps, because they're already sufficiently fast), you should consider using Introducing a type like |
Beta Was this translation helpful? Give feedback.
-
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