-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathfromtolua.rs
51 lines (47 loc) · 1.32 KB
/
fromtolua.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use rlua::{Lua, RluaCompat};
#[test]
fn test_to_array() {
Lua::new().context(|lua| {
let globals = lua.globals();
lua.load(
r#"
a = { 1, 2, 3, 4 }
"#,
)
.exec()
.unwrap();
let res = globals.get::<_, Vec<usize>>("a").unwrap();
assert_eq!(res, vec![1, 2, 3, 4]);
let res = globals.get::<_, [usize; 4]>("a").unwrap();
assert_eq!(res, [1, 2, 3, 4]);
let res = globals.get::<_, [usize; 3]>("a");
assert!(res.is_err());
let res = globals.get::<_, [usize; 5]>("a");
assert!(res.is_err());
});
}
#[test]
fn test_from_array() {
Lua::new().context(|lua| {
let globals = lua.globals();
globals.set("a", [1usize, 2, 3]).unwrap();
globals.set("v", vec![1usize, 2, 3]).unwrap();
lua.load(
r#"
correct = 0
for i=1, 3 do
if a[i] == i then
correct = correct + 1
end
if v[i] == i then
correct = correct + 1
end
end
"#,
)
.exec()
.unwrap();
let correct = globals.get::<_, usize>("correct").unwrap();
assert_eq!(correct, 6);
});
}