rune/runtime/
macros.rs

1macro_rules! inline_macros {
2    ($path:path) => {
3        $path! {
4            /// Coerce into [`Ordering`].
5            Ordering(Ordering),
6            as_ordering,
7            as_ordering_mut,
8        }
9
10        $path! {
11            /// Coerce into [`bool`].
12            Bool(bool),
13            as_bool,
14            as_bool_mut,
15        }
16
17        $path! {
18            /// Coerce into [`char`].
19            Char(char),
20            as_char,
21            as_char_mut,
22        }
23
24        $path! {
25            /// Coerce into [`u64`] unsigned integer.
26            Unsigned(u64),
27            as_unsigned,
28            as_unsigned_mut,
29        }
30
31        $path! {
32            /// Coerce into [`i64`] signed integer.
33            Signed(i64),
34            as_signed,
35            as_signed_mut,
36        }
37
38        $path! {
39            /// Coerce into [`f64`] float.
40            Float(f64),
41            as_float,
42            as_float_mut,
43        }
44
45        $path! {
46            /// Coerce into [`Type`].
47            Type(Type),
48            as_type,
49            as_type_mut,
50        }
51    };
52}
53
54macro_rules! range_iter {
55    ($range:ident, $name:ident<$ty:ident> $(, { $($item:tt)* })?) => {
56        #[derive(Any)]
57        #[rune(item = ::std::ops)]
58        pub(crate) struct $name<$ty>
59        where
60            $ty: 'static + $crate::alloc::clone::TryClone,
61            $ty: $crate::compile::Named,
62            $ty: $crate::runtime::FromValue + $crate::runtime::ToValue,
63            $ty: $crate::runtime::MaybeTypeOf + $crate::runtime::TypeOf,
64        {
65            iter: core::ops::$range<$ty>,
66        }
67
68        impl<$ty> $name<$ty>
69        where
70            $ty: 'static + $crate::alloc::clone::TryClone,
71            $ty: $crate::compile::Named,
72            $ty: $crate::runtime::FromValue + $crate::runtime::ToValue,
73            $ty: $crate::runtime::MaybeTypeOf + $crate::runtime::TypeOf,
74            core::ops::$range<$ty>: Iterator<Item = $ty>,
75        {
76            #[inline]
77            pub(crate) fn new(iter: core::ops::$range<$ty>) -> Self {
78                Self { iter }
79            }
80
81            #[rune::function(instance, keep, protocol = NEXT)]
82            #[inline]
83            pub(crate) fn next(&mut self) -> Option<$ty> {
84                self.iter.next()
85            }
86
87            $($($item)*)*
88        }
89
90        impl<$ty> Iterator for $name<$ty>
91        where
92            $ty: 'static + $crate::alloc::clone::TryClone,
93            $ty: $crate::compile::Named,
94            $ty: $crate::runtime::FromValue + $crate::runtime::ToValue,
95            $ty: $crate::runtime::MaybeTypeOf + $crate::runtime::TypeOf,
96            core::ops::$range<$ty>: Iterator<Item = $ty>,
97        {
98            type Item = $ty;
99
100            #[inline]
101            fn next(&mut self) -> Option<Self::Item> {
102                self.iter.next()
103            }
104        }
105    };
106}
107
108macro_rules! double_ended_range_iter {
109    ($range:ident, $name:ident<$ty:ident> $(, { $($item:tt)* })?) => {
110        range_iter!($range, $name<$ty> $(, { $($item)* })*);
111
112        impl<T> $name<T>
113        where
114            T: 'static + $crate::alloc::clone::TryClone,
115            T: $crate::compile::Named,
116            T: $crate::runtime::FromValue + $crate::runtime::ToValue,
117            T: $crate::runtime::MaybeTypeOf + $crate::runtime::TypeOf,
118            core::ops::$range<T>: DoubleEndedIterator<Item = T>,
119        {
120            #[rune::function(instance, keep, protocol = NEXT_BACK)]
121            #[inline]
122            pub(crate) fn next_back(&mut self) -> Option<T> {
123                self.iter.next_back()
124            }
125        }
126
127        impl<T> DoubleEndedIterator for $name<T>
128        where
129            T: 'static + $crate::alloc::clone::TryClone,
130            T: $crate::compile::Named,
131            T: $crate::runtime::FromValue + $crate::runtime::ToValue,
132            T: $crate::runtime::MaybeTypeOf + $crate::runtime::TypeOf,
133            core::ops::$range<T>: DoubleEndedIterator<Item = T>,
134        {
135            #[inline]
136            fn next_back(&mut self) -> Option<Self::Item> {
137                self.iter.next_back()
138            }
139        }
140    };
141}