New Procedural Macros Crate v0.1

This commit is contained in:
J Henry Waugh
2020-08-01 11:52:26 -05:00
parent 99d56b8f81
commit d01203cb5d
28 changed files with 2230 additions and 5 deletions

View File

@@ -681,6 +681,13 @@ impl Dynamic {
}
}
/// Copy and return a `Dynamic` if it contains a type that can be trivially copied.
/// Returns `None` if the cast fails.
#[inline(always)]
pub fn downcast_clone<T: Variant + Clone>(&self) -> Option<T> {
self.downcast_ref::<T>().map(|t| t.clone())
}
/// Cast the `Dynamic` as the system integer type `INT` and return it.
/// Returns the name of the actual type if the cast fails.
pub fn as_int(&self) -> Result<INT, &'static str> {

View File

@@ -43,7 +43,7 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
/// fn is_method_call(&self) -> bool { false }
/// fn is_varadic(&self) -> bool { false }
///
/// fn call(&self, args: &[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>> {
/// fn call(&self, args: &mut[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>> {
/// let x1: &FLOAT = args[0].downcast_ref::<FLOAT>().unwrap();
/// let y1: &FLOAT = args[1].downcast_ref::<FLOAT>().unwrap();
/// let x2: &FLOAT = args[2].downcast_ref::<FLOAT>().unwrap();
@@ -55,6 +55,13 @@ pub trait RegisterPlugin<PL: crate::plugin::Plugin> {
/// fn clone_boxed(&self) -> Box<dyn PluginFunction> {
/// Box::new(DistanceFunction())
/// }
///
/// fn input_types(&self) -> Box<[std::any::TypeId]> {
/// vec![std::any::TypeId::of::<FLOAT>(),
/// std::any::TypeId::of::<FLOAT>(),
/// std::any::TypeId::of::<FLOAT>(),
/// std::any::TypeId::of::<FLOAT>()].into_boxed_slice()
/// }
/// }
///
/// // A simple custom plugin. This should not usually be done with hand-written code.

View File

@@ -98,6 +98,8 @@ pub use syntax::{EvalContext, Expression};
pub use token::Position;
pub use utils::calc_fn_spec as calc_fn_hash;
pub use rhai_codegen::*;
#[cfg(not(feature = "no_function"))]
pub use parser::FnAccess;

View File

@@ -1,6 +1,6 @@
//! Module defining plugins in Rhai. Is exported for use by plugin authors.
use crate::stdlib::boxed::Box;
use crate::stdlib::{any::TypeId, boxed::Box};
pub use crate::any::{Dynamic, Variant};
pub use crate::fn_native::{CallableFunction, FnCallArgs, IteratorFn};
@@ -38,7 +38,9 @@ pub trait PluginFunction {
fn is_method_call(&self) -> bool;
fn is_varadic(&self) -> bool;
fn call(&self, args: &[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>>;
fn call(&self, args: &mut[&mut Dynamic], pos: Position) -> Result<Dynamic, Box<EvalAltResult>>;
fn clone_boxed(&self) -> Box<dyn PluginFunction>;
fn input_types(&self) -> Box<[TypeId]>;
}