Rename parameters.

This commit is contained in:
Stephen Chung 2021-12-31 17:49:19 +08:00
parent 05baee042b
commit ce93f56813
2 changed files with 23 additions and 22 deletions

View File

@ -9,7 +9,7 @@ use std::prelude::v1::*;
/// Trait that parses arguments to a function call. /// Trait that parses arguments to a function call.
/// ///
/// Any data type can implement this trait in order to pass arguments to a function call. /// Any data type can implement this trait in order to pass arguments to [`Engine::call_fn`][crate::Engine::call_fn].
pub trait FuncArgs { pub trait FuncArgs {
/// Parse function call arguments into a container. /// Parse function call arguments into a container.
/// ///
@ -26,10 +26,10 @@ pub trait FuncArgs {
/// } /// }
/// ///
/// impl FuncArgs for Options { /// impl FuncArgs for Options {
/// fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) { /// fn parse<ARGS: Extend<Dynamic>>(self, args: &mut ARGS) {
/// container.extend(std::iter::once(self.foo.into())); /// args.extend(std::iter::once(self.foo.into()));
/// container.extend(std::iter::once(self.bar.into())); /// args.extend(std::iter::once(self.bar.into()));
/// container.extend(std::iter::once(self.baz.into())); /// args.extend(std::iter::once(self.baz.into()));
/// } /// }
/// } /// }
/// ///
@ -55,13 +55,13 @@ pub trait FuncArgs {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER); fn parse<ARGS: Extend<Dynamic>>(self, args: &mut ARGS);
} }
impl<T: Variant + Clone> FuncArgs for Vec<T> { impl<T: Variant + Clone> FuncArgs for Vec<T> {
#[inline] #[inline]
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) { fn parse<ARGS: Extend<Dynamic>>(self, args: &mut ARGS) {
container.extend(self.into_iter().map(Variant::into_dynamic)); args.extend(self.into_iter().map(Variant::into_dynamic));
} }
} }
@ -73,9 +73,9 @@ macro_rules! impl_args {
{ {
#[inline] #[inline]
#[allow(unused_variables)] #[allow(unused_variables)]
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) { fn parse<ARGS: Extend<Dynamic>>(self, args: &mut ARGS) {
let ($($p,)*) = self; let ($($p,)*) = self;
$(container.extend(Some($p.into_dynamic()));)* $(args.extend(Some($p.into_dynamic()));)*
} }
} }

View File

@ -1,10 +1,12 @@
use crate::plugin::*; use crate::plugin::*;
use crate::types::dynamic::Variant; use crate::types::dynamic::Variant;
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT}; use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT};
use std::iter::{ExactSizeIterator, FusedIterator};
use std::ops::{Range, RangeInclusive};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{
iter::{ExactSizeIterator, FusedIterator},
ops::{Range, RangeInclusive},
};
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
use num_traits::{CheckedAdd as Add, CheckedSub as Sub}; use num_traits::{CheckedAdd as Add, CheckedSub as Sub};
@ -159,12 +161,10 @@ impl Iterator for BitRange {
type Item = bool; type Item = bool;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let Self(value, mask, len) = *self; if self.2 == 0 {
if len == 0 {
None None
} else { } else {
let r = (value & mask) != 0; let r = (self.0 & self.1) != 0;
self.1 <<= 1; self.1 <<= 1;
self.2 -= 1; self.2 -= 1;
Some(r) Some(r)
@ -263,7 +263,6 @@ macro_rules! reg_range {
($lib:ident | $x:expr => $( $y:ty ),*) => { ($lib:ident | $x:expr => $( $y:ty ),*) => {
$( $(
$lib.set_iterator::<Range<$y>>(); $lib.set_iterator::<Range<$y>>();
$lib.set_iterator::<RangeInclusive<$y>>();
let _hash = $lib.set_native_fn($x, |from: $y, to: $y| Ok(from..to)); let _hash = $lib.set_native_fn($x, |from: $y, to: $y| Ok(from..to));
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
@ -272,6 +271,8 @@ macro_rules! reg_range {
concat!("to: ", stringify!($y)), concat!("to: ", stringify!($y)),
concat!("Iterator<Item=", stringify!($y), ">") concat!("Iterator<Item=", stringify!($y), ">")
]); ]);
$lib.set_iterator::<RangeInclusive<$y>>();
)* )*
}; };
($lib:ident | step $x:expr => $( $y:ty ),*) => { ($lib:ident | step $x:expr => $( $y:ty ),*) => {
@ -450,7 +451,7 @@ def_package! {
Ok(CharsStream::new(string, from, to - from)) Ok(CharsStream::new(string, from, to - from))
}); });
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "range: Range", "Iterator<Item=char>"]); lib.update_fn_metadata(_hash, &["string: &str", "range: Range<INT>", "Iterator<Item=char>"]);
let _hash = lib.set_native_fn("chars", |string, range: InclusiveRange| { let _hash = lib.set_native_fn("chars", |string, range: InclusiveRange| {
let from = INT::max(*range.start(), 0); let from = INT::max(*range.start(), 0);
@ -458,7 +459,7 @@ def_package! {
Ok(CharsStream::new(string, from, to-from + 1)) Ok(CharsStream::new(string, from, to-from + 1))
}); });
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["string: &str", "range: RangeInclusive", "Iterator<Item=char>"]); lib.update_fn_metadata(_hash, &["string: &str", "range: RangeInclusive<INT>", "Iterator<Item=char>"]);
let _hash = lib.set_native_fn("chars", |string, from, len| Ok(CharsStream::new(string, from, len))); let _hash = lib.set_native_fn("chars", |string, from, len| Ok(CharsStream::new(string, from, len)));
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
@ -488,7 +489,7 @@ def_package! {
BitRange::new(value, from, to - from) BitRange::new(value, from, to - from)
}); });
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "range: Range", "Iterator<Item=bool>"]); lib.update_fn_metadata(_hash, &["value: INT", "range: Range<INT>", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", |value, range: InclusiveRange| { let _hash = lib.set_native_fn("bits", |value, range: InclusiveRange| {
let from = INT::max(*range.start(), 0); let from = INT::max(*range.start(), 0);
@ -496,7 +497,7 @@ def_package! {
BitRange::new(value, from, to - from + 1) BitRange::new(value, from, to - from + 1)
}); });
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: INT", "range: RangeInclusive", "Iterator<Item=bool>"]); lib.update_fn_metadata(_hash, &["value: INT", "range: RangeInclusive<INT>", "Iterator<Item=bool>"]);
let _hash = lib.set_native_fn("bits", BitRange::new); let _hash = lib.set_native_fn("bits", BitRange::new);
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
@ -514,7 +515,7 @@ def_package! {
{ {
let _hash = lib.set_getter_fn("bits", |value: &mut INT| BitRange::new(*value, 0, INT::MAX) ); let _hash = lib.set_getter_fn("bits", |value: &mut INT| BitRange::new(*value, 0, INT::MAX) );
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
lib.update_fn_metadata(_hash, &["value: &mut INT", "range: Range", "Iterator<Item=bool>"]); lib.update_fn_metadata(_hash, &["value: &mut INT", "range: Range<INT>", "Iterator<Item=bool>"]);
} }
combine_with_exported_module!(lib, "range", range_functions); combine_with_exported_module!(lib, "range", range_functions);