Skip to main content

LuaString

Struct LuaString 

Source
pub struct LuaString(/* private fields */);
Expand description

A rooted Lua byte string. Clones share a root in one Runtime; the last drop releases it. Operations reject foreign runtime ownership. Restore preserves its logical ObjectId, but roots must be reacquired in the new runtime. See Value for an ownership example and Runtime::snapshot for checkpoints. Borrowed string/payload views end before execution.

Implementations§

Source§

impl LuaString

Source

pub fn id(&self) -> ObjectId

The logical identity, preserved across snapshots.

Source§

impl LuaString

Source

pub fn as_bytes<'a>(&self, runtime: &'a Runtime) -> Result<&'a [u8]>

Borrow the exact string bytes from its runtime.

Source

pub fn to_str<'a>(&self, runtime: &'a Runtime) -> Result<&'a str>

Borrow strict UTF-8, refusing invalid bytes.

Examples found in repository?
examples/tables_and_strings.rs (line 18)
5fn main() -> ExampleResult {
6    let mut rt = Runtime::builder().build()?;
7    let table = rt.create_table()?;
8    table.raw_set(&mut rt, "answer", 42)?;
9    table.raw_set(&mut rt, "bytes", b"a\0\xff".as_slice())?;
10    let exchange: Function = eval(&mut rt, b"return function(t) return t.answer, t.bytes end")?;
11    let (answer, bytes): (i64, Vec<u8>) =
12        done(rt.call(&exchange, &table, &mut Journal::new(), 100)?)?;
13    assert_eq!(answer, 42);
14    assert_eq!(bytes, b"a\0\xff");
15    assert_eq!(table.raw_get::<_, Vec<u8>>(&mut rt, "bytes")?, bytes);
16    let text = rt.create_string(&bytes)?;
17    assert!(matches!(
18        text.to_str(&rt),
19        Err(Error::Api(ApiError::Conversion(_)))
20    ));
21    table.raw_set(&mut rt, "numeric_text", "42.5")?;
22    assert!(matches!(
23        table.raw_get::<_, f64>(&mut rt, "numeric_text"),
24        Err(Error::Api(ApiError::Conversion(_)))
25    ));
26    let Coerce(number): Coerce<f64> = table.raw_get(&mut rt, "numeric_text")?;
27    assert_eq!(number, 42.5);
28    Ok(())
29}
More examples
Hide additional examples
examples/async_wait.rs (line 24)
5fn main() -> ExampleResult {
6    let mut registry = HostRegistry::new();
7    registry.function("example.read", NativePolicy::VmLocal, |cx| {
8        Ok(NativeReturn::Wait(WaitRequest {
9            operation: "read.asset".into(),
10            payload: MultiValue(vec![cx.arg(0).to_owned_value()?]),
11        }))
12    });
13    let mut rt = Runtime::builder().registry(registry).build()?;
14    let read = rt.make_closure("example.read", ())?;
15    let mut journal = Journal::new();
16    let CallOutcome::Waiting(key) = rt.call::<i64>(&read, "answer", &mut journal, 100)? else {
17        return Err("expected a host wait".into());
18    };
19    let request = rt.wait(key).ok_or("missing wait")?;
20    assert_eq!(request.operation, "read.asset");
21    let Value::String(name) = &request.payload[0] else {
22        return Err("expected a string".into());
23    };
24    assert_eq!(name.to_str(&rt)?, "answer");
25    // Submit work here; complete only after the host has its result.
26    rt.complete(key, Completion::Return(vec![Value::Integer(42)]))?;
27    assert_eq!(rt.run(100, &mut journal)?, StepOutcome::Completed);
28    assert_eq!(rt.finish_call::<i64>()?, 42);
29    Ok(())
30}

Trait Implementations§

Source§

impl Clone for LuaString

Source§

fn clone(&self) -> Self

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 Debug for LuaString

Source§

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

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

impl FromLua for LuaString

Source§

fn from_lua(value: Value, runtime: &mut Runtime) -> Result<Self>

Convert in the value’s runtime.
Source§

fn from_context(context: &mut NativeContext<'_>, index: usize) -> Result<Self>

Read a callback argument without rooting primitive values. Custom implementations may use the owned-value default.
Source§

impl IntoLua for LuaString

Source§

fn into_lua(self, runtime: &mut Runtime) -> Result<Value>

Convert in the target runtime.
Source§

impl IntoLua for &LuaString

Source§

fn into_lua(self, runtime: &mut Runtime) -> Result<Value>

Convert in the target runtime.

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> FromLuaMulti for T
where T: SingleFrom,

Source§

fn from_context(context: &mut NativeContext<'_>) -> Result<T, Error>

Read callback arguments directly. The default supports custom conversions; scalar and tuple implementations avoid a temporary vector.
Source§

fn from_lua_multi(values: MultiValue, runtime: &mut Runtime) -> Result<T, Error>

Convert in the values’ runtime, with one-based failure positions.
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> IntoLuaMulti for T
where T: SingleInto,

Source§

fn write_native(self, context: &mut NativeContext<'_>) -> Result<(), Error>

Write an immediate typed callback’s results. Scalar and tuple implementations reuse the native result window without an owned vector.
Source§

fn into_lua_multi(self, runtime: &mut Runtime) -> Result<MultiValue, Error>

Convert in the target runtime.
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.