Hacker News new | ask | show | jobs
by loeg 1001 days ago
The API should restrict callers from providing bogus values at all.
1 comments

How would the API do that without more overhead than the check that GP is suggesting?
For some uses it might be reasonable to restrict the size input to compile-time constants, which can be verified at compile time. Or you could have a newtype with a checked safe constructor and an unchecked unsafe constructor, which allows bypassing the overhead when you know the sizes are reasonable. On 64-bit systems, it is reasonable in many domains to restrict allocation size to u32. There are lots of possible ways to approach this without falling back to "tolerate any usize input."