py_declare/
res.rs

1use crate::defs::FnSignWithName;
2use py_ir::types::TypeDefine;
3use std::any::Any;
4
5#[cfg(not(feature = "parallel"))]
6use std::rc::Rc;
7#[cfg(feature = "parallel")]
8use std::sync::Arc as Rc;
9
10#[derive(Debug, Clone)]
11pub enum Type {
12    Overload(Overload),
13    Directly(Directly),
14}
15
16impl Type {
17    pub fn overload(&self) -> &Overload {
18        if let Type::Overload(ol) = self {
19            ol
20        } else {
21            panic!()
22        }
23    }
24
25    pub fn directly(&self) -> &TypeDefine {
26        if let Type::Directly(ty) = self {
27            &ty.0
28        } else {
29            panic!()
30        }
31    }
32}
33
34impl std::fmt::Display for Type {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        use std::fmt::Display;
37        match self {
38            Type::Overload(ol) => Display::fmt(ol, f),
39            Type::Directly(ty) => Display::fmt(&ty.0, f),
40        }
41    }
42}
43
44impl Types for Type {
45    fn get_type(&self) -> &TypeDefine {
46        match self {
47            Type::Overload(item) => item.get_type(),
48            Type::Directly(item) => item.get_type(),
49        }
50    }
51}
52
53impl<T: Into<TypeDefine>> From<T> for Type {
54    fn from(value: T) -> Self {
55        Self::Directly(Directly(Rc::new(value.into())))
56    }
57}
58
59pub trait Types: Any + Sized {
60    fn get_type(&self) -> &TypeDefine;
61}
62
63pub type Overload = Rc<FnSignWithName>;
64impl Types for Overload {
65    fn get_type(&self) -> &TypeDefine {
66        &self.ty
67    }
68}
69
70/// directly represent to a val's type
71#[derive(Debug, Clone)]
72pub struct Directly(pub Rc<TypeDefine>);
73impl Types for Directly {
74    fn get_type(&self) -> &TypeDefine {
75        &self.0
76    }
77}