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
impl LuaString
Sourcepub fn as_bytes<'a>(&self, runtime: &'a Runtime) -> Result<&'a [u8]>
pub fn as_bytes<'a>(&self, runtime: &'a Runtime) -> Result<&'a [u8]>
Borrow the exact string bytes from its runtime.
Sourcepub fn to_str<'a>(&self, runtime: &'a Runtime) -> Result<&'a str>
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
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 FromLua for LuaString
impl FromLua for LuaString
Source§fn from_lua(value: Value, runtime: &mut Runtime) -> Result<Self>
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>
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.
Auto Trait Implementations§
impl !RefUnwindSafe for LuaString
impl !Send for LuaString
impl !Sync for LuaString
impl !UnwindSafe for LuaString
impl Freeze for LuaString
impl Unpin for LuaString
impl UnsafeUnpin for LuaString
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FromLuaMulti for Twhere
T: SingleFrom,
impl<T> FromLuaMulti for Twhere
T: SingleFrom,
Source§fn from_context(context: &mut NativeContext<'_>) -> Result<T, Error>
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>
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> IntoLuaMulti for Twhere
T: SingleInto,
impl<T> IntoLuaMulti for Twhere
T: SingleInto,
Source§fn write_native(self, context: &mut NativeContext<'_>) -> Result<(), Error>
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>
fn into_lua_multi(self, runtime: &mut Runtime) -> Result<MultiValue, Error>
Convert in the target runtime.