pub struct LuaString { /* private fields */ }Expand description
Lua’s immutable byte-string value.
The byte payload is a Box<[u8]>, NOT an Rc<[u8]>. Strings are immutable
and GC-owned: every live LuaString is reached through a GcRef<LuaString>
(the interner stores GcRefs, LuaValue::Str holds a GcRef), and all
value-level sharing happens at that GcRef layer. An Rc<[u8]> would
co-locate a 16-byte refcount header (strong + weak counts) with the payload
in the string’s heap allocation, so every string allocation paid those 16
bytes on top of its GcBox<LuaString> for a refcount machinery nothing
uses. Switching to Box<[u8]> drops the 16-byte header per string and the
refcount inc/dec traffic; the win is in the heap allocation, not the struct
field (both Rc<[u8]> and Box<[u8]> are 16-byte fat pointers).
The #[derive(Clone)] is retained, but a by-value LuaString clone is now
a deep copy (alloc + memcpy) rather than a refcount bump. This is acceptable
because no hot path clones a LuaString by value — hot sharing goes through
the Copy GcRef<LuaString> handle. The only by-value clones are cold
(error-message construction, GlobalState init).
Implementations§
Source§impl LuaString
impl LuaString
pub fn from_bytes(b: Vec<u8>) -> Self
Sourcepub fn from_slice(b: &[u8]) -> Self
pub fn from_slice(b: &[u8]) -> Self
Construct directly from a borrowed slice with a single allocating copy.
from_bytes takes an owned Vec<u8>, but Vec<u8> -> Box<[u8]> via
into_boxed_slice only adopts the existing buffer when it is exactly
full, otherwise it reallocates; a caller holding only a slice would copy
twice (once into a Vec, once into the Box). Box::from(&[u8]) copies
the slice straight into the final allocation, matching C’s single
luaS_newlstr allocation per string. Hash is computed with the same
algorithm as from_bytes.
pub fn placeholder() -> Self
pub fn as_bytes(&self) -> &[u8] ⓘ
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn is_short(&self) -> bool
pub fn is_long(&self) -> bool
pub fn hash(&self) -> u32
pub fn buffer_bytes(&self) -> usize
pub fn is_reserved_word(&self) -> bool
pub fn hash_bytes(bytes: &[u8], seed: u32) -> u32
pub fn hash_long(&mut self) -> u32
Trait Implementations§
impl Eq for LuaString
Source§impl Trace for LuaString
LuaString — interned byte string. The Rc<[u8]> backing buffer is
owned, not GC-managed, so this impl is intentionally empty.
impl Trace for LuaString
LuaString — interned byte string. The Rc<[u8]> backing buffer is
owned, not GC-managed, so this impl is intentionally empty.
Source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
Heap::type_name_count). Collector behavior must not branch on
this. The default covers container blanket impls, which are never
GC-boxed directly; concrete runtime types override it with
std::any::type_name::<Self>().