rhai/tests/build_type.rs

148 lines
3.5 KiB
Rust
Raw Normal View History

#![cfg(not(feature = "no_object"))]
use rhai::{CustomType, Engine, EvalAltResult, Position, TypeBuilder, INT};
#[test]
fn build_type() -> Result<(), Box<EvalAltResult>> {
2022-08-29 08:27:05 +02:00
#[derive(Debug, Clone, PartialEq, Eq)]
struct Vec3 {
x: INT,
y: INT,
z: INT,
}
impl Vec3 {
fn new(x: INT, y: INT, z: INT) -> Self {
Self { x, y, z }
}
fn get_x(&mut self) -> INT {
self.x
}
fn set_x(&mut self, x: INT) {
self.x = x
}
fn get_y(&mut self) -> INT {
self.y
}
fn set_y(&mut self, y: INT) {
self.y = y
}
fn get_z(&mut self) -> INT {
self.z
}
fn set_z(&mut self, z: INT) {
self.z = z
}
fn get_component(&mut self, idx: INT) -> Result<INT, Box<EvalAltResult>> {
match idx {
0 => Ok(self.x),
1 => Ok(self.y),
2 => Ok(self.z),
_ => Err(Box::new(EvalAltResult::ErrorIndexNotFound(
idx.into(),
Position::NONE,
))),
}
}
}
2022-08-10 14:00:43 +02:00
impl IntoIterator for Vec3 {
type Item = INT;
2022-08-09 12:18:41 +02:00
type IntoIter = std::vec::IntoIter<Self::Item>;
2022-08-10 14:00:43 +02:00
fn into_iter(self) -> Self::IntoIter {
2022-08-09 12:18:41 +02:00
vec![self.x, self.y, self.z].into_iter()
2022-08-10 14:00:43 +02:00
}
}
impl CustomType for Vec3 {
fn build(mut builder: TypeBuilder<Self>) {
builder
.with_name("Vec3")
2022-08-10 14:00:43 +02:00
.is_iterable()
.with_fn("vec3", Self::new)
2022-08-29 08:27:05 +02:00
.with_fn("==", |x: &mut Vec3, y: Vec3| *x == y)
.with_fn("!=", |x: &mut Vec3, y: Vec3| *x != y)
.with_get_set("x", Self::get_x, Self::set_x)
.with_get_set("y", Self::get_y, Self::set_y)
.with_get_set("z", Self::get_z, Self::set_z);
#[cfg(not(feature = "no_index"))]
2022-08-22 16:16:26 +02:00
builder.with_indexer_get(Self::get_component);
}
}
let mut engine = Engine::new();
engine.build_type::<Vec3>();
assert_eq!(
engine.eval::<Vec3>(
"
let v = vec3(1, 2, 3);
v
",
)?,
Vec3::new(1, 2, 3),
);
assert_eq!(
engine.eval::<INT>(
"
let v = vec3(1, 2, 3);
v.x
",
)?,
1,
);
assert_eq!(
engine.eval::<INT>(
"
let v = vec3(1, 2, 3);
v.y
",
)?,
2,
);
assert_eq!(
engine.eval::<INT>(
"
let v = vec3(1, 2, 3);
v.z
",
)?,
3,
);
#[cfg(not(feature = "no_index"))]
assert!(engine.eval::<bool>(
"
let v = vec3(1, 2, 3);
v.x == v[0] && v.y == v[1] && v.z == v[2]
",
)?);
assert_eq!(
engine.eval::<Vec3>(
"
let v = vec3(1, 2, 3);
v.x = 5;
v.y = 6;
v.z = 7;
v
",
)?,
Vec3::new(5, 6, 7),
);
2022-08-10 14:00:43 +02:00
assert_eq!(
engine.eval::<INT>(
"
let sum = 0;
let v = vec3(1, 2, 3);
for i in v {
sum += i;
}
sum
",
)?,
6,
);
Ok(())
}