ligen_ir/types/
generics.rs

1use crate::Type;
2use crate::prelude::*;
3
4/// Generic arguments list.
5#[derive(Default, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
6pub struct Generics {
7    /// Generic types.
8    pub types: Vec<Type>
9}
10
11impl<T: Into<Type>> From<Vec<T>> for Generics {
12    fn from(value: Vec<T>) -> Self {
13        let types = value
14            .into_iter()
15            .map(|type_| type_.into())
16            .collect();
17        Self { types }
18    }
19}
20
21impl From<Type> for Generics {
22    fn from(value: Type) -> Self {
23        let types = vec![value];
24        Self { types }
25    }
26}
27
28impl std::fmt::Display for Generics {
29    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30        if self.types.is_empty() {
31            f.write_str("")
32        } else {
33            let generics = self
34                .types
35                .iter()
36                .map(|generic| format!("{}", generic))
37                .collect::<Vec<String>>()
38                .join(", ");
39            f.write_str(&format!("<{}>", generics))
40        }
41    }
42}
43
44// FIXME: Remove this.
45// impl IntoIterTypeMut<Type> for Generics {
46//     fn type_iterator(&mut self) -> TypeIterMut<'_, Type> {
47//         self.types.iter_mut().collect::<Vec<_>>().into()
48//     }
49// }