Skip to content

Commit

Permalink
Add ability to specify a dynamic container
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 4, 2023
1 parent 6ada618 commit bcc5c90
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
17 changes: 15 additions & 2 deletions crates/rune/src/module/function_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,26 @@ where
where
N: ToInstance,
T: TypeOf + Named,
{
self.build_associated_with(T::type_of(), T::type_info())
}

#[doc(hidden)]
#[inline]
pub fn build_associated_with(
self,
container: FullTypeOf,
container_type_info: TypeInfo,
) -> alloc::Result<FunctionMetaKind>
where
N: ToInstance,
{
Ok(FunctionMetaKind::AssociatedFunction(
AssociatedFunctionData {
name: self.name.to_instance()?,
handler: Arc::new(move |stack, args| self.f.fn_call(stack, args)),
container: T::type_of(),
container_type_info: T::type_info(),
container,
container_type_info,
#[cfg(feature = "doc")]
is_async: K::is_async(),
#[cfg(feature = "doc")]
Expand Down
30 changes: 28 additions & 2 deletions crates/rune/src/module/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::module::{
VariantMut,
};
use crate::runtime::{
AttributeMacroHandler, ConstValue, FromValue, GeneratorState, MacroHandler, MaybeTypeOf,
Protocol, Stack, ToValue, TypeCheck, TypeOf, Value, VmResult,
AttributeMacroHandler, ConstValue, FromValue, FullTypeOf, GeneratorState, MacroHandler,
MaybeTypeOf, Protocol, Stack, ToValue, TypeCheck, TypeInfo, TypeOf, Value, VmResult,
};
use crate::Hash;

Expand All @@ -43,6 +43,7 @@ where
K: FunctionKind,
{
/// Construct a regular function.
// TODO: add code example.
#[inline]
pub fn build(self) -> Result<ItemFnMut<'a>, ContextError>
where
Expand All @@ -54,6 +55,7 @@ where
}

/// Construct a function that is associated with `T`.
// TODO: add code example.
#[inline]
pub fn build_associated<T>(self) -> Result<ItemFnMut<'a>, ContextError>
where
Expand All @@ -63,6 +65,30 @@ where
let meta = self.inner.build_associated::<T>()?;
self.module.function_from_meta_kind(meta)
}

/// Construct a function that is associated with a custom dynamically
/// specified container.
///
/// [`FullTypeOf`] and [`TypeInfo`] are usually constructed through the
/// [`TypeOf`] trait. But that requires access to a static type, for which
/// you should use [`build_associated`] instead.
///
/// [`build_associated`]: ModuleFunctionBuilder::build_associated
// TODO: add code example.
#[inline]
pub fn build_associated_with(
self,
container: FullTypeOf,
container_type_info: TypeInfo,
) -> Result<ItemFnMut<'a>, ContextError>
where
N: ToInstance,
{
let meta = self
.inner
.build_associated_with(container, container_type_info)?;
self.module.function_from_meta_kind(meta)
}
}

#[doc(hidden)]
Expand Down

0 comments on commit bcc5c90

Please sign in to comment.