I am currently writing Rust code based on the Gemini specification which specifies a particular field like this:
<META>
is a UTF-8 encoded string of maximum length 1024 bytes, whose meaning is<STATUS>
dependent.
Of course, as the transmitter of this value I can manually perform a series of steps:
- Obtain the intended value
- Assert it is valid UTF-8
- Assert it is <= 1024 bytes
- Place it in a struct for handling, or make my
new()
implementation error out if these constraints are not met
But now it seems the reins are loosened—there is no type safety that asserts the value of that field will maintain the <1024 bytes property after the struct is created. It is now just a Vec<u8>
or a String
.
In the context of my application this is somewhat pedantic, but now I'm curious—is there any convention for how to enforce maximum-length strings in types, other than this "outer" check? Particularly for a restriction based on UTF-8 bytes rather than characters?
I've searched both the forum and crates and haven't managed to pull up anything relevant.