Use impl Fn style.

This commit is contained in:
Stephen Chung 2020-03-01 19:26:57 +08:00
parent b152ed88f0
commit a64b01692b
2 changed files with 19 additions and 24 deletions

View File

@ -1,6 +1,6 @@
use crate::{any::Any, Array, Dynamic, Engine, RegisterDynamicFn, RegisterFn}; use crate::{any::Any, Array, Dynamic, Engine, RegisterDynamicFn, RegisterFn};
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub}; use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Range, Rem, Shl, Shr, Sub};
macro_rules! reg_op { macro_rules! reg_op {
($self:expr, $x:expr, $op:expr, $( $y:ty ),*) => ( ($self:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
@ -297,7 +297,6 @@ impl Engine {
}); });
// Register range function // Register range function
use std::ops::Range;
self.register_iterator::<Range<i64>, _>(|a| { self.register_iterator::<Range<i64>, _>(|a| {
Box::new( Box::new(
a.downcast_ref::<Range<i64>>() a.downcast_ref::<Range<i64>>()

View File

@ -307,39 +307,38 @@ impl Engine {
/// Register an iterator adapter for a type. /// Register an iterator adapter for a type.
pub fn register_iterator<T: Any, F>(&mut self, f: F) pub fn register_iterator<T: Any, F>(&mut self, f: F)
where where
F: 'static + Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>>, F: Fn(&Dynamic) -> Box<dyn Iterator<Item = Dynamic>> + 'static,
{ {
self.type_iterators.insert(TypeId::of::<T>(), Arc::new(f)); self.type_iterators.insert(TypeId::of::<T>(), Arc::new(f));
} }
/// Register a get function for a member of a registered type /// Register a get function for a member of a registered type
pub fn register_get<T: Any + Clone, U: Any + Clone, F>(&mut self, name: &str, get_fn: F) pub fn register_get<T: Any + Clone, U: Any + Clone>(
where &mut self,
F: 'static + Fn(&mut T) -> U, name: &str,
{ get_fn: impl Fn(&mut T) -> U + 'static,
) {
let get_name = "get$".to_string() + name; let get_name = "get$".to_string() + name;
self.register_fn(&get_name, get_fn); self.register_fn(&get_name, get_fn);
} }
/// Register a set function for a member of a registered type /// Register a set function for a member of a registered type
pub fn register_set<T: Any + Clone, U: Any + Clone, F>(&mut self, name: &str, set_fn: F) pub fn register_set<T: Any + Clone, U: Any + Clone>(
where &mut self,
F: 'static + Fn(&mut T, U) -> (), name: &str,
{ set_fn: impl Fn(&mut T, U) -> () + 'static,
) {
let set_name = "set$".to_string() + name; let set_name = "set$".to_string() + name;
self.register_fn(&set_name, set_fn); self.register_fn(&set_name, set_fn);
} }
/// Shorthand for registering both getters and setters /// Shorthand for registering both getters and setters
pub fn register_get_set<T: Any + Clone, U: Any + Clone, F, G>( pub fn register_get_set<T: Any + Clone, U: Any + Clone>(
&mut self, &mut self,
name: &str, name: &str,
get_fn: F, get_fn: impl Fn(&mut T) -> U + 'static,
set_fn: G, set_fn: impl Fn(&mut T, U) -> () + 'static,
) where ) {
F: 'static + Fn(&mut T) -> U,
G: 'static + Fn(&mut T, U) -> (),
{
self.register_get(name, get_fn); self.register_get(name, get_fn);
self.register_set(name, set_fn); self.register_set(name, set_fn);
} }
@ -432,14 +431,11 @@ impl Engine {
} }
} }
fn search_scope<'a, F, T>( fn search_scope<'a, T>(
scope: &'a mut Scope, scope: &'a mut Scope,
id: &str, id: &str,
map: F, map: impl FnOnce(&'a mut Variant) -> Result<T, EvalAltResult>,
) -> Result<(usize, T), EvalAltResult> ) -> Result<(usize, T), EvalAltResult> {
where
F: FnOnce(&'a mut Variant) -> Result<T, EvalAltResult>,
{
scope scope
.iter_mut() .iter_mut()
.enumerate() .enumerate()