Skip to main content

StringView

Struct StringView 

Source
#[repr(C)]
pub struct StringView { pub data: *const u8, pub len: usize, }
Expand description

Non-owning string — (pointer, length) pair, no \0 terminator.

Layout identical to nonstd::string_view in the C++ SDK. 8 bytes on x86 32-bit (ptr 4 + len 4). Returned by componentName() via hidden pointer.

StringView does not take ownership — the producer guarantees the pointer’s validity for the duration of use.

Fields§

§data: *const u8§len: usize

Implementations§

Source§

impl StringView

Source

pub const EMPTY: StringView

The empty view: null data, zero length. What a hidden-pointer return slot starts as, and what the server reads as “no text”.

Source

pub const fn of(s: &str) -> StringView

A view of s to hand the server for the length of one call.

The view carries no lifetime, so it is the caller’s to keep s alive while the server reads it. Every interface that takes a StringView copies what it keeps, so the duration of the call is enough.

Source

pub const fn from_static(s: &'static str) -> StringView

Creates a StringView from a static &str, which outlives any use.

Source

pub unsafe fn to_owned_string(self) -> Option<String>

Copies the viewed bytes into a String.

None when the view is empty, null, or not UTF-8 — the server’s answer for “no text” and a malformed one read the same to a caller.

§Safety

A non-null data must point to len readable bytes.

Source

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

Converts to &str. Safe only if the pointer is valid and UTF-8.

§Safety

The pointer must be valid and point to len bytes of valid UTF-8 for the lifetime 'a.

Source

pub unsafe fn try_as_str<'a>(self) -> Result<&'a str, Utf8Error>

Converts to &str with explicit UTF-8 validation.

Preferable to as_str when the string content is untrusted or in contexts where defensive validation is needed.

§Safety

The pointer must be valid and point to len bytes for the lifetime 'a.

§Errors

std::str::Utf8Error if the pointed-to bytes do not form valid UTF-8.

Trait Implementations§

Source§

impl Clone for StringView

Source§

fn clone(&self) -> StringView

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 Copy for StringView

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

Source§

type Output = T

Should always be Self
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, 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.