Skip to main content

melodium_common/descriptor/
generic.rs

1use super::DataTrait;
2use core::fmt::{Debug, Display};
3use downcast_rs::{impl_downcast, DowncastSync};
4
5#[derive(Clone, Hash, Debug)]
6pub struct Generic {
7    pub name: String,
8    pub traits: Vec<DataTrait>,
9}
10
11impl PartialEq for Generic {
12    fn eq(&self, other: &Self) -> bool {
13        self.name == other.name
14            && self.traits.len() == other.traits.len()
15            && !self.traits.iter().any(|tr| !other.traits.contains(tr))
16    }
17}
18
19impl Display for Generic {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        if self.traits.is_empty() {
22            write!(f, "{}", self.name)
23        } else {
24            write!(
25                f,
26                "{}: {}",
27                self.name,
28                self.traits
29                    .iter()
30                    .map(|tr| tr.to_string())
31                    .collect::<Vec<_>>()
32                    .join(" + ")
33            )
34        }
35    }
36}
37
38impl Generic {
39    pub fn new(name: String, traits: Vec<DataTrait>) -> Self {
40        Self { name, traits }
41    }
42}
43
44pub trait Generics: DowncastSync + Display + Debug + Send + Sync {
45    fn generics(&self) -> &Vec<Generic>;
46}
47impl_downcast!(sync Generics);