diff --git a/crates/rune/src/module/function_meta.rs b/crates/rune/src/module/function_meta.rs index fd4eb92f3..b206b9ae9 100644 --- a/crates/rune/src/module/function_meta.rs +++ b/crates/rune/src/module/function_meta.rs @@ -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 + 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")] diff --git a/crates/rune/src/module/module.rs b/crates/rune/src/module/module.rs index 12bd3db2f..01029a66b 100644 --- a/crates/rune/src/module/module.rs +++ b/crates/rune/src/module/module.rs @@ -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; @@ -43,6 +43,7 @@ where K: FunctionKind, { /// Construct a regular function. + // TODO: add code example. #[inline] pub fn build(self) -> Result, ContextError> where @@ -54,6 +55,7 @@ where } /// Construct a function that is associated with `T`. + // TODO: add code example. #[inline] pub fn build_associated(self) -> Result, ContextError> where @@ -63,6 +65,30 @@ where let meta = self.inner.build_associated::()?; 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, ContextError> + where + N: ToInstance, + { + let meta = self + .inner + .build_associated_with(container, container_type_info)?; + self.module.function_from_meta_kind(meta) + } } #[doc(hidden)]