#[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:
StbString::from_owned— takes aBox<[u8]>the caller allocated; theStbStringnow owns it;freedeallocates.StbString::from_boxed_str/StbString::from_string— convenience overfrom_owned(host side, needsalloc).StbString::empty— null/0;freeis a no-op.
Null ptr ⇒ empty (len MUST be 0). A null pointer is never
dereferenced.
Fields§
§ptr: *mut c_charUTF-8 bytes. Null when len == 0 (empty string).
len: usizeByte length (NOT a NUL terminator — the buffer is NOT NUL-terminated).
Implementations§
Source§impl StbString
impl StbString
Sourcepub fn from_owned(buf: Box<[u8]>) -> Self
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.
Sourcepub fn from_string(s: String) -> Self
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.
Sourcepub fn from_vec(v: Vec<u8>) -> Self
pub fn from_vec(v: Vec<u8>) -> Self
Convenience: from a Vec<u8> (UTF-8), transferring ownership.
Sourcepub fn from_boxed_str(s: Box<str>) -> Self
pub fn from_boxed_str(s: Box<str>) -> Self
Convenience: from a boxed str.
Sourcepub fn to_string_lossy(&self) -> String
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
impl StbString
Sourcepub fn free_with(self, free_fn: Option<FreeStringFn>)
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.