pub struct ProtoString { /* private fields */ }Expand description
An owned type representing protobuf string field’s contents.
§UTF-8
Protobuf intends to maintain the invariant that a string fields are UTF-8 encoded text, and
by default the validity of the UTF-8 encoding is enforced at parse time.
However, the Rust implementation is designed to zero-copy integrate with C++Proto. C++Proto is designed such that string fields should be valid UTF-8, and generally the validity is checked at parse time, but it is not undefined behavior to set malformed UTF-8 data. For the reason, RustProto uses a ‘should-be-UTF-8’ types, but it is not considered undefined behavior to set arbitrary &u8 onto a string field.
Doing so should be done with great caution however, as it can lead to difficult to debug issues and problems in downstream systems.
ProtoString represents a string type that is expected to contain valid
UTF-8. However, ProtoString is not validated, so users must
call [ProtoString::to_string] to perform a (possibly runtime-elided) UTF-8
validation check. This validation should rarely fail in pure Rust programs,
but is necessary to prevent UB when interacting with C++, or other languages
with looser restrictions.
§Display and ToString
ProtoString 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, ProtoString::to_string() is equivalent to
String::from_utf8_lossy(proto_string.as_bytes()).into_owned().
Implementations§
Source§impl ProtoString
impl ProtoString
pub fn as_view(&self) -> &ProtoStr
pub fn as_bytes(&self) -> &[u8] ⓘ
Sourcepub fn from_utf8_unchecked(v: &[u8]) -> Self
pub fn from_utf8_unchecked(v: &[u8]) -> Self
Converts bytes to a ProtoString 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.
Methods from Deref<Target = ProtoStr>§
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub 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 fn to_str(&self) -> Result<&str, Utf8Error>
pub 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 fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of self.
Like &str, this is a length in bytes, not chars or graphemes.