Skip to main content

StbString

Struct StbString 

Source
#[repr(C)]
pub struct StbString { pub ptr: *mut c_char, pub len: usize, }
Expand description

An owned UTF-8 string crossing the ABI as a (ptr, len) pair.

Copy (raw pointers are Copy): copying a StbString duplicates the pointer, not the allocation. The receiver of a StbString owns the allocation and MUST free it exactly once by calling the producer’s FreeStringFn (or StbString::free_with / [StbString::free_host]). Never free a StbString you did not receive as an owner (e.g. one built from a borrow via [StbString::from_ref], which is non-owning — its free is a no-op only if the producer guarantees the buffer outlives the call; in practice inputs cross as StbStringRef instead).

Construction ownership contract:

Null ptr ⇒ empty (len MUST be 0). A null pointer is never dereferenced.

Fields§

§ptr: *mut c_char

UTF-8 bytes. Null when len == 0 (empty string).

§len: usize

Byte length (NOT a NUL terminator — the buffer is NOT NUL-terminated).

Implementations§

Source§

impl StbString

Source

pub const fn empty() -> Self

An empty string: null pointer, zero length. free is a no-op.

Source

pub const fn is_empty(&self) -> bool

Whether this is the empty/null string.

Source§

impl StbString

Source

pub fn from_owned(buf: Box<[u8]>) -> Self

Wrap an allocation the caller already boxed. The StbString takes ownership: a later free_with(free_fn) will deallocate it via the producer’s free_string.

The buffer MUST be UTF-8.

Source

pub fn from_string(s: String) -> Self

Convenience: from a String, transferring ownership. After this the passed String is consumed and must not be reused.

Source

pub fn from_vec(v: Vec<u8>) -> Self

Convenience: from a Vec<u8> (UTF-8), transferring ownership.

Source

pub fn from_boxed_str(s: Box<str>) -> Self

Convenience: from a boxed str.

Source

pub fn to_string_lossy(&self) -> String

Copy this StbString’s bytes into an owned String (without freeing the original — the caller still owns the original allocation). Use this to read a received StbString into safe Rust; then free the original.

Source§

impl StbString

Source

pub fn free_with(self, free_fn: Option<FreeStringFn>)

Free this StbString via the given free_string fn, if non-null and non-empty. Consumes ownership (the value is Copy, but semantically the caller relinquishes the allocation).

After this call the bytes are invalid; do not use the StbString again.

Trait Implementations§

Source§

impl Clone for StbString

Source§

fn clone(&self) -> StbString

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 StbString

Source§

impl Send for StbString

Source§

impl Sync for StbString

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