Merge pull request #65 from tguichaoua/type_builder_iterable

with_iterator --> is_iterable
This commit is contained in:
Stephen Chung 2022-08-11 09:39:04 +08:00 committed by GitHub
commit ceaf9fab1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -49,7 +49,7 @@ fn main() -> Result<(), Box<EvalAltResult>> {
.with_fn("new_ts", Self::new)
.with_fn("update", Self::update)
.with_fn("calc", Self::calculate)
.with_iterator()
.is_iterable()
.with_get_set("x", Self::get_x, Self::set_x);
}
}

View File

@ -154,7 +154,7 @@ where
/// Register a type iterator.
/// This is an advanced API.
#[inline(always)]
pub fn with_iterator(&mut self) -> &mut Self {
pub fn is_iterable(&mut self) -> &mut Self {
self.engine.register_iterator::<T>();
self
}

View File

@ -58,8 +58,9 @@ fn build_type() -> Result<(), Box<EvalAltResult>> {
fn build(mut builder: TypeBuilder<Self>) {
builder
.with_name("Vec3")
.is_iterable()
.with_fn("vec3", Self::new)
.with_iterator()
.is_iterable()
.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);
@ -127,6 +128,19 @@ fn build_type() -> Result<(), Box<EvalAltResult>> {
)?,
Vec3::new(5, 6, 7),
);
assert_eq!(
engine.eval::<INT>(
"
let sum = 0;
let v = vec3(1, 2, 3);
for i in v {
sum += i;
}
sum
",
)?,
6,
);
Ok(())
}