Fix up tests and some coding style changes.
This commit is contained in:
parent
438dffef78
commit
d9a58907d9
@ -1,12 +1,16 @@
|
|||||||
use core::marker::PhantomData;
|
//! Trait to build a custom type for use with [`Engine`].
|
||||||
|
#![allow(deprecated)]
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
func::SendSync, types::dynamic::Variant, Engine, Identifier, RegisterNativeFunction,
|
func::SendSync, types::dynamic::Variant, Engine, Identifier, RegisterNativeFunction,
|
||||||
RhaiResultOf,
|
RhaiResultOf,
|
||||||
};
|
};
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
#[cfg(feature = "no_std")]
|
||||||
|
use std::prelude::v1::*;
|
||||||
|
|
||||||
/// Trait to build a custom type for use with the [`Engine`].
|
/// Trait to build a custom type for use with an [`Engine`]
|
||||||
/// i.e. register the type and its getters, setters, methods, etc...
|
/// (i.e. register the type and its getters, setters, methods, etc.).
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
@ -58,49 +62,44 @@ use crate::{
|
|||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[deprecated = "This trait is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||||
pub trait CustomType: Variant + Clone {
|
pub trait CustomType: Variant + Clone {
|
||||||
/// Builds the custom type for use with the [`Engine`].
|
/// Builds the custom type for use with the [`Engine`].
|
||||||
/// i.e. register the type, getters, setters, methods, etc...
|
///
|
||||||
|
/// Methods, property getters/setters, indexers etc. should be registered in this function.
|
||||||
fn build(builder: TypeBuilder<Self>);
|
fn build(builder: TypeBuilder<Self>);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Engine {
|
impl Engine {
|
||||||
/// Build a custom type for use with the [`Engine`].
|
/// Build a custom type for use with the [`Engine`].
|
||||||
/// i.e. register the type and its getters, setters, methods, etc...
|
|
||||||
///
|
///
|
||||||
/// See [`CustomType`].
|
/// The custom type must implement [`CustomType`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn build_type<T>(&mut self) -> &mut Self
|
pub fn build_type<T: CustomType>(&mut self) -> &mut Self {
|
||||||
where
|
|
||||||
T: CustomType,
|
|
||||||
{
|
|
||||||
T::build(TypeBuilder::new(self));
|
T::build(TypeBuilder::new(self));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builder to build a custom type i.e. register this type and its getters, setters, methods, etc...
|
/// Builder to build a custom type for use with an [`Engine`].
|
||||||
///
|
///
|
||||||
/// The type is automatically registered when this builder is dropped.
|
/// The type is automatically registered when this builder is dropped.
|
||||||
///
|
///
|
||||||
/// ## Pretty name
|
/// ## Pretty name
|
||||||
/// By default the type is registered with [`Engine::register_type`] i.e. without a pretty name.
|
|
||||||
///
|
///
|
||||||
/// To define a pretty name call `.with_name`, in this case [`Engine::register_type_with_name`] will be used.
|
/// By default the type is registered with [`Engine::register_type`] (i.e. without a pretty name).
|
||||||
pub struct TypeBuilder<'a, T>
|
///
|
||||||
where
|
/// To define a pretty name, call [`with_name`][`TypeBuilder::with_name`],
|
||||||
T: Variant + Clone,
|
/// to use [`Engine::register_type_with_name`] instead.
|
||||||
{
|
#[deprecated = "This type is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||||
|
pub struct TypeBuilder<'a, T: Variant + Clone> {
|
||||||
engine: &'a mut Engine,
|
engine: &'a mut Engine,
|
||||||
name: Option<&'static str>,
|
name: Option<&'static str>,
|
||||||
_marker: PhantomData<T>,
|
_marker: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> TypeBuilder<'a, T>
|
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||||
where
|
#[inline(always)]
|
||||||
T: Variant + Clone,
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn new(engine: &'a mut Engine) -> Self {
|
fn new(engine: &'a mut Engine) -> Self {
|
||||||
Self {
|
Self {
|
||||||
engine,
|
engine,
|
||||||
@ -110,21 +109,16 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> TypeBuilder<'a, T>
|
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||||
where
|
|
||||||
T: Variant + Clone,
|
|
||||||
{
|
|
||||||
/// Sets a pretty-print name for the `type_of` function.
|
/// Sets a pretty-print name for the `type_of` function.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_name(&mut self, name: &'static str) -> &mut Self {
|
pub fn with_name(&mut self, name: &'static str) -> &mut Self {
|
||||||
self.name = Some(name);
|
self.name = Some(name);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register a custom function.
|
/// Register a custom function.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_fn<N, A, F>(&mut self, name: N, method: F) -> &mut Self
|
pub fn with_fn<N, A, F>(&mut self, name: N, method: F) -> &mut Self
|
||||||
where
|
where
|
||||||
N: AsRef<str> + Into<Identifier>,
|
N: AsRef<str> + Into<Identifier>,
|
||||||
@ -135,8 +129,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Register a custom fallible function.
|
/// Register a custom fallible function.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_result_fn<N, A, F, R>(&mut self, name: N, method: F) -> &mut Self
|
pub fn with_result_fn<N, A, F, R>(&mut self, name: N, method: F) -> &mut Self
|
||||||
where
|
where
|
||||||
N: AsRef<str> + Into<Identifier>,
|
N: AsRef<str> + Into<Identifier>,
|
||||||
@ -148,17 +141,13 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
impl<'a, T> TypeBuilder<'a, T>
|
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||||
where
|
|
||||||
T: Variant + Clone,
|
|
||||||
{
|
|
||||||
/// Register a getter function.
|
/// Register a getter function.
|
||||||
///
|
///
|
||||||
/// The function signature must start with `&mut self` and not `&self`.
|
/// The function signature must start with `&mut self` and not `&self`.
|
||||||
///
|
///
|
||||||
/// Not available under `no_object`.
|
/// Not available under `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_get<V: Variant + Clone>(
|
pub fn with_get<V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: impl AsRef<str>,
|
name: impl AsRef<str>,
|
||||||
@ -173,8 +162,7 @@ where
|
|||||||
/// The function signature must start with `&mut self` and not `&self`.
|
/// The function signature must start with `&mut self` and not `&self`.
|
||||||
///
|
///
|
||||||
/// Not available under `no_object`.
|
/// Not available under `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_get_result<V: Variant + Clone>(
|
pub fn with_get_result<V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: impl AsRef<str>,
|
name: impl AsRef<str>,
|
||||||
@ -187,8 +175,7 @@ where
|
|||||||
/// Register a setter function.
|
/// Register a setter function.
|
||||||
///
|
///
|
||||||
/// Not available under `no_object`.
|
/// Not available under `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_set<V: Variant + Clone>(
|
pub fn with_set<V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: impl AsRef<str>,
|
name: impl AsRef<str>,
|
||||||
@ -201,8 +188,7 @@ where
|
|||||||
/// Register a fallible setter function.
|
/// Register a fallible setter function.
|
||||||
///
|
///
|
||||||
/// Not available under `no_object`.
|
/// Not available under `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_set_result<V: Variant + Clone>(
|
pub fn with_set_result<V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: impl AsRef<str>,
|
name: impl AsRef<str>,
|
||||||
@ -217,8 +203,7 @@ where
|
|||||||
/// All function signatures must start with `&mut self` and not `&self`.
|
/// All function signatures must start with `&mut self` and not `&self`.
|
||||||
///
|
///
|
||||||
/// Not available under `no_object`.
|
/// Not available under `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_get_set<V: Variant + Clone>(
|
pub fn with_get_set<V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: impl AsRef<str>,
|
name: impl AsRef<str>,
|
||||||
@ -231,17 +216,13 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||||
impl<'a, T> TypeBuilder<'a, T>
|
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||||
where
|
|
||||||
T: Variant + Clone,
|
|
||||||
{
|
|
||||||
/// Register an index getter.
|
/// Register an index getter.
|
||||||
///
|
///
|
||||||
/// The function signature must start with `&mut self` and not `&self`.
|
/// The function signature must start with `&mut self` and not `&self`.
|
||||||
///
|
///
|
||||||
/// Not available under both `no_index` and `no_object`.
|
/// Not available under both `no_index` and `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_indexer_get<X: Variant + Clone, V: Variant + Clone>(
|
pub fn with_indexer_get<X: Variant + Clone, V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
get_fn: impl Fn(&mut T, X) -> V + SendSync + 'static,
|
get_fn: impl Fn(&mut T, X) -> V + SendSync + 'static,
|
||||||
@ -255,8 +236,7 @@ where
|
|||||||
/// The function signature must start with `&mut self` and not `&self`.
|
/// The function signature must start with `&mut self` and not `&self`.
|
||||||
///
|
///
|
||||||
/// Not available under both `no_index` and `no_object`.
|
/// Not available under both `no_index` and `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_indexer_get_result<X: Variant + Clone, V: Variant + Clone>(
|
pub fn with_indexer_get_result<X: Variant + Clone, V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
get_fn: impl Fn(&mut T, X) -> RhaiResultOf<V> + SendSync + 'static,
|
get_fn: impl Fn(&mut T, X) -> RhaiResultOf<V> + SendSync + 'static,
|
||||||
@ -268,8 +248,7 @@ where
|
|||||||
/// Register an index setter.
|
/// Register an index setter.
|
||||||
///
|
///
|
||||||
/// Not available under both `no_index` and `no_object`.
|
/// Not available under both `no_index` and `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_indexer_set<X: Variant + Clone, V: Variant + Clone>(
|
pub fn with_indexer_set<X: Variant + Clone, V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
set_fn: impl Fn(&mut T, X, V) + SendSync + 'static,
|
set_fn: impl Fn(&mut T, X, V) + SendSync + 'static,
|
||||||
@ -281,8 +260,7 @@ where
|
|||||||
/// Register an fallible index setter.
|
/// Register an fallible index setter.
|
||||||
///
|
///
|
||||||
/// Not available under both `no_index` and `no_object`.
|
/// Not available under both `no_index` and `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_indexer_set_result<X: Variant + Clone, V: Variant + Clone>(
|
pub fn with_indexer_set_result<X: Variant + Clone, V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
set_fn: impl Fn(&mut T, X, V) -> RhaiResultOf<()> + SendSync + 'static,
|
set_fn: impl Fn(&mut T, X, V) -> RhaiResultOf<()> + SendSync + 'static,
|
||||||
@ -294,8 +272,7 @@ where
|
|||||||
/// Short-hand for registering both index getter and setter functions.
|
/// Short-hand for registering both index getter and setter functions.
|
||||||
///
|
///
|
||||||
/// Not available under both `no_index` and `no_object`.
|
/// Not available under both `no_index` and `no_object`.
|
||||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
#[inline(always)]
|
||||||
#[inline]
|
|
||||||
pub fn with_indexer_get_set<X: Variant + Clone, V: Variant + Clone>(
|
pub fn with_indexer_get_set<X: Variant + Clone, V: Variant + Clone>(
|
||||||
&mut self,
|
&mut self,
|
||||||
get_fn: impl Fn(&mut T, X) -> V + SendSync + 'static,
|
get_fn: impl Fn(&mut T, X) -> V + SendSync + 'static,
|
||||||
@ -306,10 +283,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Drop for TypeBuilder<'a, T>
|
impl<'a, T: Variant + Clone> Drop for TypeBuilder<'a, T> {
|
||||||
where
|
|
||||||
T: Variant + Clone,
|
|
||||||
{
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(name) = self.name {
|
if let Some(name) = self.name {
|
||||||
|
@ -1,37 +1,38 @@
|
|||||||
use rhai::{CustomType, Engine, EvalAltResult, Position, TypeBuilder};
|
#![cfg(not(feature = "no_object"))]
|
||||||
|
use rhai::{CustomType, Engine, EvalAltResult, Position, TypeBuilder, INT};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn build_type() -> Result<(), Box<EvalAltResult>> {
|
fn build_type() -> Result<(), Box<EvalAltResult>> {
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
struct Vec3 {
|
struct Vec3 {
|
||||||
x: i64,
|
x: INT,
|
||||||
y: i64,
|
y: INT,
|
||||||
z: i64,
|
z: INT,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vec3 {
|
impl Vec3 {
|
||||||
fn new(x: i64, y: i64, z: i64) -> Self {
|
fn new(x: INT, y: INT, z: INT) -> Self {
|
||||||
Self { x, y, z }
|
Self { x, y, z }
|
||||||
}
|
}
|
||||||
fn get_x(&mut self) -> i64 {
|
fn get_x(&mut self) -> INT {
|
||||||
self.x
|
self.x
|
||||||
}
|
}
|
||||||
fn set_x(&mut self, x: i64) {
|
fn set_x(&mut self, x: INT) {
|
||||||
self.x = x
|
self.x = x
|
||||||
}
|
}
|
||||||
fn get_y(&mut self) -> i64 {
|
fn get_y(&mut self) -> INT {
|
||||||
self.y
|
self.y
|
||||||
}
|
}
|
||||||
fn set_y(&mut self, y: i64) {
|
fn set_y(&mut self, y: INT) {
|
||||||
self.y = y
|
self.y = y
|
||||||
}
|
}
|
||||||
fn get_z(&mut self) -> i64 {
|
fn get_z(&mut self) -> INT {
|
||||||
self.z
|
self.z
|
||||||
}
|
}
|
||||||
fn set_z(&mut self, z: i64) {
|
fn set_z(&mut self, z: INT) {
|
||||||
self.z = z
|
self.z = z
|
||||||
}
|
}
|
||||||
fn get_component(&mut self, idx: i64) -> Result<i64, Box<EvalAltResult>> {
|
fn get_component(&mut self, idx: INT) -> Result<INT, Box<EvalAltResult>> {
|
||||||
match idx {
|
match idx {
|
||||||
0 => Ok(self.x),
|
0 => Ok(self.x),
|
||||||
1 => Ok(self.y),
|
1 => Ok(self.y),
|
||||||
@ -51,8 +52,10 @@ fn build_type() -> Result<(), Box<EvalAltResult>> {
|
|||||||
.with_fn("vec3", Self::new)
|
.with_fn("vec3", Self::new)
|
||||||
.with_get_set("x", Self::get_x, Self::set_x)
|
.with_get_set("x", Self::get_x, Self::set_x)
|
||||||
.with_get_set("y", Self::get_y, Self::set_y)
|
.with_get_set("y", Self::get_y, Self::set_y)
|
||||||
.with_get_set("z", Self::get_z, Self::set_z)
|
.with_get_set("z", Self::get_z, Self::set_z);
|
||||||
.with_indexer_get_result(Self::get_component);
|
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
builder.with_indexer_get_result(Self::get_component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,55 +64,56 @@ fn build_type() -> Result<(), Box<EvalAltResult>> {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<Vec3>(
|
engine.eval::<Vec3>(
|
||||||
r#"
|
"
|
||||||
let v = vec3(1, 2, 3);
|
let v = vec3(1, 2, 3);
|
||||||
v
|
v
|
||||||
"#,
|
",
|
||||||
)?,
|
)?,
|
||||||
Vec3::new(1, 2, 3),
|
Vec3::new(1, 2, 3),
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<i64>(
|
engine.eval::<INT>(
|
||||||
r#"
|
"
|
||||||
let v = vec3(1, 2, 3);
|
let v = vec3(1, 2, 3);
|
||||||
v.x
|
v.x
|
||||||
"#,
|
",
|
||||||
)?,
|
)?,
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<i64>(
|
engine.eval::<INT>(
|
||||||
r#"
|
"
|
||||||
let v = vec3(1, 2, 3);
|
let v = vec3(1, 2, 3);
|
||||||
v.y
|
v.y
|
||||||
"#,
|
",
|
||||||
)?,
|
)?,
|
||||||
2,
|
2,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<i64>(
|
engine.eval::<INT>(
|
||||||
r#"
|
"
|
||||||
let v = vec3(1, 2, 3);
|
let v = vec3(1, 2, 3);
|
||||||
v.z
|
v.z
|
||||||
"#,
|
",
|
||||||
)?,
|
)?,
|
||||||
3,
|
3,
|
||||||
);
|
);
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
assert!(engine.eval::<bool>(
|
assert!(engine.eval::<bool>(
|
||||||
r#"
|
"
|
||||||
let v = vec3(1, 2, 3);
|
let v = vec3(1, 2, 3);
|
||||||
v.x == v[0] && v.y == v[1] && v.z == v[2]
|
v.x == v[0] && v.y == v[1] && v.z == v[2]
|
||||||
"#,
|
",
|
||||||
)?);
|
)?);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
engine.eval::<Vec3>(
|
engine.eval::<Vec3>(
|
||||||
r#"
|
"
|
||||||
let v = vec3(1, 2, 3);
|
let v = vec3(1, 2, 3);
|
||||||
v.x = 5;
|
v.x = 5;
|
||||||
v.y = 6;
|
v.y = 6;
|
||||||
v.z = 7;
|
v.z = 7;
|
||||||
v
|
v
|
||||||
"#,
|
",
|
||||||
)?,
|
)?,
|
||||||
Vec3::new(5, 6, 7),
|
Vec3::new(5, 6, 7),
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user