Skip to main content

StringView

Struct StringView 

Source
pub struct StringView<'a> { /* private fields */ }
Expand description

A borrowed ProtoCache byte string.

ProtoCache does not require string payloads to be UTF-8; use Self::as_str when text validation is required.

Implementations§

Source§

impl<'a> StringView<'a>

Source

pub fn new(words: &'a [u32]) -> Option<Self>

Parses a string/bytes object from the beginning of words.

Source

pub fn detect_len(words: &'a [u32]) -> Option<usize>

Returns the encoded object length in words without constructing a view.

Source

pub fn detect(words: &'a [u32]) -> Option<&'a [u32]>

Returns the exact encoded subslice occupied by the string/bytes object.

Source

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

Returns the payload without UTF-8 validation.

Source

pub fn as_str(self) -> Option<&'a str>

Returns the payload as text, or None if it is not valid UTF-8.

Examples found in repository?
examples/basic.rs (line 13)
3fn main() {
4    let mut buffer = Buffer::new();
5    let name = serialize_str("Ada", &mut buffer).expect("name is encodable");
6    let mut fields = [serialize_scalar(42_i32), name];
7    serialize_message(&mut fields, &mut buffer).expect("message is encodable");
8
9    let view = MessageView::new(buffer.view()).expect("valid ProtoCache message");
10    let id = view.scalar::<i32>(0).expect("id field");
11    let name = view
12        .string(1)
13        .and_then(|value| value.as_str())
14        .expect("UTF-8 name field");
15
16    println!("id={id}, name={name}");
17}
Source

pub fn as_bool_array(self) -> BoolArray<'a>

Interprets every payload byte as one boolean value.

Trait Implementations§

Source§

impl AsRef<[u8]> for StringView<'_>

Source§

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

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

impl<'a> Clone for StringView<'a>

Source§

fn clone(&self) -> StringView<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for StringView<'a>

Source§

impl<'a> Debug for StringView<'a>

Source§

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

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

impl<'a> Display for StringView<'a>

Source§

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

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

impl<'a> FieldDecode<'a> for StringView<'a>

Source§

fn decode(field: FieldView<'a>) -> Option<Self>

Returns None when the field does not have the expected shape.

Auto Trait Implementations§

§

impl<'a> Freeze for StringView<'a>

§

impl<'a> RefUnwindSafe for StringView<'a>

§

impl<'a> Send for StringView<'a>

§

impl<'a> Sync for StringView<'a>

§

impl<'a> Unpin for StringView<'a>

§

impl<'a> UnsafeUnpin for StringView<'a>

§

impl<'a> UnwindSafe for StringView<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.