pub struct ProtoStr(/* private fields */);Expand description
A shared immutable view of a protobuf string field’s contents.
Like a str, it can be cheaply accessed as bytes and
is dynamically sized, requiring it be accessed through a pointer.
§UTF-8 and &str access
Protobuf docs state that a string field contains UTF-8 encoded text.
However, not every runtime enforces this, and the Rust runtime is designed
to integrate with other runtimes with FFI, like C++.
Because of this, in order to access the contents as a &str, users must
call ProtoStr::to_str to perform a (possibly runtime-elided) UTF-8
validation check. However, the Rust API only allows set()ting a string
field with data should be valid UTF-8 like a &str or a
&ProtoStr. This means that this check should rarely fail, but is necessary
to prevent UB when interacting with C++, which has looser restrictions.
Most of the time, users should not perform direct &str access to the
contents - this type implements Display and comparison with str,
so it’s best to avoid a UTF-8 check by working directly with &ProtoStr
or converting to &[u8].
§Display and ToString
ProtoStr is ordinarily UTF-8 and so implements Display. If there are
any invalid UTF-8 sequences, they are replaced with U+FFFD REPLACEMENT CHARACTER. Because anything implementing Display also implements
ToString, proto_str.to_string() is equivalent to
String::from_utf8_lossy(proto_str.as_bytes()).into_owned().
Implementations§
Source§impl ProtoStr
impl ProtoStr
Sourcepub const fn as_bytes(&self) -> &[u8] ⓘ
pub const fn as_bytes(&self) -> &[u8] ⓘ
Converts self to a byte slice.
Note: this type does not implement Deref; you must call as_bytes()
or AsRef<[u8]> to get access to bytes.
Sourcepub const fn to_str(&self) -> Result<&str, Utf8Error>
pub const fn to_str(&self) -> Result<&str, Utf8Error>
Yields a &str slice if self contains valid UTF-8.
This may perform a runtime check, dependent on runtime.
String::from_utf8_lossy(proto_str.as_bytes()) can be used to
infallibly construct a string, replacing invalid UTF-8 with
U+FFFD REPLACEMENT CHARACTER.
Sourcepub fn to_cow_lossy(&self) -> Cow<'_, str>
pub fn to_cow_lossy(&self) -> Cow<'_, str>
Converts self to a string, including invalid characters.
Invalid UTF-8 sequences are replaced with
U+FFFD REPLACEMENT CHARACTER.
Users should be prefer this to .to_string() provided by Display.
.to_cow_lossy() is the same operation, but it may avoid an
allocation if the string is already UTF-8.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the length of self.
Like &str, this is a length in bytes, not chars or graphemes.
Sourcepub const fn from_utf8_unchecked(bytes: &[u8]) -> &Self
pub const fn from_utf8_unchecked(bytes: &[u8]) -> &Self
Converts bytes to a &ProtoStr without a check. Prefer using .try_into()
where possible.
The input bytes should be valid UTF-8. Note that unlike with str this
method is not unsafe, as the underlying implementations are robust against
invalid UTF-8 and this will not result in language undefined behavior.
However, string fields are intended to maintain the invariant that they
contain valid UTF-8, and the system behavior if invalid UTF-8 is contained may be
poor, including that that you could end up storing malformed data which is not parsable.