Improve docs.

This commit is contained in:
Stephen Chung 2022-08-24 18:27:58 +08:00
parent 010a96dde3
commit b40ca9e40d
6 changed files with 70 additions and 76 deletions

View File

@ -128,11 +128,11 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// Register a custom function. /// Register a custom function.
#[inline(always)] #[inline(always)]
pub fn with_fn<N, A, F, R, S>(&mut self, name: N, method: F) -> &mut Self pub fn with_fn<A, R, S>(
where &mut self,
N: AsRef<str> + Into<Identifier>, name: impl AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, R, S>, method: impl RegisterNativeFunction<A, R, S>,
{ ) -> &mut Self {
self.engine.register_fn(name, method); self.engine.register_fn(name, method);
self self
} }

View File

@ -143,11 +143,11 @@ impl Engine {
/// This method will be removed in the next major version. /// This method will be removed in the next major version.
#[deprecated(since = "1.9.1", note = "use `register_fn` instead")] #[deprecated(since = "1.9.1", note = "use `register_fn` instead")]
#[inline(always)] #[inline(always)]
pub fn register_result_fn<N, A, F, R, S>(&mut self, name: N, func: F) -> &mut Self pub fn register_result_fn<A, R, S>(
where &mut self,
N: AsRef<str> + Into<Identifier>, name: impl AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, R, RhaiResultOf<S>>, func: impl RegisterNativeFunction<A, R, RhaiResultOf<S>>,
{ ) -> &mut Self {
self.register_fn(name, func) self.register_fn(name, func)
} }
/// Register a getter function for a member of a registered type with the [`Engine`]. /// Register a getter function for a member of a registered type with the [`Engine`].

View File

@ -53,11 +53,11 @@ impl Engine {
/// # } /// # }
/// ``` /// ```
#[inline] #[inline]
pub fn register_fn<N, A, F, R, S>(&mut self, name: N, func: F) -> &mut Self pub fn register_fn<A, R, S, F: RegisterNativeFunction<A, R, S>>(
where &mut self,
N: AsRef<str> + Into<Identifier>, name: impl AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, R, S>, func: F,
{ ) -> &mut Self {
let param_types = F::param_types(); let param_types = F::param_types();
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
@ -109,16 +109,12 @@ impl Engine {
/// ///
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()` /// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
#[inline(always)] #[inline(always)]
pub fn register_raw_fn<N, T>( pub fn register_raw_fn<T: Variant + Clone>(
&mut self, &mut self,
name: N, name: impl AsRef<str> + Into<Identifier>,
arg_types: impl AsRef<[TypeId]>, arg_types: impl AsRef<[TypeId]>,
func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static, func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
) -> &mut Self ) -> &mut Self {
where
N: AsRef<str> + Into<Identifier>,
T: Variant + Clone,
{
self.global_namespace_mut().set_raw_fn( self.global_namespace_mut().set_raw_fn(
name, name,
FnNamespace::Global, FnNamespace::Global,

View File

@ -11,20 +11,23 @@ use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf};
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{any::TypeId, mem}; use std::{any::TypeId, mem};
// These types are used to build a unique _marker_ tuple type for each combination /// These types are used to build a unique _marker_ tuple type for each combination
// of function parameter types in order to make each trait implementation unique. /// of function parameter types in order to make each trait implementation unique.
// That is because stable Rust currently does not allow distinguishing implementations ///
// based purely on parameter types of traits (`Fn`, `FnOnce` and `FnMut`). /// That is because stable Rust currently does not allow distinguishing implementations
// /// based purely on parameter types of traits (`Fn`, `FnOnce` and `FnMut`).
// For example: ///
// /// # Examples
// `NativeFunction<(Mut<A>, B, Ref<C>), R>` ///
// /// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, ()>` = `Fn(&mut A, B, &C) -> R`
// will have the function prototype constraint to: ///
// /// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, NativeCallContext>` = `Fn(NativeCallContext, &mut A, B, &C) -> R`
// `FN: (&mut A, B, &C) -> R` ///
// /// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, RhaiResultOf<()>>` = `Fn(&mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
// These types are not actually used anywhere. ///
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, RhaiResultOf<NativeCallContext>>` = `Fn(NativeCallContext, &mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
///
/// These types are not actually used anywhere.
pub struct Mut<T>(T); pub struct Mut<T>(T);
//pub struct Ref<T>(T); //pub struct Ref<T>(T);
@ -60,6 +63,11 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
} }
/// Trait to register custom Rust functions. /// Trait to register custom Rust functions.
///
/// # Type Parameters
///
/// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by [`Mut<T>`].
/// * `RET` - return type of the function; if the function returns `Result`, it is the unwrapped inner value type.
pub trait RegisterNativeFunction<ARGS, RET, RESULT> { pub trait RegisterNativeFunction<ARGS, RET, RESULT> {
/// Convert this function into a [`CallableFunction`]. /// Convert this function into a [`CallableFunction`].
#[must_use] #[must_use]

View File

@ -1245,18 +1245,14 @@ impl Module {
/// assert!(module.contains_fn(hash)); /// assert!(module.contains_fn(hash));
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn set_raw_fn<T, F>( pub fn set_raw_fn<T: Variant + Clone>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
namespace: FnNamespace, namespace: FnNamespace,
access: FnAccess, access: FnAccess,
arg_types: impl AsRef<[TypeId]>, arg_types: impl AsRef<[TypeId]>,
func: F, func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
) -> u64 ) -> u64 {
where
T: Variant + Clone,
F: Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
{
let f = let f =
move |ctx: NativeCallContext, args: &mut FnCallArgs| func(ctx, args).map(Dynamic::from); move |ctx: NativeCallContext, args: &mut FnCallArgs| func(ctx, args).map(Dynamic::from);
@ -1293,11 +1289,14 @@ impl Module {
/// assert!(module.contains_fn(hash)); /// assert!(module.contains_fn(hash));
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn set_native_fn<ARGS, N, T, F, S>(&mut self, name: N, func: F) -> u64 pub fn set_native_fn<A, T, F, S>(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
func: F,
) -> u64
where where
N: AsRef<str> + Into<Identifier>,
T: Variant + Clone, T: Variant + Clone,
F: RegisterNativeFunction<ARGS, T, RhaiResultOf<S>>, F: RegisterNativeFunction<A, T, RhaiResultOf<S>>,
{ {
self.set_fn( self.set_fn(
name, name,
@ -1587,16 +1586,17 @@ impl Module {
/// ``` /// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline(always)] #[inline(always)]
pub fn set_indexer_get_set_fn<A, B, T, S1, S2>( pub fn set_indexer_get_set_fn<
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S1>> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S2>> + SendSync + 'static,
) -> (u64, u64)
where
A: Variant + Clone, A: Variant + Clone,
B: Variant + Clone, B: Variant + Clone,
T: Variant + Clone, T: Variant + Clone,
{ S1,
S2,
>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S1>> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S2>> + SendSync + 'static,
) -> (u64, u64) {
( (
self.set_indexer_get_fn(get_fn), self.set_indexer_get_fn(get_fn),
self.set_indexer_set_fn(set_fn), self.set_indexer_set_fn(set_fn),

View File

@ -515,41 +515,31 @@ impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> serde::de::SeqAccess<'de>
/// `MapAccess` implementation for maps. /// `MapAccess` implementation for maps.
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
struct IterateMap<'a, KEYS, VALUES> struct IterateMap<'a, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>> {
where
KEYS: Iterator<Item = &'a str>,
VALUES: Iterator<Item = &'a Dynamic>,
{
// Iterator for a stream of [`Dynamic`][crate::Dynamic] keys. // Iterator for a stream of [`Dynamic`][crate::Dynamic] keys.
keys: KEYS, keys: K,
// Iterator for a stream of [`Dynamic`][crate::Dynamic] values. // Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
values: VALUES, values: V,
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
impl<'a, KEYS, VALUES> IterateMap<'a, KEYS, VALUES> impl<'a, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>> IterateMap<'a, K, V> {
where
KEYS: Iterator<Item = &'a str>,
VALUES: Iterator<Item = &'a Dynamic>,
{
#[must_use] #[must_use]
pub fn new(keys: KEYS, values: VALUES) -> Self { pub fn new(keys: K, values: V) -> Self {
Self { keys, values } Self { keys, values }
} }
} }
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
impl<'a: 'de, 'de, KEYS, VALUES> serde::de::MapAccess<'de> for IterateMap<'a, KEYS, VALUES> impl<'a: 'de, 'de, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>>
where serde::de::MapAccess<'de> for IterateMap<'a, K, V>
KEYS: Iterator<Item = &'a str>,
VALUES: Iterator<Item = &'a Dynamic>,
{ {
type Error = RhaiError; type Error = RhaiError;
fn next_key_seed<K: serde::de::DeserializeSeed<'de>>( fn next_key_seed<S: serde::de::DeserializeSeed<'de>>(
&mut self, &mut self,
seed: K, seed: S,
) -> RhaiResultOf<Option<K::Value>> { ) -> RhaiResultOf<Option<S::Value>> {
// Deserialize each `Identifier` key coming out of the keys iterator. // Deserialize each `Identifier` key coming out of the keys iterator.
match self.keys.next() { match self.keys.next() {
None => Ok(None), None => Ok(None),
@ -559,10 +549,10 @@ where
} }
} }
fn next_value_seed<V: serde::de::DeserializeSeed<'de>>( fn next_value_seed<S: serde::de::DeserializeSeed<'de>>(
&mut self, &mut self,
seed: V, seed: S,
) -> RhaiResultOf<V::Value> { ) -> RhaiResultOf<S::Value> {
// Deserialize each value item coming out of the iterator. // Deserialize each value item coming out of the iterator.
seed.deserialize(&mut DynamicDeserializer::from_dynamic( seed.deserialize(&mut DynamicDeserializer::from_dynamic(
self.values.next().unwrap(), self.values.next().unwrap(),