Skip to main content

ProtoString

Struct ProtoString 

Source
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

Source

pub fn as_view(&self) -> &ProtoStr

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

Source

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

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 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 fn trim_ascii(&self) -> &Self

Source

pub fn trim_ascii_start(&self) -> &Self

Source

pub fn trim_ascii_end(&self) -> &Self

Trait Implementations§

Source§

impl AsRef<[u8]> for ProtoString

Source§

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

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

impl AsView for ProtoString

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 ProtoString

Source§

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

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

impl Deref for ProtoString

Source§

type Target = ProtoStr

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl EntityType for ProtoString

Source§

impl From<&str> for ProtoString

Source§

fn from(v: &str) -> Self

Converts to this type from the input type.
Source§

impl From<ProtoString> for ProtoBytes

Source§

fn from(v: ProtoString) -> Self

Converts to this type from the input type.
Source§

impl IntoProxied<ProtoString> for ProtoStringCow<'_>

Source§

impl IntoProxied<ProtoString> for &str

Source§

impl IntoProxied<ProtoString> for &ProtoStr

Source§

impl IntoProxied<ProtoString> for String

Source§

impl IntoProxied<ProtoString> for &String

Source§

impl IntoProxied<ProtoString> for OsString

Source§

impl IntoProxied<ProtoString> for &OsStr

Source§

impl IntoProxied<ProtoString> for Box<str>

Source§

impl IntoProxied<ProtoString> for Cow<'_, str>

Source§

impl IntoProxied<ProtoString> for Rc<str>

Source§

impl IntoProxied<ProtoString> for Arc<str>

Source§

impl ProtoObject for ProtoString

Source§

type View<'msg> = &'msg ProtoStr

The proxy type that provides shared access to a T, like a &'msg T. Read more
Source§

impl SealedInternal for ProtoString

Source§

impl Send for ProtoString

Source§

impl Sync for ProtoString

Source§

impl TryFrom<&[u8]> for ProtoString

Source§

type Error = Utf8Error

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

fn try_from(v: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl UpbTypeConversions<PrimitiveTag> for ProtoString

Source§

fn upb_type() -> CType

Source§

fn to_message_value(val: View<'_, ProtoString>) -> upb_MessageValue

Source§

unsafe fn into_message_value_fuse_if_required( raw_arena: NonNull<upb_Arena>, val: ProtoString, ) -> upb_MessageValue

Safety Read more
Source§

unsafe fn from_message_value<'msg>( msg: upb_MessageValue, ) -> View<'msg, ProtoString>

Safety Read more
Source§

unsafe fn copy_repeated( src: NonNull<upb_Array>, dest: NonNull<upb_Array>, arena: NonNull<upb_Arena>, )

Safety Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoProxied<T> for T
where T: ProtoObject,

Source§

fn into_proxied(self, _private: Private) -> T

Source§

impl<MessageType> MapValue for MessageType
where MessageType: ProtoObject + EntityType + UpbTypeConversions<<MessageType as EntityType>::Tag>,

Source§

fn map_new<K>(_private: Private) -> Map<K, MessageType>
where K: MapKey,

Source§

unsafe fn map_free<K>(_private: Private, _map: &mut Map<K, MessageType>)
where K: MapKey,

Source§

fn map_clear<K>(_private: Private, map: MapMut<'_, K, MessageType>)
where K: MapKey,

Source§

fn map_len<K>(_private: Private, map: MapView<'_, K, MessageType>) -> usize
where K: MapKey,

Source§

fn map_insert<K>( _private: Private, map: MapMut<'_, K, MessageType>, key: <K as ProtoObject>::View<'_>, value: impl IntoProxied<MessageType>, ) -> bool
where K: MapKey,

Source§

fn map_get<'a, K>( _private: Private, map: MapView<'a, K, MessageType>, key: <K as ProtoObject>::View<'_>, ) -> Option<<MessageType as ProtoObject>::View<'a>>
where K: MapKey,

Source§

fn map_get_mut<'a, K>( _private: Private, map: MapMut<'a, K, MessageType>, key: <K as ProtoObject>::View<'_>, ) -> Option<<MessageType as MutProtoObject>::Mut<'a>>
where K: MapKey, MessageType: Message,

Source§

fn map_remove<K>( _private: Private, map: MapMut<'_, K, MessageType>, key: <K as ProtoObject>::View<'_>, ) -> bool
where K: MapKey,

Source§

fn map_iter<K>( _private: Private, map: MapView<'_, K, MessageType>, ) -> MapIter<'_, K, MessageType> ⓘ
where K: MapKey,

Source§

fn map_iter_next<'a, K>( _private: Private, iter: &mut MapIter<'a, K, MessageType>, ) -> Option<(<K as ProtoObject>::View<'a>, <MessageType as ProtoObject>::View<'a>)>
where K: MapKey,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Singular for T

Source§

fn repeated_new(_private: Private) -> Repeated<T>

Source§

unsafe fn repeated_free(_private: Private, _repeated: &mut Repeated<T>)

Source§

fn repeated_len( _private: Private, repeated: <Repeated<T> as ProtoObject>::View<'_>, ) -> usize

Source§

fn repeated_push( _private: Private, repeated: <Repeated<T> as MutProtoObject>::Mut<'_>, val: impl IntoProxied<T>, )

Source§

fn repeated_clear( _private: Private, repeated: <Repeated<T> as MutProtoObject>::Mut<'_>, )

Source§

unsafe fn repeated_get_unchecked<'a>( _private: Private, repeated: <Repeated<T> as ProtoObject>::View<'a>, index: usize, ) -> <T as ProtoObject>::View<'a>

Source§

unsafe fn repeated_get_mut_unchecked<'a>( _private: Private, repeated: <Repeated<T> as MutProtoObject>::Mut<'a>, index: usize, ) -> <T as MutProtoObject>::Mut<'a>
where T: Message,

Source§

unsafe fn repeated_set_unchecked( _private: Private, repeated: <Repeated<T> as MutProtoObject>::Mut<'_>, index: usize, val: impl IntoProxied<T>, )

Source§

fn repeated_copy_from( _private: Private, src: <Repeated<T> as ProtoObject>::View<'_>, dest: <Repeated<T> as MutProtoObject>::Mut<'_>, )

Source§

fn repeated_reserve( _private: Private, repeated: <Repeated<T> as MutProtoObject>::Mut<'_>, additional: usize, )

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.