[go: up one dir, main page]

Skip to main content

ProtoStr

Struct ProtoStr 

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn is_empty(&self) -> bool

Returns true if self has a length of zero bytes.

Source

pub fn len(&self) -> usize

Returns the length of self.

Like &str, this is a length in bytes, not chars or graphemes.

Source

pub fn chars(&self) -> impl Iterator<Item = char> + '_ + Debug

Iterates over the chars in this protobuf string.

Invalid UTF-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.

Source

pub fn utf8_chunks(&self) -> impl Iterator<Item = Result<&str, &[u8]>> + '_

Returns an iterator over chunks of UTF-8 data in the string.

An Ok(&str) is yielded for every valid UTF-8 chunk, and an Err(&[u8]) for each non-UTF-8 chunk. An Err will be emitted multiple times in a row for contiguous invalid chunks. Each invalid chunk in an Err has a maximum length of 3 bytes.

Source

pub unsafe fn from_utf8_unchecked(bytes: &[u8]) -> &Self

Converts known-UTF-8 bytes to a ProtoStr without a check.

§Safety

bytes must be valid UTF-8 if the current runtime requires it.

Source

pub fn from_str(string: &str) -> &Self

Interprets a string slice as a &ProtoStr.

Trait Implementations§

Source§

impl AsRef<[u8]> for ProtoStr

Source§

fn as_ref(&self) -> &[u8] ⓘ

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsView for &ProtoStr

Source§

type Proxied = ProtoString

Source§

fn as_view(&self) -> &ProtoStr

Converts a borrow into a View with the lifetime of that borrow. Read more
Source§

impl Debug for ProtoStr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ProtoStr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'msg> From<&'msg ProtoStr> for &'msg [u8]

Source§

fn from(val: &'msg ProtoStr) -> &'msg [u8] ⓘ

Converts to this type from the input type.
Source§

impl<'msg> From<&'msg str> for &'msg ProtoStr

Source§

fn from(val: &'msg str) -> &'msg ProtoStr

Converts to this type from the input type.
Source§

impl Hash for ProtoStr

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
Source§

impl<'msg> IntoView<'msg> for &'msg ProtoStr

Source§

fn into_view<'shorter>(self) -> &'shorter ProtoStr
where 'msg: 'shorter,

Converts into a View with a potentially shorter lifetime. Read more
Source§

impl Ord for ProtoStr

Source§

fn cmp(&self, other: &ProtoStr) -> Ordering

This method returns an Ordering between self and other. Read more
Source§

impl PartialEq<ProtoStr> for str

Source§

fn eq(&self, other: &ProtoStr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for ProtoStr

Source§

fn eq(&self, other: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for ProtoStr

Source§

fn eq(&self, other: &ProtoStr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd<ProtoStr> for str

Source§

fn partial_cmp(&self, other: &ProtoStr) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd<str> for ProtoStr

Source§

fn partial_cmp(&self, other: &str) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd for ProtoStr

Source§

fn partial_cmp(&self, other: &ProtoStr) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'msg> TryFrom<&'msg [u8]> for &'msg ProtoStr

Source§

type Error = Utf8Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: &'msg [u8]) -> Result<&'msg ProtoStr, Utf8Error>

Performs the conversion.
Source§

impl<'msg> TryFrom<&'msg ProtoStr> for &'msg str

Source§

type Error = Utf8Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: &'msg ProtoStr) -> Result<&'msg str, Utf8Error>

Performs the conversion.
Source§

impl Eq for ProtoStr

Source§

impl IntoProxied<ProtoString> for &ProtoStr

Source§

impl<'msg> Proxy<'msg> for &'msg ProtoStr

Source§

impl SealedInternal for &ProtoStr

Source§

impl<'msg> ViewProxy<'msg> for &'msg ProtoStr

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more