rune/modules/
inner_macros.rs

1macro_rules! unsigned {
2    ($m:ident, $ty:ty) => {
3        unsigned!($m, $ty, stringify!($ty));
4    };
5
6    ($m:ident, $ty:ty, $n:expr) => {
7        $m.function("parse", parse).build()?;
8        $m.function_meta(to_float)?;
9
10        $m.function_meta(max__meta)?;
11        $m.function_meta(min__meta)?;
12        $m.function_meta(pow)?;
13
14        $m.function_meta(checked_add)?;
15        $m.function_meta(checked_sub)?;
16        $m.function_meta(checked_div)?;
17        $m.function_meta(checked_mul)?;
18        $m.function_meta(checked_rem)?;
19
20        $m.function_meta(wrapping_add)?;
21        $m.function_meta(wrapping_sub)?;
22        $m.function_meta(wrapping_div)?;
23        $m.function_meta(wrapping_mul)?;
24        $m.function_meta(wrapping_rem)?;
25
26        $m.function_meta(saturating_add)?;
27        $m.function_meta(saturating_sub)?;
28        $m.function_meta(saturating_mul)?;
29        $m.function_meta(saturating_pow)?;
30
31        $m.function_meta(to_string)?;
32
33        $m.function_meta(clone__meta)?;
34        $m.implement_trait::<$ty>(rune::item!(::std::clone::Clone))?;
35
36        $m.function_meta(partial_eq__meta)?;
37        $m.implement_trait::<$ty>(rune::item!(::std::cmp::PartialEq))?;
38
39        $m.function_meta(eq__meta)?;
40        $m.implement_trait::<$ty>(rune::item!(::std::cmp::Eq))?;
41
42        $m.function_meta(partial_cmp__meta)?;
43        $m.implement_trait::<$ty>(rune::item!(::std::cmp::PartialOrd))?;
44
45        $m.function_meta(cmp__meta)?;
46        $m.implement_trait::<$ty>(rune::item!(::std::cmp::Ord))?;
47
48        $m.constant("MIN", <$ty>::MIN).build()?.docs(docstring! {
49            /// The smallest value that can be represented by this integer type
50            /// (&minus;2<sup>63</sup>).
51            ///
52            /// # Examples
53            ///
54            /// Basic usage:
55            ///
56            /// ```rune
57            #[doc = concat!(" assert_eq!(", $n, "::MIN, -9223372036854775808);")]
58            /// ```
59        })?;
60
61        $m.constant("MAX", <$ty>::MAX).build()?.docs(docstring! {
62            /// The largest value that can be represented by this integer type
63            /// (2<sup>63</sup> &minus; 1).
64            ///
65            /// # Examples
66            ///
67            /// Basic usage:
68            ///
69            /// ```rune
70            #[doc = concat!(" assert_eq!(", $n, "::MAX, 9223372036854775807);")]
71            /// ```
72        })?;
73    };
74}
75
76macro_rules! unsigned_fns {
77    ($ty:ty) => {
78        unsigned_fns!($ty, stringify!($ty));
79    };
80
81    ($ty:ty, $n:expr) => {
82        unsigned_fns! {
83            inner $ty, $n,
84            checked_div {
85                #[doc = concat!(" assert_eq!(128", $n, ".checked_div(2), Some(64));")]
86                #[doc = concat!(" assert_eq!(1", $n, ".checked_div(0), None);")]
87            },
88            saturating_pow {
89                #[doc = concat!(" assert_eq!(4", $n, ".saturating_pow(3), 64);")]
90                #[doc = concat!(" assert_eq!(", $n, "::MAX.saturating_pow(2), ", $n, "::MAX);")]
91            },
92            checked_rem {
93                #[doc = concat!(" assert_eq!(5", $n, ".checked_rem(2), Some(1));")]
94                #[doc = concat!(" assert_eq!(5", $n, ".checked_rem(0), None);")]
95            },
96            wrapping_sub {
97                #[doc = concat!(" assert_eq!(200", $n, ".wrapping_add(55), 255);")]
98                #[doc = concat!(" assert_eq!(200", $n, ".wrapping_add(", $n, "::MAX), 199);")]
99            },
100            saturating_add {
101                #[doc = concat!(" assert_eq!(100", $n, ".saturating_add(1), 101);")]
102                #[doc = concat!(" assert_eq!(", $n, "::MAX.saturating_add(127), ", $n, "::MAX);")]
103            },
104            saturating_sub {
105                #[doc = concat!(" assert_eq!(100", $n, ".saturating_sub(27), 73);")]
106                #[doc = concat!(" assert_eq!(13", $n, ".saturating_sub(127), 0);")]
107            },
108            to_string {
109                #[doc = concat!(" assert_eq!(10", $n, ".to_string(), \"10\");")]
110            },
111        }
112    };
113
114    (
115        inner $ty:ty, $n:expr,
116        checked_div { $(#[$checked_div:meta])* },
117        saturating_pow { $(#[$saturating_pow:meta])* },
118        checked_rem { $(#[$checked_rem:meta])* },
119        wrapping_sub { $(#[$wrapping_sub:meta])* },
120        saturating_add { $(#[$saturating_add:meta])* },
121        saturating_sub { $(#[$saturating_sub:meta])* },
122        to_string { $(#[$to_string:meta])* },
123    ) => {
124        #[doc = concat!(" Parse an `", $n, "`.")]
125        ///
126        /// # Examples
127        ///
128        /// ```rune
129        #[doc = concat!(" assert_eq!(", $n, "::parse(\"10\")?, 10", $n, ");")]
130        /// ```
131        fn parse(s: &str) -> Result<$ty, ParseIntError> {
132            str::parse::<$ty>(s)
133        }
134
135        #[doc = concat!(" Converts an `", $n, "` to a `f64`.")]
136        ///
137        /// # Examples
138        ///
139        /// ```rune
140        #[doc = concat!(" assert!(10", $n, ".to::<f64>() is f64);")]
141        /// ```
142        #[rune::function(instance, path = to::<f64>)]
143        #[inline]
144        fn to_float(value: $ty) -> f64 {
145            value as f64
146        }
147
148        /// Compares and returns the maximum of two values.
149        ///
150        /// Returns the second argument if the comparison determines them to be
151        /// equal.
152        ///
153        /// # Examples
154        ///
155        /// ```rune
156        #[doc = concat!(" assert_eq!(1", $n, ".max(2", $n, "), 2", $n, ");")]
157        #[doc = concat!(" assert_eq!(2", $n, ".max(2", $n, "), 2", $n, ");")]
158        /// ```
159        #[rune::function(keep, instance, protocol = MAX)]
160        #[inline]
161        fn max(this: $ty, other: $ty) -> $ty {
162            <$ty>::max(this, other)
163        }
164
165        /// Compares and returns the minimum of two values.
166        ///
167        /// Returns the first argument if the comparison determines them to be equal.
168        ///
169        /// # Examples
170        ///
171        /// ```rune
172        #[doc = concat!(" assert_eq!(1", $n, ".min(2", $n, "), 1", $n, ");")]
173        #[doc = concat!(" assert_eq!(2", $n, ".min(2", $n, "), 2", $n, ");")]
174        /// ```
175        #[rune::function(keep, instance, protocol = MIN)]
176        #[inline]
177        fn min(this: $ty, other: $ty) -> $ty {
178            <$ty>::min(this, other)
179        }
180
181        /// Raises self to the power of `exp`, using exponentiation by squaring.
182        ///
183        /// # Overflow behavior
184        ///
185        /// This function will wrap on overflow.
186        ///
187        /// # Examples
188        ///
189        /// Basic usage:
190        ///
191        /// ```rune
192        /// let x = 2;
193        ///
194        /// assert_eq!(x.pow(5), 32);
195        /// ```
196        #[rune::function(instance)]
197        #[inline]
198        fn pow(this: $ty, pow: u32) -> $ty {
199            <$ty>::wrapping_pow(this, pow)
200        }
201
202        /// Checked integer addition. Computes `self + rhs`, returning `None` if
203        /// overflow occurred.
204        ///
205        /// # Examples
206        ///
207        /// Basic usage:
208        ///
209        /// ```rune
210        #[doc = concat!(" assert_eq!((", $n, "::MAX - 2).checked_add(1), Some(", $n, "::MAX - 1));")]
211        #[doc = concat!(" assert_eq!((", $n, "::MAX - 2).checked_add(3), None);")]
212        /// ```
213        #[rune::function(instance)]
214        #[inline]
215        fn checked_add(this: $ty, rhs: $ty) -> Option<$ty> {
216            <$ty>::checked_add(this, rhs)
217        }
218
219        /// Checked integer subtraction. Computes `self - rhs`, returning `None` if
220        /// overflow occurred.
221        ///
222        /// # Examples
223        ///
224        /// Basic usage:
225        ///
226        /// ```rune
227        #[doc = concat!(" assert_eq!((", $n, "::MIN + 2).checked_sub(1), Some(", $n, "::MIN + 1));")]
228        #[doc = concat!(" assert_eq!((", $n, "::MIN + 2).checked_sub(3), None);")]
229        /// ```
230        #[rune::function(instance)]
231        #[inline]
232        fn checked_sub(this: $ty, rhs: $ty) -> Option<$ty> {
233            <$ty>::checked_sub(this, rhs)
234        }
235
236        /// Checked integer division. Computes `self / rhs`, returning `None` if
237        /// `rhs == 0` or the division results in overflow.
238        ///
239        /// # Examples
240        ///
241        /// Basic usage:
242        ///
243        /// ```rune
244        $(#[$checked_div])*
245        /// ``````
246        #[rune::function(instance)]
247        #[inline]
248        fn checked_div(this: $ty, rhs: $ty) -> Option<$ty> {
249            <$ty>::checked_div(this, rhs)
250        }
251
252        /// Checked integer multiplication. Computes `self * rhs`, returning `None` if
253        /// overflow occurred.
254        ///
255        /// # Examples
256        ///
257        /// Basic usage:
258        ///
259        /// ```rune
260        #[doc = concat!(" assert_eq!(", $n, "::MAX.checked_mul(1), Some(", $n, "::MAX));")]
261        #[doc = concat!(" assert_eq!(", $n, "::MAX.checked_mul(2), None);")]
262        /// ```
263        #[rune::function(instance)]
264        #[inline]
265        fn checked_mul(this: $ty, rhs: $ty) -> Option<$ty> {
266            <$ty>::checked_mul(this, rhs)
267        }
268
269        /// Checked integer remainder. Computes `self % rhs`, returning `None` if `rhs
270        /// == 0` or the division results in overflow.
271        ///
272        /// # Examples
273        ///
274        /// Basic usage:
275        ///
276        /// ```rune
277        $(#[$checked_rem])*
278        /// ```
279        #[rune::function(instance)]
280        #[inline]
281        fn checked_rem(this: $ty, rhs: $ty) -> Option<$ty> {
282            <$ty>::checked_rem(this, rhs)
283        }
284
285        /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
286        /// boundary of the type.
287        ///
288        /// # Examples
289        ///
290        /// Basic usage:
291        ///
292        /// ```rune
293        #[doc = concat!(" assert_eq!(100", $n, ".wrapping_add(27), 127", $n, ");")]
294        #[doc = concat!(" assert_eq!(", $n, "::MAX.wrapping_add(2), ", $n, "::MIN + 1);")]
295        /// ```
296        #[rune::function(instance)]
297        #[inline]
298        fn wrapping_add(this: $ty, rhs: $ty) -> $ty {
299            <$ty>::wrapping_add(this, rhs)
300        }
301
302        /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at
303        /// the boundary of the type.
304        ///
305        /// # Examples
306        ///
307        /// Basic usage:
308        ///
309        /// ```rune
310        $(#[$wrapping_sub])*
311        /// ```
312        #[rune::function(instance)]
313        #[inline]
314        fn wrapping_sub(this: $ty, rhs: $ty) -> $ty {
315            <$ty>::wrapping_sub(this, rhs)
316        }
317
318        /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
319        /// boundary of the type.
320        ///
321        /// The only case where such wrapping can occur is when one divides `MIN / -1`
322        /// on a signed type (where `MIN` is the negative minimal value for the type);
323        /// this is equivalent to `-MIN`, a positive value that is too large to
324        /// represent in the type. In such a case, this function returns `MIN` itself.
325        ///
326        /// # Panics
327        ///
328        /// This function will panic if `rhs` is 0.
329        ///
330        /// # Examples
331        ///
332        /// Basic usage:
333        ///
334        /// ```rune
335        #[doc = concat!(" assert_eq!(100", $n, ".wrapping_div(10), 10", $n, ");")]
336        /// ```
337        #[rune::function(instance)]
338        #[inline]
339        fn wrapping_div(this: $ty, rhs: $ty) -> VmResult<$ty> {
340            if rhs == 0 {
341                return VmResult::err(VmErrorKind::DivideByZero);
342            }
343
344            VmResult::Ok(<$ty>::wrapping_div(this, rhs))
345        }
346
347        /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
348        /// the boundary of the type.
349        ///
350        /// # Examples
351        ///
352        /// Basic usage:
353        ///
354        /// ```rune
355        #[doc = concat!(" assert_eq!(10", $n, ".wrapping_mul(12), 120", $n, ");")]
356        /// ```
357        #[rune::function(instance)]
358        #[inline]
359        fn wrapping_mul(this: $ty, rhs: $ty) -> $ty {
360            <$ty>::wrapping_mul(this, rhs)
361        }
362
363        /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the
364        /// boundary of the type.
365        ///
366        /// Such wrap-around never actually occurs mathematically; implementation
367        /// artifacts make `x % y` invalid for `MIN / -1` on a signed type (where `MIN`
368        /// is the negative minimal value). In such a case, this function returns `0`.
369        ///
370        /// # Panics
371        ///
372        /// This function will panic if `rhs` is 0.
373        ///
374        /// # Examples
375        ///
376        /// Basic usage:
377        ///
378        /// ```rune
379        #[doc = concat!(" assert_eq!(100", $n, ".wrapping_rem(10), 0);")]
380        /// ```
381        #[rune::function(instance)]
382        #[inline]
383        fn wrapping_rem(this: $ty, rhs: $ty) -> VmResult<$ty> {
384            if rhs == 0 {
385                return VmResult::err(VmErrorKind::DivideByZero);
386            }
387
388            VmResult::Ok(<$ty>::wrapping_rem(this, rhs))
389        }
390
391        /// Saturating integer addition. Computes `self + rhs`, saturating at the
392        /// numeric bounds instead of overflowing.
393        ///
394        /// # Examples
395        ///
396        /// Basic usage:
397        ///
398        /// ```rune
399        $(#[$saturating_add])*
400        /// ```
401        #[rune::function(instance)]
402        #[inline]
403        fn saturating_add(this: $ty, rhs: $ty) -> $ty {
404            <$ty>::saturating_add(this, rhs)
405        }
406
407        /// Saturating integer subtraction. Computes `self - rhs`, saturating at the
408        /// numeric bounds instead of overflowing.
409        ///
410        /// # Examples
411        ///
412        /// Basic usage:
413        ///
414        /// ```rune
415        $(#[$saturating_sub])*
416        /// ```
417        #[rune::function(instance)]
418        #[inline]
419        fn saturating_sub(this: $ty, rhs: $ty) -> $ty {
420            <$ty>::saturating_sub(this, rhs)
421        }
422
423        /// Saturating integer multiplication. Computes `self * rhs`, saturating at the
424        /// numeric bounds instead of overflowing.
425        ///
426        /// # Examples
427        ///
428        /// Basic usage:
429        ///
430        /// ```rune
431        #[doc = concat!(" assert_eq!(10", $n, ".saturating_mul(12), 120);")]
432        #[doc = concat!(" assert_eq!(", $n, "::MAX.saturating_mul(10), ", $n, "::MAX);")]
433        #[doc = concat!(" assert_eq!(", $n, "::MIN.saturating_mul(10), ", $n, "::MIN);")]
434        /// ```
435        #[rune::function(instance)]
436        #[inline]
437        fn saturating_mul(this: $ty, rhs: $ty) -> $ty {
438            <$ty>::saturating_mul(this, rhs)
439        }
440
441        /// Saturating integer exponentiation. Computes `self.pow(exp)`, saturating at
442        /// the numeric bounds instead of overflowing.
443        ///
444        /// # Examples
445        ///
446        /// Basic usage:
447        ///
448        /// ```rune
449        $(#[$saturating_pow])*
450        /// ```
451        #[rune::function(instance)]
452        #[inline]
453        fn saturating_pow(this: $ty, rhs: u32) -> $ty {
454            <$ty>::saturating_pow(this, rhs)
455        }
456
457        #[doc = concat!(" Clone a `", $n, "`.")]
458        ///
459        /// Note that since the type is copy, cloning has the same effect as assigning
460        /// it.
461        ///
462        /// # Examples
463        ///
464        /// ```rune
465        #[doc = concat!(" let a = 5", $n, ";")]
466        /// let b = a;
467        /// let c = a.clone();
468        ///
469        /// a += 1;
470        ///
471        /// assert_eq!(a, 6);
472        /// assert_eq!(b, 5);
473        /// assert_eq!(c, 5);
474        /// ```
475        #[rune::function(keep, instance, protocol = CLONE)]
476        #[inline]
477        fn clone(this: $ty) -> $ty {
478            this
479        }
480
481        /// Test two integers for partial equality.
482        ///
483        /// # Examples
484        ///
485        /// ```rune
486        #[doc = concat!("  assert_eq!(5", $n, " == 5, true);")]
487        #[doc = concat!("  assert_eq!(5", $n, " == 10, false);")]
488        #[doc = concat!("  assert_eq!(10", $n, " == 5, false);")]
489        /// ```
490        #[rune::function(keep, instance, protocol = PARTIAL_EQ)]
491        #[inline]
492        fn partial_eq(this: $ty, rhs: $ty) -> bool {
493            this.eq(&rhs)
494        }
495
496        /// Test two integers for total equality.
497        ///
498        /// # Examples
499        ///
500        /// ```rune
501        /// use std::ops::eq;
502        ///
503        #[doc = concat!("  assert_eq!(eq(5", $n, ", 5", $n, "), true);")]
504        #[doc = concat!("  assert_eq!(eq(5", $n, ", 10", $n, "), false);")]
505        #[doc = concat!("  assert_eq!(eq(10", $n, ", 5", $n, "), false);")]
506        /// ```
507        #[rune::function(keep, instance, protocol = EQ)]
508        #[inline]
509        fn eq(this: $ty, rhs: $ty) -> bool {
510            this.eq(&rhs)
511        }
512
513        /// Perform a partial ordered comparison between two integers.
514        ///
515        /// # Examples
516        ///
517        /// ```rune
518        /// use std::cmp::Ordering;
519        /// use std::ops::partial_cmp;
520        ///
521        #[doc = concat!(" assert_eq!(partial_cmp(5", $n, ", 10", $n, "), Some(Ordering::Less));")]
522        #[doc = concat!(" assert_eq!(partial_cmp(10", $n, ", 5", $n, "), Some(Ordering::Greater));")]
523        #[doc = concat!(" assert_eq!(partial_cmp(5", $n, ", 5", $n, "), Some(Ordering::Equal));")]
524        /// ```
525        #[rune::function(keep, instance, protocol = PARTIAL_CMP)]
526        #[inline]
527        fn partial_cmp(this: $ty, rhs: $ty) -> Option<Ordering> {
528            this.partial_cmp(&rhs)
529        }
530
531        /// Perform a totally ordered comparison between two integers.
532        ///
533        /// # Examples
534        ///
535        /// ```rune
536        /// use std::cmp::Ordering;
537        /// use std::ops::cmp;
538        ///
539        #[doc = concat!(" assert_eq!(cmp(5", $n, ", 10", $n, "), Ordering::Less);")]
540        #[doc = concat!(" assert_eq!(cmp(10", $n, ", 5", $n, "), Ordering::Greater);")]
541        #[doc = concat!(" assert_eq!(cmp(5", $n, ", 5", $n, "), Ordering::Equal);")]
542        /// ```
543        #[rune::function(keep, instance, protocol = CMP)]
544        #[inline]
545        fn cmp(this: $ty, rhs: $ty) -> Ordering {
546            this.cmp(&rhs)
547        }
548
549        /// Returns the number as a string.
550        ///
551        /// # Examples
552        ///
553        /// Basic usage:
554        ///
555        /// ```rune
556        $(#[$to_string])*
557        /// ```
558        #[rune::function(instance)]
559        #[inline]
560        fn to_string(this: $ty) -> VmResult<alloc::String> {
561            VmResult::Ok(vm_try!(this.try_to_string()))
562        }
563    };
564}
565
566macro_rules! signed {
567    ($m:ident, $ty:ty) => {
568        unsigned!($m, $ty, stringify!($ty));
569
570        $m.function_meta(abs)?;
571        $m.function_meta(saturating_abs)?;
572        $m.function_meta(signum)?;
573        $m.function_meta(is_positive)?;
574        $m.function_meta(is_negative)?;
575    };
576}
577
578macro_rules! signed_fns {
579    ($ty:ty) => {
580        signed_fns!($ty, stringify!($ty));
581    };
582
583    ($ty:ty, $n:expr) => {
584        unsigned_fns! {
585            inner $ty, $n,
586            checked_div {
587                #[doc = concat!(" assert_eq!((", $n, "::MIN + 1).checked_div(-1), Some(", $n, "::MAX));")]
588                #[doc = concat!(" assert_eq!(", $n, "::MIN.checked_div(-1), None);")]
589                #[doc = concat!(" assert_eq!(1", $n, ".checked_div(0), None);")]
590            },
591            saturating_pow {
592                /// assert_eq!((-4).saturating_pow(3), -64);
593                #[doc = concat!(" assert_eq!(", $n, "::MIN.saturating_pow(2), ", $n, "::MAX);")]
594                #[doc = concat!(" assert_eq!(", $n, "::MIN.saturating_pow(3), ", $n, "::MIN);")]
595            },
596            checked_rem {
597                #[doc = concat!(" assert_eq!(5", $n, ".checked_rem(2), Some(1));")]
598                #[doc = concat!(" assert_eq!(5", $n, ".checked_rem(0), None);")]
599                #[doc = concat!(" assert_eq!(", $n, "::MIN.checked_rem(-1), None);")]
600            },
601            wrapping_sub {
602                /// assert_eq!(0.wrapping_sub(127), -127);
603                #[doc = concat!(" assert_eq!((-2", $n, ").wrapping_sub(", $n, "::MAX), ", $n, "::MAX);")]
604            },
605            saturating_add {
606                /// assert_eq!(100.saturating_add(1), 101);
607                #[doc = concat!(" assert_eq!(", $n, "::MAX.saturating_add(100), ", $n, "::MAX);")]
608                #[doc = concat!(" assert_eq!(", $n, "::MIN.saturating_add(-1), ", $n, "::MIN);")]
609            },
610            saturating_sub {
611                /// assert_eq!(100.saturating_sub(127), -27);
612                #[doc = concat!(" assert_eq!(", $n, "::MIN.saturating_sub(100), ", $n, "::MIN);")]
613                #[doc = concat!(" assert_eq!(", $n, "::MAX.saturating_sub(-1), ", $n, "::MAX);")]
614            },
615            to_string {
616                #[doc = concat!(" assert_eq!((-10", $n, ").to_string(), \"-10\");")]
617                #[doc = concat!(" assert_eq!(10", $n, ".to_string(), \"10\");")]
618            },
619        }
620
621        /// Computes the absolute value of `self`.
622        ///
623        /// # Overflow behavior
624        ///
625        #[doc = concat!(" The absolute value of `", $n, "::MIN` cannot be represented as an `int`,")]
626        /// and attempting to calculate it will cause an overflow. This means
627        #[doc = concat!(" that such code will wrap to `", $n, "::MIN` without a panic.")]
628        ///
629        /// # Examples
630        ///
631        /// Basic usage:
632        ///
633        /// ```rune
634        /// assert_eq!(10.abs(), 10);
635        /// assert_eq!((-10).abs(), 10);
636        /// ```
637        #[rune::function(instance)]
638        #[inline]
639        fn abs(this: $ty) -> $ty {
640            <$ty>::wrapping_abs(this)
641        }
642
643        /// Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self
644        /// == MIN` instead of overflowing.
645        ///
646        /// # Examples
647        ///
648        /// Basic usage:
649        ///
650        /// ```rune
651        /// assert_eq!(100.saturating_abs(), 100);
652        /// assert_eq!((-100).saturating_abs(), 100);
653        #[doc = concat!(" assert_eq!(", $n, "::MIN.saturating_abs(), ", $n, "::MAX);")]
654        #[doc = concat!(" assert_eq!((", $n, "::MIN + 1).saturating_abs(), ", $n, "::MAX);")]
655        /// ```
656        #[rune::function(instance)]
657        #[inline]
658        fn saturating_abs(this: $ty) -> $ty {
659            <$ty>::saturating_abs(this)
660        }
661
662        /// Returns a number representing sign of `self`.
663        ///
664        /// - `0` if the number is zero
665        /// - `1` if the number is positive
666        /// - `-1` if the number is negative
667        ///
668        /// # Examples
669        ///
670        /// Basic usage:
671        ///
672        /// ```rune
673        /// assert_eq!(10.signum(), 1);
674        /// assert_eq!(0.signum(), 0);
675        /// assert_eq!((-10).signum(), -1);
676        /// ```
677        #[rune::function(instance)]
678        #[inline]
679        fn signum(this: $ty) -> $ty {
680            <$ty>::signum(this)
681        }
682
683        /// Returns `true` if `self` is positive and `false` if the number is zero or
684        /// negative.
685        ///
686        /// # Examples
687        ///
688        /// Basic usage:
689        ///
690        /// ```rune
691        /// assert!(10.is_positive());
692        /// assert!(!(-10).is_positive());
693        /// ```
694        #[rune::function(instance)]
695        #[inline]
696        fn is_positive(this: $ty) -> bool {
697            <$ty>::is_positive(this)
698        }
699
700        /// Returns `true` if `self` is negative and `false` if the number is zero or
701        /// positive.
702        ///
703        /// # Examples
704        ///
705        /// Basic usage:
706        ///
707        /// ```rune
708        /// assert!((-10).is_negative());
709        /// assert!(!10.is_negative());
710        /// ```
711        #[rune::function(instance)]
712        #[inline]
713        fn is_negative(this: $ty) -> bool {
714            <$ty>::is_negative(this)
715        }
716    }
717}