Skip to main content

tables_and_strings/
tables_and_strings.rs

1//! tables and strings through the public embedding API.
2use moonseed::*;
3mod support;
4use support::*;
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}
30
31#[cfg(test)]
32#[test]
33fn example_runs_and_asserts_its_result() {
34    main().unwrap();
35}