rune/module/
item_fn_mut.rs
1use core::fmt;
2
3#[cfg(feature = "doc")]
4use crate::alloc::Box;
5#[cfg(feature = "doc")]
6use crate::compile::meta;
7use crate::compile::{ContextError, Docs};
8use crate::function_meta::FunctionArgs;
9use crate::runtime::MaybeTypeOf;
10
11pub struct ItemFnMut<'a> {
31 pub(super) docs: &'a mut Docs,
32 #[cfg(feature = "doc")]
33 pub(super) deprecated: &'a mut Option<Box<str>>,
34 #[cfg(feature = "doc")]
35 pub(super) is_async: &'a mut bool,
36 #[cfg(feature = "doc")]
37 pub(super) args: &'a mut Option<usize>,
38 #[cfg(feature = "doc")]
39 pub(super) argument_types: &'a mut Box<[meta::DocType]>,
40 #[cfg(feature = "doc")]
41 pub(super) return_type: &'a mut meta::DocType,
42}
43
44impl ItemFnMut<'_> {
45 pub fn docs(self, docs: impl IntoIterator<Item: AsRef<str>>) -> Result<Self, ContextError> {
49 self.docs.set_docs(docs)?;
50 Ok(self)
51 }
52
53 pub fn is_async(self, #[cfg_attr(not(feature = "doc"), allow(unused))] is_async: bool) -> Self {
55 #[cfg(feature = "doc")]
56 {
57 *self.is_async = is_async;
58 }
59
60 self
61 }
62
63 pub fn deprecated(
65 self,
66 #[cfg_attr(not(feature = "doc"), allow(unused))] deprecated: impl AsRef<str>,
67 ) -> Result<Self, ContextError> {
68 #[cfg(feature = "doc")]
69 {
70 *self.deprecated = Some(deprecated.as_ref().try_into()?);
71 }
72
73 Ok(self)
74 }
75
76 pub fn args(self, #[cfg_attr(not(feature = "doc"), allow(unused))] args: usize) -> Self {
78 #[cfg(feature = "doc")]
79 {
80 *self.args = Some(args);
81 }
82
83 self
84 }
85
86 pub fn return_type<T>(self) -> Result<Self, ContextError>
88 where
89 T: MaybeTypeOf,
90 {
91 #[cfg(feature = "doc")]
92 {
93 *self.return_type = T::maybe_type_of()?;
94 }
95
96 Ok(self)
97 }
98
99 pub fn argument_types<A>(self) -> Result<Self, ContextError>
101 where
102 A: FunctionArgs,
103 {
104 #[cfg(feature = "doc")]
105 {
106 *self.argument_types = A::into_box()?;
107 *self.args = Some(A::len());
108 }
109
110 Ok(self)
111 }
112}
113
114impl fmt::Debug for ItemFnMut<'_> {
115 #[inline]
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 f.debug_struct("ItemFnMut").finish_non_exhaustive()
118 }
119}