diff --git a/src/api/build_type.rs b/src/api/build_type.rs index 83533e41..02a5b3cd 100644 --- a/src/api/build_type.rs +++ b/src/api/build_type.rs @@ -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); } 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(&mut self) -> &mut Self - where - T: CustomType, - { + pub fn build_type(&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, } -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(&mut self, name: N, method: F) -> &mut Self where N: AsRef + Into, @@ -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(&mut self, name: N, method: F) -> &mut Self where N: AsRef + Into, @@ -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( &mut self, name: impl AsRef, @@ -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( &mut self, name: impl AsRef, @@ -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( &mut self, name: impl AsRef, @@ -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( &mut self, name: impl AsRef, @@ -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( &mut self, name: impl AsRef, @@ -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( &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( &mut self, get_fn: impl Fn(&mut T, X) -> RhaiResultOf + 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( &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( &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( &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 { diff --git a/tests/build_type.rs b/tests/build_type.rs index 76d1bcac..9f8924d4 100644 --- a/tests/build_type.rs +++ b/tests/build_type.rs @@ -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> { #[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> { + fn get_component(&mut self, idx: INT) -> Result> { match idx { 0 => Ok(self.x), 1 => Ok(self.y), @@ -51,8 +52,10 @@ fn build_type() -> Result<(), Box> { .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> { assert_eq!( engine.eval::( - r#" - let v = vec3(1, 2, 3); - v -"#, + " + let v = vec3(1, 2, 3); + v + ", )?, Vec3::new(1, 2, 3), ); assert_eq!( - engine.eval::( - r#" - let v = vec3(1, 2, 3); - v.x -"#, + engine.eval::( + " + let v = vec3(1, 2, 3); + v.x + ", )?, 1, ); assert_eq!( - engine.eval::( - r#" - let v = vec3(1, 2, 3); - v.y -"#, + engine.eval::( + " + let v = vec3(1, 2, 3); + v.y + ", )?, 2, ); assert_eq!( - engine.eval::( - r#" - let v = vec3(1, 2, 3); - v.z -"#, + engine.eval::( + " + let v = vec3(1, 2, 3); + v.z + ", )?, 3, ); + #[cfg(not(feature = "no_index"))] assert!(engine.eval::( - r#" - let v = vec3(1, 2, 3); - v.x == v[0] && v.y == v[1] && v.z == v[2] -"#, + " + let v = vec3(1, 2, 3); + v.x == v[0] && v.y == v[1] && v.z == v[2] + ", )?); assert_eq!( engine.eval::( - r#" - let v = vec3(1, 2, 3); - v.x = 5; - v.y = 6; - v.z = 7; - v -"#, + " + let v = vec3(1, 2, 3); + v.x = 5; + v.y = 6; + v.z = 7; + v + ", )?, Vec3::new(5, 6, 7), );