rune/compile/
named.rs

1use core::cmp::Ordering;
2use core::fmt;
3use core::marker::PhantomData;
4
5use crate as rune;
6use crate::{item, Hash, Item};
7
8/// The trait used for something that can be statically named.
9pub trait Named {
10    /// The name item.
11    const ITEM: &'static Item;
12
13    /// The full name of the type.
14    #[inline]
15    fn full_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        write!(f, "{}", Self::ITEM)
17    }
18
19    /// Return a display wrapper for the full name of the type.
20    #[inline]
21    fn display() -> impl fmt::Display {
22        struct DisplayNamed<T>(PhantomData<T>)
23        where
24            T: ?Sized;
25
26        impl<T> DisplayNamed<T>
27        where
28            T: ?Sized,
29        {
30            fn new() -> Self {
31                Self(PhantomData)
32            }
33        }
34
35        impl<T> fmt::Display for DisplayNamed<T>
36        where
37            T: ?Sized + Named,
38        {
39            #[inline]
40            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41                T::full_name(f)
42            }
43        }
44
45        DisplayNamed::<Self>::new()
46    }
47}
48
49impl Named for i64 {
50    const ITEM: &'static Item = item!(::std::i64);
51}
52
53impl Named for u64 {
54    const ITEM: &'static Item = item!(::std::u64);
55}
56
57impl Named for f64 {
58    const ITEM: &'static Item = item!(::std::f64);
59}
60
61impl Named for char {
62    const ITEM: &'static Item = item!(::std::char);
63}
64
65impl Named for bool {
66    const ITEM: &'static Item = item!(::std::bool);
67}
68
69impl Named for Ordering {
70    const ITEM: &'static Item = item!(::std::cmp::Ordering);
71}
72
73impl Named for Hash {
74    const ITEM: &'static Item = item!(::std::any::Hash);
75}