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::{
|
||||
func::SendSync, types::dynamic::Variant, Engine, Identifier, RegisterNativeFunction,
|
||||
RhaiResultOf,
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
/// Trait to build a custom type for use with the [`Engine`].
|
||||
/// i.e. register the type and its getters, setters, methods, etc...
|
||||
/// Trait to build a custom type for use with an [`Engine`]
|
||||
/// (i.e. register the type and its getters, setters, methods, etc.).
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@ -58,49 +62,44 @@ use crate::{
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[deprecated = "This trait is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
pub trait CustomType: Variant + Clone {
|
||||
/// 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>);
|
||||
}
|
||||
|
||||
impl 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]
|
||||
pub fn build_type<T>(&mut self) -> &mut Self
|
||||
where
|
||||
T: CustomType,
|
||||
{
|
||||
pub fn build_type<T: CustomType>(&mut self) -> &mut Self {
|
||||
T::build(TypeBuilder::new(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.
|
||||
///
|
||||
/// ## 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.
|
||||
pub struct TypeBuilder<'a, T>
|
||||
where
|
||||
T: Variant + Clone,
|
||||
{
|
||||
/// By default the type is registered with [`Engine::register_type`] (i.e. without a pretty name).
|
||||
///
|
||||
/// To define a pretty name, call [`with_name`][`TypeBuilder::with_name`],
|
||||
/// 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,
|
||||
name: Option<&'static str>,
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, T> TypeBuilder<'a, T>
|
||||
where
|
||||
T: Variant + Clone,
|
||||
{
|
||||
#[inline]
|
||||
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
#[inline(always)]
|
||||
fn new(engine: &'a mut Engine) -> Self {
|
||||
Self {
|
||||
engine,
|
||||
@ -110,21 +109,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> TypeBuilder<'a, T>
|
||||
where
|
||||
T: Variant + Clone,
|
||||
{
|
||||
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
/// 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]
|
||||
#[inline(always)]
|
||||
pub fn with_name(&mut self, name: &'static str) -> &mut Self {
|
||||
self.name = Some(name);
|
||||
self
|
||||
}
|
||||
|
||||
/// Register a custom function.
|
||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn with_fn<N, A, F>(&mut self, name: N, method: F) -> &mut Self
|
||||
where
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
@ -135,8 +129,7 @@ where
|
||||
}
|
||||
|
||||
/// Register a custom fallible function.
|
||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn with_result_fn<N, A, F, R>(&mut self, name: N, method: F) -> &mut Self
|
||||
where
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
@ -148,17 +141,13 @@ where
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
impl<'a, T> TypeBuilder<'a, T>
|
||||
where
|
||||
T: Variant + Clone,
|
||||
{
|
||||
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
/// Register a getter function.
|
||||
///
|
||||
/// The function signature must start with `&mut self` and not `&self`.
|
||||
///
|
||||
/// Not available under `no_object`.
|
||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn with_get<V: Variant + Clone>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
@ -173,8 +162,7 @@ where
|
||||
/// The function signature must start with `&mut self` and not `&self`.
|
||||
///
|
||||
/// Not available under `no_object`.
|
||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn with_get_result<V: Variant + Clone>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
@ -187,8 +175,7 @@ where
|
||||
/// Register a setter function.
|
||||
///
|
||||
/// Not available under `no_object`.
|
||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn with_set<V: Variant + Clone>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
@ -201,8 +188,7 @@ where
|
||||
/// Register a fallible setter function.
|
||||
///
|
||||
/// Not available under `no_object`.
|
||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn with_set_result<V: Variant + Clone>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
@ -217,8 +203,7 @@ where
|
||||
/// All function signatures must start with `&mut self` and not `&self`.
|
||||
///
|
||||
/// Not available under `no_object`.
|
||||
#[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."]
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
pub fn with_get_set<V: Variant + Clone>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
@ -231,17 +216,13 @@ where
|
||||
}
|
||||
|
||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||
impl<'a, T> TypeBuilder<'a, T>
|
||||
where
|
||||
T: Variant + Clone,
|
||||
{
|
||||
impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
/// Register an index getter.
|
||||
///
|
||||
/// The function signature must start with `&mut self` and not `&self`.
|
||||
///
|
||||
/// 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]
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_get<X: Variant + Clone, V: Variant + Clone>(
|
||||
&mut self,
|
||||
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`.
|
||||
///
|
||||
/// 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]
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_get_result<X: Variant + Clone, V: Variant + Clone>(
|
||||
&mut self,
|
||||
get_fn: impl Fn(&mut T, X) -> RhaiResultOf<V> + SendSync + 'static,
|
||||
@ -268,8 +248,7 @@ where
|
||||
/// Register an index setter.
|
||||
///
|
||||
/// 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]
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_set<X: Variant + Clone, V: Variant + Clone>(
|
||||
&mut self,
|
||||
set_fn: impl Fn(&mut T, X, V) + SendSync + 'static,
|
||||
@ -281,8 +260,7 @@ where
|
||||
/// Register an fallible index setter.
|
||||
///
|
||||
/// 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]
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_set_result<X: Variant + Clone, V: Variant + Clone>(
|
||||
&mut self,
|
||||
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.
|
||||
///
|
||||
/// 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]
|
||||
#[inline(always)]
|
||||
pub fn with_indexer_get_set<X: Variant + Clone, V: Variant + Clone>(
|
||||
&mut self,
|
||||
get_fn: impl Fn(&mut T, X) -> V + SendSync + 'static,
|
||||
@ -306,10 +283,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Drop for TypeBuilder<'a, T>
|
||||
where
|
||||
T: Variant + Clone,
|
||||
{
|
||||
impl<'a, T: Variant + Clone> Drop for TypeBuilder<'a, T> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
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]
|
||||
fn build_type() -> Result<(), Box<EvalAltResult>> {
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
struct Vec3 {
|
||||
x: i64,
|
||||
y: i64,
|
||||
z: i64,
|
||||
x: INT,
|
||||
y: INT,
|
||||
z: INT,
|
||||
}
|
||||
|
||||
impl Vec3 {
|
||||
fn new(x: i64, y: i64, z: i64) -> Self {
|
||||
fn new(x: INT, y: INT, z: INT) -> Self {
|
||||
Self { x, y, z }
|
||||
}
|
||||
fn get_x(&mut self) -> i64 {
|
||||
fn get_x(&mut self) -> INT {
|
||||
self.x
|
||||
}
|
||||
fn set_x(&mut self, x: i64) {
|
||||
fn set_x(&mut self, x: INT) {
|
||||
self.x = x
|
||||
}
|
||||
fn get_y(&mut self) -> i64 {
|
||||
fn get_y(&mut self) -> INT {
|
||||
self.y
|
||||
}
|
||||
fn set_y(&mut self, y: i64) {
|
||||
fn set_y(&mut self, y: INT) {
|
||||
self.y = y
|
||||
}
|
||||
fn get_z(&mut self) -> i64 {
|
||||
fn get_z(&mut self) -> INT {
|
||||
self.z
|
||||
}
|
||||
fn set_z(&mut self, z: i64) {
|
||||
fn set_z(&mut self, z: INT) {
|
||||
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 {
|
||||
0 => Ok(self.x),
|
||||
1 => Ok(self.y),
|
||||
@ -51,8 +52,10 @@ fn build_type() -> Result<(), Box<EvalAltResult>> {
|
||||
.with_fn("vec3", Self::new)
|
||||
.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)
|
||||
.with_indexer_get_result(Self::get_component);
|
||||
.with_get_set("z", Self::get_z, Self::set_z);
|
||||
|
||||
#[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!(
|
||||
engine.eval::<Vec3>(
|
||||
r#"
|
||||
"
|
||||
let v = vec3(1, 2, 3);
|
||||
v
|
||||
"#,
|
||||
",
|
||||
)?,
|
||||
Vec3::new(1, 2, 3),
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<i64>(
|
||||
r#"
|
||||
engine.eval::<INT>(
|
||||
"
|
||||
let v = vec3(1, 2, 3);
|
||||
v.x
|
||||
"#,
|
||||
",
|
||||
)?,
|
||||
1,
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<i64>(
|
||||
r#"
|
||||
engine.eval::<INT>(
|
||||
"
|
||||
let v = vec3(1, 2, 3);
|
||||
v.y
|
||||
"#,
|
||||
",
|
||||
)?,
|
||||
2,
|
||||
);
|
||||
assert_eq!(
|
||||
engine.eval::<i64>(
|
||||
r#"
|
||||
engine.eval::<INT>(
|
||||
"
|
||||
let v = vec3(1, 2, 3);
|
||||
v.z
|
||||
"#,
|
||||
",
|
||||
)?,
|
||||
3,
|
||||
);
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
assert!(engine.eval::<bool>(
|
||||
r#"
|
||||
"
|
||||
let v = vec3(1, 2, 3);
|
||||
v.x == v[0] && v.y == v[1] && v.z == v[2]
|
||||
"#,
|
||||
",
|
||||
)?);
|
||||
assert_eq!(
|
||||
engine.eval::<Vec3>(
|
||||
r#"
|
||||
"
|
||||
let v = vec3(1, 2, 3);
|
||||
v.x = 5;
|
||||
v.y = 6;
|
||||
v.z = 7;
|
||||
v
|
||||
"#,
|
||||
",
|
||||
)?,
|
||||
Vec3::new(5, 6, 7),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user