cranelift_assembler_x64_meta/dsl/
features.rs1use core::fmt;
4use std::ops::BitOr;
5
6#[derive(PartialEq)]
24pub struct Features(Vec<Feature>);
25
26impl Features {
27 #[must_use]
28 pub fn is_empty(&self) -> bool {
29 self.0.is_empty()
30 }
31
32 pub fn iter(&self) -> impl Iterator<Item = &Feature> {
33 self.0.iter()
34 }
35}
36
37impl fmt::Display for Features {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 write!(
40 f,
41 "{}",
42 self.0
43 .iter()
44 .map(ToString::to_string)
45 .collect::<Vec<_>>()
46 .join(" | ")
47 )
48 }
49}
50
51#[derive(Clone, Copy, Debug, PartialEq)]
61#[allow(non_camel_case_types, reason = "makes DSL definitions easier to read")]
62pub enum Feature {
63 _64b,
64 compat,
65 sse,
66 sse2,
67 ssse3,
68}
69
70pub const ALL_FEATURES: &[Feature] = &[
78 Feature::_64b,
79 Feature::compat,
80 Feature::sse,
81 Feature::sse2,
82 Feature::ssse3,
83];
84
85impl fmt::Display for Feature {
86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87 match self {
88 Feature::_64b => write!(f, "_64b"),
89 Feature::compat => write!(f, "compat"),
90 Feature::sse => write!(f, "sse"),
91 Feature::sse2 => write!(f, "sse2"),
92 Feature::ssse3 => write!(f, "ssse3"),
93 }
94 }
95}
96
97impl From<Feature> for Features {
98 fn from(flag: Feature) -> Self {
99 Features(vec![flag])
100 }
101}
102
103impl From<Option<Feature>> for Features {
104 fn from(flag: Option<Feature>) -> Self {
105 Features(flag.into_iter().collect())
106 }
107}
108
109impl BitOr for Feature {
110 type Output = Features;
111 fn bitor(self, rhs: Self) -> Self::Output {
112 assert_ne!(self, rhs, "duplicate feature: {self:?}");
113 Features(vec![self, rhs])
114 }
115}
116
117impl BitOr<Feature> for Features {
118 type Output = Features;
119 fn bitor(mut self, rhs: Feature) -> Self::Output {
120 assert!(!self.0.contains(&rhs), "duplicate feature: {rhs:?}");
121 self.0.push(rhs);
122 self
123 }
124}