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 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.

Source

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.

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 const fn is_empty(&self) -> bool

Returns true if self has a length of zero bytes.

Source

pub const fn len(&self) -> usize

Returns the length of self.

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

Source

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.

Source

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

Interprets a string slice as a &ProtoStr.

Source

pub const fn is_ascii(&self) -> bool

Source

pub fn contains<T>(&self, other: &T) -> bool
where T: AsRef<[u8]> + ?Sized,

Source

pub fn starts_with<T>(&self, other: &T) -> bool
where T: AsRef<[u8]> + ?Sized,

Source

pub fn ends_with<T>(&self, other: &T) -> bool
where T: AsRef<[u8]> + ?Sized,

Source

pub fn find<T>(&self, other: &T) -> Option<usize>
where T: AsRef<[u8]> + ?Sized,

Source

pub const fn trim_ascii(&self) -> &Self

Source

pub const fn trim_ascii_start(&self) -> &Self

Source

pub const fn trim_ascii_end(&self) -> &Self

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 Eq for ProtoStr

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 IntoProxied<ProtoString> for &ProtoStr

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 for ProtoStr

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<ProtoStr> for str

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialEq<str> for ProtoStr

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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<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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 SealedInternal for &ProtoStr

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<'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.

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