Improve docs.
This commit is contained in:
parent
010a96dde3
commit
b40ca9e40d
@ -128,11 +128,11 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
|
||||
|
||||
/// Register a custom function.
|
||||
#[inline(always)]
|
||||
pub fn with_fn<N, A, F, R, S>(&mut self, name: N, method: F) -> &mut Self
|
||||
where
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
F: RegisterNativeFunction<A, R, S>,
|
||||
{
|
||||
pub fn with_fn<A, R, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
method: impl RegisterNativeFunction<A, R, S>,
|
||||
) -> &mut Self {
|
||||
self.engine.register_fn(name, method);
|
||||
self
|
||||
}
|
||||
|
@ -143,11 +143,11 @@ impl Engine {
|
||||
/// This method will be removed in the next major version.
|
||||
#[deprecated(since = "1.9.1", note = "use `register_fn` instead")]
|
||||
#[inline(always)]
|
||||
pub fn register_result_fn<N, A, F, R, S>(&mut self, name: N, func: F) -> &mut Self
|
||||
where
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
F: RegisterNativeFunction<A, R, RhaiResultOf<S>>,
|
||||
{
|
||||
pub fn register_result_fn<A, R, S>(
|
||||
&mut self,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
func: impl RegisterNativeFunction<A, R, RhaiResultOf<S>>,
|
||||
) -> &mut Self {
|
||||
self.register_fn(name, func)
|
||||
}
|
||||
/// Register a getter function for a member of a registered type with the [`Engine`].
|
||||
|
@ -53,11 +53,11 @@ impl Engine {
|
||||
/// # }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn register_fn<N, A, F, R, S>(&mut self, name: N, func: F) -> &mut Self
|
||||
where
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
F: RegisterNativeFunction<A, R, S>,
|
||||
{
|
||||
pub fn register_fn<A, R, S, F: RegisterNativeFunction<A, R, S>>(
|
||||
&mut self,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
func: F,
|
||||
) -> &mut Self {
|
||||
let param_types = F::param_types();
|
||||
|
||||
#[cfg(feature = "metadata")]
|
||||
@ -109,16 +109,12 @@ impl Engine {
|
||||
///
|
||||
/// To access the first mutable parameter, use `args.get_mut(0).unwrap()`
|
||||
#[inline(always)]
|
||||
pub fn register_raw_fn<N, T>(
|
||||
pub fn register_raw_fn<T: Variant + Clone>(
|
||||
&mut self,
|
||||
name: N,
|
||||
name: impl AsRef<str> + Into<Identifier>,
|
||||
arg_types: impl AsRef<[TypeId]>,
|
||||
func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
|
||||
) -> &mut Self
|
||||
where
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
T: Variant + Clone,
|
||||
{
|
||||
) -> &mut Self {
|
||||
self.global_namespace_mut().set_raw_fn(
|
||||
name,
|
||||
FnNamespace::Global,
|
||||
|
@ -11,20 +11,23 @@ use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf};
|
||||
use std::prelude::v1::*;
|
||||
use std::{any::TypeId, mem};
|
||||
|
||||
// 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.
|
||||
// That is because stable Rust currently does not allow distinguishing implementations
|
||||
// based purely on parameter types of traits (`Fn`, `FnOnce` and `FnMut`).
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// `NativeFunction<(Mut<A>, B, Ref<C>), R>`
|
||||
//
|
||||
// will have the function prototype constraint to:
|
||||
//
|
||||
// `FN: (&mut A, B, &C) -> R`
|
||||
//
|
||||
// These types are not actually used anywhere.
|
||||
/// 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.
|
||||
///
|
||||
/// That is because stable Rust currently does not allow distinguishing implementations
|
||||
/// based purely on parameter types of traits (`Fn`, `FnOnce` and `FnMut`).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, ()>` = `Fn(&mut A, B, &C) -> R`
|
||||
///
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, NativeCallContext>` = `Fn(NativeCallContext, &mut A, B, &C) -> R`
|
||||
///
|
||||
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), R, RhaiResultOf<()>>` = `Fn(&mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
|
||||
///
|
||||
/// `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 Ref<T>(T);
|
||||
|
||||
@ -60,6 +63,11 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
/// Convert this function into a [`CallableFunction`].
|
||||
#[must_use]
|
||||
|
@ -1245,18 +1245,14 @@ impl Module {
|
||||
/// assert!(module.contains_fn(hash));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn set_raw_fn<T, F>(
|
||||
pub fn set_raw_fn<T: Variant + Clone>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
namespace: FnNamespace,
|
||||
access: FnAccess,
|
||||
arg_types: impl AsRef<[TypeId]>,
|
||||
func: F,
|
||||
) -> u64
|
||||
where
|
||||
T: Variant + Clone,
|
||||
F: Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
|
||||
{
|
||||
func: impl Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResultOf<T> + SendSync + 'static,
|
||||
) -> u64 {
|
||||
let f =
|
||||
move |ctx: NativeCallContext, args: &mut FnCallArgs| func(ctx, args).map(Dynamic::from);
|
||||
|
||||
@ -1293,11 +1289,14 @@ impl Module {
|
||||
/// assert!(module.contains_fn(hash));
|
||||
/// ```
|
||||
#[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
|
||||
N: AsRef<str> + Into<Identifier>,
|
||||
T: Variant + Clone,
|
||||
F: RegisterNativeFunction<ARGS, T, RhaiResultOf<S>>,
|
||||
F: RegisterNativeFunction<A, T, RhaiResultOf<S>>,
|
||||
{
|
||||
self.set_fn(
|
||||
name,
|
||||
@ -1587,16 +1586,17 @@ impl Module {
|
||||
/// ```
|
||||
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
|
||||
#[inline(always)]
|
||||
pub fn set_indexer_get_set_fn<A, B, T, 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)
|
||||
where
|
||||
pub fn set_indexer_get_set_fn<
|
||||
A: Variant + Clone,
|
||||
B: 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_set_fn(set_fn),
|
||||
|
@ -515,41 +515,31 @@ impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> serde::de::SeqAccess<'de>
|
||||
|
||||
/// `MapAccess` implementation for maps.
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
struct IterateMap<'a, KEYS, VALUES>
|
||||
where
|
||||
KEYS: Iterator<Item = &'a str>,
|
||||
VALUES: Iterator<Item = &'a Dynamic>,
|
||||
{
|
||||
struct IterateMap<'a, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>> {
|
||||
// Iterator for a stream of [`Dynamic`][crate::Dynamic] keys.
|
||||
keys: KEYS,
|
||||
keys: K,
|
||||
// Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
|
||||
values: VALUES,
|
||||
values: V,
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
impl<'a, KEYS, VALUES> IterateMap<'a, KEYS, VALUES>
|
||||
where
|
||||
KEYS: Iterator<Item = &'a str>,
|
||||
VALUES: Iterator<Item = &'a Dynamic>,
|
||||
{
|
||||
impl<'a, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>> IterateMap<'a, K, V> {
|
||||
#[must_use]
|
||||
pub fn new(keys: KEYS, values: VALUES) -> Self {
|
||||
pub fn new(keys: K, values: V) -> Self {
|
||||
Self { keys, values }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
impl<'a: 'de, 'de, KEYS, VALUES> serde::de::MapAccess<'de> for IterateMap<'a, KEYS, VALUES>
|
||||
where
|
||||
KEYS: Iterator<Item = &'a str>,
|
||||
VALUES: Iterator<Item = &'a Dynamic>,
|
||||
impl<'a: 'de, 'de, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>>
|
||||
serde::de::MapAccess<'de> for IterateMap<'a, K, V>
|
||||
{
|
||||
type Error = RhaiError;
|
||||
|
||||
fn next_key_seed<K: serde::de::DeserializeSeed<'de>>(
|
||||
fn next_key_seed<S: serde::de::DeserializeSeed<'de>>(
|
||||
&mut self,
|
||||
seed: K,
|
||||
) -> RhaiResultOf<Option<K::Value>> {
|
||||
seed: S,
|
||||
) -> RhaiResultOf<Option<S::Value>> {
|
||||
// Deserialize each `Identifier` key coming out of the keys iterator.
|
||||
match self.keys.next() {
|
||||
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,
|
||||
seed: V,
|
||||
) -> RhaiResultOf<V::Value> {
|
||||
seed: S,
|
||||
) -> RhaiResultOf<S::Value> {
|
||||
// Deserialize each value item coming out of the iterator.
|
||||
seed.deserialize(&mut DynamicDeserializer::from_dynamic(
|
||||
self.values.next().unwrap(),
|
||||
|
Loading…
Reference in New Issue
Block a user