Remove Box on callback traits.
This commit is contained in:
parent
64de20bcd3
commit
e476929597
@ -12,8 +12,7 @@ impl Engine {
|
|||||||
///
|
///
|
||||||
/// The callback function signature takes the following form:
|
/// The callback function signature takes the following form:
|
||||||
///
|
///
|
||||||
/// > `Fn(name: &str, index: usize, context: &EvalContext)`
|
/// > `Fn(name: &str, index: usize, context: &EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>>`
|
||||||
/// > ` -> Result<Option<Dynamic>, Box<EvalAltResult>> + 'static`
|
|
||||||
///
|
///
|
||||||
/// where:
|
/// where:
|
||||||
/// * `index`: an offset from the bottom of the current [`Scope`][crate::Scope] that the
|
/// * `index`: an offset from the bottom of the current [`Scope`][crate::Scope] that the
|
||||||
|
@ -115,17 +115,17 @@ pub struct Engine {
|
|||||||
/// Custom syntax.
|
/// Custom syntax.
|
||||||
pub(crate) custom_syntax: BTreeMap<Identifier, Box<CustomSyntax>>,
|
pub(crate) custom_syntax: BTreeMap<Identifier, Box<CustomSyntax>>,
|
||||||
/// Callback closure for resolving variable access.
|
/// Callback closure for resolving variable access.
|
||||||
pub(crate) resolve_var: Option<OnVarCallback>,
|
pub(crate) resolve_var: Option<Box<OnVarCallback>>,
|
||||||
/// Callback closure to remap tokens during parsing.
|
/// Callback closure to remap tokens during parsing.
|
||||||
pub(crate) token_mapper: Option<Box<OnParseTokenCallback>>,
|
pub(crate) token_mapper: Option<Box<OnParseTokenCallback>>,
|
||||||
|
|
||||||
/// Callback closure for implementing the `print` command.
|
/// Callback closure for implementing the `print` command.
|
||||||
pub(crate) print: Option<OnPrintCallback>,
|
pub(crate) print: Option<Box<OnPrintCallback>>,
|
||||||
/// Callback closure for implementing the `debug` command.
|
/// Callback closure for implementing the `debug` command.
|
||||||
pub(crate) debug: Option<OnDebugCallback>,
|
pub(crate) debug: Option<Box<OnDebugCallback>>,
|
||||||
/// Callback closure for progress reporting.
|
/// Callback closure for progress reporting.
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
pub(crate) progress: Option<crate::func::native::OnProgressCallback>,
|
pub(crate) progress: Option<Box<crate::func::native::OnProgressCallback>>,
|
||||||
|
|
||||||
/// Optimize the [`AST`][crate::AST] after compilation.
|
/// Optimize the [`AST`][crate::AST] after compilation.
|
||||||
#[cfg(not(feature = "no_optimize"))]
|
#[cfg(not(feature = "no_optimize"))]
|
||||||
@ -140,7 +140,7 @@ pub struct Engine {
|
|||||||
|
|
||||||
/// Callback closure for debugging.
|
/// Callback closure for debugging.
|
||||||
#[cfg(feature = "debugging")]
|
#[cfg(feature = "debugging")]
|
||||||
pub(crate) debugger: Option<crate::eval::OnDebuggerCallback>,
|
pub(crate) debugger: Option<Box<crate::eval::OnDebuggerCallback>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Engine {
|
impl fmt::Debug for Engine {
|
||||||
|
@ -10,18 +10,13 @@ use std::prelude::v1::*;
|
|||||||
|
|
||||||
/// A standard callback function for debugging.
|
/// A standard callback function for debugging.
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
pub type OnDebuggerCallback = Box<
|
pub type OnDebuggerCallback =
|
||||||
dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf<DebuggerCommand>
|
dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf<DebuggerCommand>;
|
||||||
+ 'static,
|
|
||||||
>;
|
|
||||||
/// A standard callback function for debugging.
|
/// A standard callback function for debugging.
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
pub type OnDebuggerCallback = Box<
|
pub type OnDebuggerCallback = dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf<DebuggerCommand>
|
||||||
dyn Fn(&mut EvalContext, ASTNode, Option<&str>, Position) -> RhaiResultOf<DebuggerCommand>
|
|
||||||
+ Send
|
+ Send
|
||||||
+ Sync
|
+ Sync;
|
||||||
+ 'static,
|
|
||||||
>;
|
|
||||||
|
|
||||||
/// A command for the debugger on the next iteration.
|
/// A command for the debugger on the next iteration.
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||||
|
@ -178,7 +178,7 @@ impl GlobalRuntimeState<'_> {
|
|||||||
/// [modules][Module].
|
/// [modules][Module].
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_iter(&self, id: TypeId) -> Option<IteratorFn> {
|
pub fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> {
|
||||||
self.modules
|
self.modules
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
|
@ -17,7 +17,7 @@ pub enum CallableFunction {
|
|||||||
/// and the rest passed by value.
|
/// and the rest passed by value.
|
||||||
Method(Shared<FnAny>),
|
Method(Shared<FnAny>),
|
||||||
/// An iterator function.
|
/// An iterator function.
|
||||||
Iterator(IteratorFn),
|
Iterator(Shared<IteratorFn>),
|
||||||
/// A plugin function,
|
/// A plugin function,
|
||||||
Plugin(Shared<FnPlugin>),
|
Plugin(Shared<FnPlugin>),
|
||||||
/// A script-defined function.
|
/// A script-defined function.
|
||||||
@ -177,9 +177,9 @@ impl CallableFunction {
|
|||||||
/// Get a reference to an iterator function.
|
/// Get a reference to an iterator function.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_iter_fn(&self) -> Option<IteratorFn> {
|
pub fn get_iter_fn(&self) -> Option<&IteratorFn> {
|
||||||
match self {
|
match self {
|
||||||
Self::Iterator(f) => Some(*f),
|
Self::Iterator(f) => Some(f.as_ref()),
|
||||||
Self::Pure(_) | Self::Method(_) | Self::Plugin(_) => None,
|
Self::Pure(_) | Self::Method(_) | Self::Plugin(_) => None,
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
@ -218,13 +218,6 @@ impl CallableFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<IteratorFn> for CallableFunction {
|
|
||||||
#[inline(always)]
|
|
||||||
fn from(func: IteratorFn) -> Self {
|
|
||||||
Self::Iterator(func)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
impl From<crate::ast::ScriptFnDef> for CallableFunction {
|
impl From<crate::ast::ScriptFnDef> for CallableFunction {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -376,7 +376,11 @@ pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult + Send
|
|||||||
pub type FnBuiltin = fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult;
|
pub type FnBuiltin = fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult;
|
||||||
|
|
||||||
/// A standard function that gets an iterator from a type.
|
/// A standard function that gets an iterator from a type.
|
||||||
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
|
#[cfg(not(feature = "sync"))]
|
||||||
|
pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
|
||||||
|
/// A standard function that gets an iterator from a type.
|
||||||
|
#[cfg(feature = "sync")]
|
||||||
|
pub type IteratorFn = dyn Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + Send + Sync;
|
||||||
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
pub type FnPlugin = dyn PluginFunction;
|
pub type FnPlugin = dyn PluginFunction;
|
||||||
@ -386,39 +390,37 @@ pub type FnPlugin = dyn PluginFunction + Send + Sync;
|
|||||||
/// A standard callback function for progress reporting.
|
/// A standard callback function for progress reporting.
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
pub type OnProgressCallback = Box<dyn Fn(u64) -> Option<Dynamic> + 'static>;
|
pub type OnProgressCallback = dyn Fn(u64) -> Option<Dynamic>;
|
||||||
/// A standard callback function for progress reporting.
|
/// A standard callback function for progress reporting.
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
pub type OnProgressCallback = Box<dyn Fn(u64) -> Option<Dynamic> + Send + Sync + 'static>;
|
pub type OnProgressCallback = dyn Fn(u64) -> Option<Dynamic> + Send + Sync;
|
||||||
|
|
||||||
/// A standard callback function for printing.
|
/// A standard callback function for printing.
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
pub type OnPrintCallback = Box<dyn Fn(&str) + 'static>;
|
pub type OnPrintCallback = dyn Fn(&str);
|
||||||
/// A standard callback function for printing.
|
/// A standard callback function for printing.
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
pub type OnPrintCallback = Box<dyn Fn(&str) + Send + Sync + 'static>;
|
pub type OnPrintCallback = dyn Fn(&str) + Send + Sync;
|
||||||
|
|
||||||
/// A standard callback function for debugging.
|
/// A standard callback function for debugging.
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
pub type OnDebugCallback = Box<dyn Fn(&str, Option<&str>, Position) + 'static>;
|
pub type OnDebugCallback = dyn Fn(&str, Option<&str>, Position);
|
||||||
/// A standard callback function for debugging.
|
/// A standard callback function for debugging.
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
pub type OnDebugCallback = Box<dyn Fn(&str, Option<&str>, Position) + Send + Sync + 'static>;
|
pub type OnDebugCallback = dyn Fn(&str, Option<&str>, Position) + Send + Sync;
|
||||||
|
|
||||||
/// A standard callback function for mapping tokens during parsing.
|
/// A standard callback function for mapping tokens during parsing.
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
pub type OnParseTokenCallback = dyn Fn(Token, Position, &TokenizeState) -> Token;
|
pub type OnParseTokenCallback = dyn Fn(Token, Position, &TokenizeState) -> Token;
|
||||||
/// A standard callback function for mapping tokens during parsing.
|
/// A standard callback function for mapping tokens during parsing.
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
pub type OnParseTokenCallback =
|
pub type OnParseTokenCallback = dyn Fn(Token, Position, &TokenizeState) -> Token + Send + Sync;
|
||||||
dyn Fn(Token, Position, &TokenizeState) -> Token + Send + Sync + 'static;
|
|
||||||
|
|
||||||
/// A standard callback function for variable access.
|
/// A standard callback function for variable access.
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
pub type OnVarCallback =
|
pub type OnVarCallback = dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf<Option<Dynamic>>;
|
||||||
Box<dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf<Option<Dynamic>> + 'static>;
|
|
||||||
/// A standard callback function for variable access.
|
/// A standard callback function for variable access.
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
pub type OnVarCallback =
|
pub type OnVarCallback =
|
||||||
Box<dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf<Option<Dynamic>> + Send + Sync + 'static>;
|
dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf<Option<Dynamic>> + Send + Sync;
|
||||||
|
@ -244,9 +244,9 @@ pub struct Module {
|
|||||||
/// including those in sub-modules.
|
/// including those in sub-modules.
|
||||||
all_functions: BTreeMap<u64, Shared<CallableFunction>>,
|
all_functions: BTreeMap<u64, Shared<CallableFunction>>,
|
||||||
/// Iterator functions, keyed by the type producing the iterator.
|
/// Iterator functions, keyed by the type producing the iterator.
|
||||||
type_iterators: BTreeMap<TypeId, IteratorFn>,
|
type_iterators: BTreeMap<TypeId, Shared<IteratorFn>>,
|
||||||
/// Flattened collection of iterator functions, including those in sub-modules.
|
/// Flattened collection of iterator functions, including those in sub-modules.
|
||||||
all_type_iterators: BTreeMap<TypeId, IteratorFn>,
|
all_type_iterators: BTreeMap<TypeId, Shared<IteratorFn>>,
|
||||||
/// Is the [`Module`] indexed?
|
/// Is the [`Module`] indexed?
|
||||||
indexed: bool,
|
indexed: bool,
|
||||||
/// Does the [`Module`] contain indexed functions that have been exposed to the global namespace?
|
/// Does the [`Module`] contain indexed functions that have been exposed to the global namespace?
|
||||||
@ -1432,8 +1432,8 @@ impl Module {
|
|||||||
other.functions.iter().for_each(|(&k, v)| {
|
other.functions.iter().for_each(|(&k, v)| {
|
||||||
self.functions.entry(k).or_insert_with(|| v.clone());
|
self.functions.entry(k).or_insert_with(|| v.clone());
|
||||||
});
|
});
|
||||||
other.type_iterators.iter().for_each(|(&k, &v)| {
|
other.type_iterators.iter().for_each(|(&k, v)| {
|
||||||
self.type_iterators.entry(k).or_insert(v);
|
self.type_iterators.entry(k).or_insert(v.clone());
|
||||||
});
|
});
|
||||||
self.all_functions.clear();
|
self.all_functions.clear();
|
||||||
self.all_variables.clear();
|
self.all_variables.clear();
|
||||||
@ -1483,7 +1483,8 @@ impl Module {
|
|||||||
.map(|(&k, v)| (k, v.clone())),
|
.map(|(&k, v)| (k, v.clone())),
|
||||||
);
|
);
|
||||||
|
|
||||||
self.type_iterators.extend(other.type_iterators.iter());
|
self.type_iterators
|
||||||
|
.extend(other.type_iterators.iter().map(|(&k, v)| (k, v.clone())));
|
||||||
self.all_functions.clear();
|
self.all_functions.clear();
|
||||||
self.all_variables.clear();
|
self.all_variables.clear();
|
||||||
self.all_type_iterators.clear();
|
self.all_type_iterators.clear();
|
||||||
@ -1768,7 +1769,7 @@ impl Module {
|
|||||||
path: &mut Vec<&'a str>,
|
path: &mut Vec<&'a str>,
|
||||||
variables: &mut BTreeMap<u64, Dynamic>,
|
variables: &mut BTreeMap<u64, Dynamic>,
|
||||||
functions: &mut BTreeMap<u64, Shared<CallableFunction>>,
|
functions: &mut BTreeMap<u64, Shared<CallableFunction>>,
|
||||||
type_iterators: &mut BTreeMap<TypeId, IteratorFn>,
|
type_iterators: &mut BTreeMap<TypeId, Shared<IteratorFn>>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let mut contains_indexed_global_functions = false;
|
let mut contains_indexed_global_functions = false;
|
||||||
|
|
||||||
@ -1789,7 +1790,7 @@ impl Module {
|
|||||||
|
|
||||||
// Index type iterators
|
// Index type iterators
|
||||||
module.type_iterators.iter().for_each(|(&type_id, func)| {
|
module.type_iterators.iter().for_each(|(&type_id, func)| {
|
||||||
type_iterators.insert(type_id, *func);
|
type_iterators.insert(type_id, func.clone());
|
||||||
contains_indexed_global_functions = true;
|
contains_indexed_global_functions = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1868,10 +1869,33 @@ impl Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set a type iterator into the [`Module`].
|
/// Set a type iterator into the [`Module`].
|
||||||
|
#[cfg(not(feature = "sync"))]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_iter(&mut self, type_id: TypeId, func: IteratorFn) -> &mut Self {
|
pub fn set_iter(
|
||||||
|
&mut self,
|
||||||
|
type_id: TypeId,
|
||||||
|
func: impl Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static,
|
||||||
|
) -> &mut Self {
|
||||||
|
let func = Shared::new(func);
|
||||||
if self.indexed {
|
if self.indexed {
|
||||||
self.all_type_iterators.insert(type_id, func);
|
self.all_type_iterators.insert(type_id, func.clone());
|
||||||
|
self.contains_indexed_global_functions = true;
|
||||||
|
}
|
||||||
|
self.type_iterators.insert(type_id, func);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set a type iterator into the [`Module`].
|
||||||
|
#[cfg(feature = "sync")]
|
||||||
|
#[inline]
|
||||||
|
pub fn set_iter(
|
||||||
|
&mut self,
|
||||||
|
type_id: TypeId,
|
||||||
|
func: impl Fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + SendSync + 'static,
|
||||||
|
) -> &mut Self {
|
||||||
|
let func = Shared::new(func);
|
||||||
|
if self.indexed {
|
||||||
|
self.all_type_iterators.insert(type_id, func.clone());
|
||||||
self.contains_indexed_global_functions = true;
|
self.contains_indexed_global_functions = true;
|
||||||
}
|
}
|
||||||
self.type_iterators.insert(type_id, func);
|
self.type_iterators.insert(type_id, func);
|
||||||
@ -1905,15 +1929,15 @@ impl Module {
|
|||||||
/// Get the specified type iterator.
|
/// Get the specified type iterator.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub(crate) fn get_qualified_iter(&self, id: TypeId) -> Option<IteratorFn> {
|
pub(crate) fn get_qualified_iter(&self, id: TypeId) -> Option<&IteratorFn> {
|
||||||
self.all_type_iterators.get(&id).cloned()
|
self.all_type_iterators.get(&id).map(|f| f.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the specified type iterator.
|
/// Get the specified type iterator.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub(crate) fn get_iter(&self, id: TypeId) -> Option<IteratorFn> {
|
pub(crate) fn get_iter(&self, id: TypeId) -> Option<&IteratorFn> {
|
||||||
self.type_iterators.get(&id).cloned()
|
self.type_iterators.get(&id).map(|f| f.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user