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
use crate::{
    prelude::*,
    string::Encoding,
    vm::InstrSeq,
};

/// A type that can be instantiated from a typed `Class` instance.
pub trait Classify: Object {
    /// Returns the typed class that can be used to get an instance of `self`.
    fn class() -> Class<Self>;
}

macro_rules! impl_trait {
    ($($t:ty, $c:ident ;)+) => { $(
        impl Classify for $t {
            #[inline]
            fn class() -> Class<Self> {
                unsafe { Class::cast_unchecked(Class::$c()) }
            }
        }
    )+ };
}

impl<O: Object> Classify for Array<O> {
    #[inline]
    fn class() -> Class<Self> {
        unsafe { Class::cast_unchecked(Class::array()) }
    }
}

impl<K: Object, V: Object> Classify for Hash<K, V> {
    #[inline]
    fn class() -> Class<Self> {
        unsafe { Class::cast_unchecked(Class::hash()) }
    }
}

impl_trait! {
    AnyObject,    object;
    Class,        class;
    Module,       module;
    Integer,      integer;
    String,       string;
    Symbol,       symbol;
    Encoding,     encoding;
    AnyException, exception;
    InstrSeq,     instr_seq;
}