rune/compile/
named.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use core::cmp::Ordering;
use core::fmt;
use core::marker::PhantomData;

use crate as rune;
use crate::module::InstallWith;
use crate::{item, Item};

/// The trait used for something that can be statically named.
pub trait Named {
    /// The name item.
    const ITEM: &'static Item;

    /// The exact type name
    fn full_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", Self::ITEM)
    }

    /// Return a display wrapper for the named type.
    #[inline]
    fn display() -> impl fmt::Display {
        struct DisplayNamed<T>(PhantomData<T>)
        where
            T: ?Sized;

        impl<T> DisplayNamed<T>
        where
            T: ?Sized,
        {
            fn new() -> Self {
                Self(PhantomData)
            }
        }

        impl<T> fmt::Display for DisplayNamed<T>
        where
            T: ?Sized + Named,
        {
            #[inline]
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                T::full_name(f)
            }
        }

        DisplayNamed::<Self>::new()
    }
}

impl Named for i64 {
    const ITEM: &'static Item = item!(::std::i64);
}

impl InstallWith for i64 {}

impl Named for u64 {
    const ITEM: &'static Item = item!(::std::u64);
}

impl InstallWith for u64 {}

impl Named for f64 {
    const ITEM: &'static Item = item!(::std::f64);
}

impl InstallWith for f64 {}

impl Named for char {
    const ITEM: &'static Item = item!(::std::char);
}

impl InstallWith for char {}

impl Named for bool {
    const ITEM: &'static Item = item!(::std::bool);
}

impl InstallWith for bool {}

impl Named for Ordering {
    const ITEM: &'static Item = item!(::std::cmp::Ordering);
}

impl InstallWith for Ordering {}