use ::rust_alloc::sync::Arc;
use crate::compile::ContextError;
use crate::function_meta::{
Associated, AssociatedFunctionData, FunctionData, FunctionMetaKind, ToInstance,
};
use crate::item::IntoComponent;
use crate::module::ItemFnMut;
use crate::runtime::{FunctionHandler, TypeInfo, TypeOf};
use crate::{Hash, ItemBuf};
use super::Module;
#[must_use = "Must call one of the build functions, like `build` or `build_associated`"]
pub struct ModuleRawFunctionBuilder<'a, N> {
pub(super) module: &'a mut Module,
pub(super) name: N,
pub(super) handler: Arc<FunctionHandler>,
}
impl<'a, N> ModuleRawFunctionBuilder<'a, N> {
#[inline]
pub fn build(self) -> Result<ItemFnMut<'a>, ContextError>
where
N: IntoComponent,
{
let item = ItemBuf::with_item([self.name])?;
self.module
.function_from_meta_kind(FunctionMetaKind::Function(FunctionData::from_raw(
item,
self.handler,
)))
}
#[inline]
pub fn build_associated<T>(self) -> Result<ItemFnMut<'a>, ContextError>
where
N: ToInstance,
T: TypeOf,
{
let associated = Associated::from_type::<T>(self.name.to_instance()?)?;
self.module
.function_from_meta_kind(FunctionMetaKind::AssociatedFunction(
AssociatedFunctionData::from_raw(associated, self.handler),
))
}
#[inline]
pub fn build_associated_with(
self,
container: Hash,
container_type_info: TypeInfo,
) -> Result<ItemFnMut<'a>, ContextError>
where
N: ToInstance,
{
let associated = Associated::new(self.name.to_instance()?, container, container_type_info);
self.module
.function_from_meta_kind(FunctionMetaKind::AssociatedFunction(
AssociatedFunctionData::from_raw(associated, self.handler),
))
}
}