Skip to main content

v8/
property_filter.rs

1#[repr(C)]
2#[derive(Debug, Eq, PartialEq, Clone, Copy)]
3pub struct PropertyFilter(u32);
4
5impl PropertyFilter {
6  pub const ALL_PROPERTIES: PropertyFilter = PropertyFilter(0);
7
8  pub const ONLY_WRITABLE: PropertyFilter = PropertyFilter(1 << 0);
9
10  pub const ONLY_ENUMERABLE: PropertyFilter = PropertyFilter(1 << 1);
11
12  pub const ONLY_CONFIGURABLE: PropertyFilter = PropertyFilter(1 << 2);
13
14  pub const SKIP_STRINGS: PropertyFilter = PropertyFilter(1 << 3);
15
16  pub const SKIP_SYMBOLS: PropertyFilter = PropertyFilter(1 << 4);
17
18  /// Test if all property filters are set.
19  #[inline(always)]
20  pub fn is_all_properties(&self) -> bool {
21    *self == Self::ALL_PROPERTIES
22  }
23
24  /// Test if the only-writable property filter is set.
25  #[inline(always)]
26  pub fn is_only_writable(&self) -> bool {
27    self.has(Self::ONLY_WRITABLE)
28  }
29
30  /// Test if the only-enumerable property filter is set.
31  #[inline(always)]
32  pub fn is_only_enumerable(&self) -> bool {
33    self.has(Self::ONLY_ENUMERABLE)
34  }
35
36  /// Test if the only-configurable property filter is set.
37  #[inline(always)]
38  pub fn is_only_configurable(&self) -> bool {
39    self.has(Self::ONLY_CONFIGURABLE)
40  }
41
42  /// Test if the skip-strings property filter is set.
43  #[inline(always)]
44  pub fn is_skip_strings(&self) -> bool {
45    self.has(Self::SKIP_STRINGS)
46  }
47
48  /// Test if the skip-symbols property filter is set.
49  #[inline(always)]
50  pub fn is_skip_symbols(&self) -> bool {
51    self.has(Self::SKIP_SYMBOLS)
52  }
53
54  #[inline(always)]
55  fn has(&self, that: Self) -> bool {
56    let Self(lhs) = self;
57    let Self(rhs) = that;
58    0 != lhs & rhs
59  }
60}
61
62// Identical to #[derive(Default)] but arguably clearer when made explicit.
63impl Default for PropertyFilter {
64  fn default() -> Self {
65    Self::ALL_PROPERTIES
66  }
67}
68
69impl std::ops::BitOr for PropertyFilter {
70  type Output = Self;
71
72  fn bitor(self, Self(rhs): Self) -> Self {
73    let Self(lhs) = self;
74    Self(lhs | rhs)
75  }
76}
77
78#[test]
79fn test_attr() {
80  assert!(PropertyFilter::ALL_PROPERTIES.is_all_properties());
81  assert!(!PropertyFilter::ALL_PROPERTIES.is_only_writable());
82  assert!(!PropertyFilter::ALL_PROPERTIES.is_only_enumerable());
83  assert!(!PropertyFilter::ALL_PROPERTIES.is_only_configurable());
84  assert!(!PropertyFilter::ALL_PROPERTIES.is_skip_strings());
85  assert!(!PropertyFilter::ALL_PROPERTIES.is_skip_symbols());
86
87  assert!(!PropertyFilter::ONLY_WRITABLE.is_all_properties());
88  assert!(PropertyFilter::ONLY_WRITABLE.is_only_writable());
89  assert!(!PropertyFilter::ONLY_WRITABLE.is_only_enumerable());
90  assert!(!PropertyFilter::ONLY_WRITABLE.is_only_configurable());
91  assert!(!PropertyFilter::ONLY_WRITABLE.is_skip_strings());
92  assert!(!PropertyFilter::ONLY_WRITABLE.is_skip_symbols());
93
94  assert!(!PropertyFilter::ONLY_ENUMERABLE.is_all_properties());
95  assert!(!PropertyFilter::ONLY_ENUMERABLE.is_only_writable());
96  assert!(PropertyFilter::ONLY_ENUMERABLE.is_only_enumerable());
97  assert!(!PropertyFilter::ONLY_ENUMERABLE.is_only_configurable());
98  assert!(!PropertyFilter::ONLY_ENUMERABLE.is_skip_strings());
99  assert!(!PropertyFilter::ONLY_ENUMERABLE.is_skip_symbols());
100
101  assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_all_properties());
102  assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_only_writable());
103  assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_only_enumerable());
104  assert!(PropertyFilter::ONLY_CONFIGURABLE.is_only_configurable());
105  assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_skip_strings());
106  assert!(!PropertyFilter::ONLY_CONFIGURABLE.is_skip_symbols());
107
108  assert!(!PropertyFilter::SKIP_STRINGS.is_all_properties());
109  assert!(!PropertyFilter::SKIP_STRINGS.is_only_writable());
110  assert!(!PropertyFilter::SKIP_STRINGS.is_only_enumerable());
111  assert!(!PropertyFilter::SKIP_STRINGS.is_only_configurable());
112  assert!(PropertyFilter::SKIP_STRINGS.is_skip_strings());
113  assert!(!PropertyFilter::SKIP_STRINGS.is_skip_symbols());
114
115  assert!(!PropertyFilter::SKIP_SYMBOLS.is_all_properties());
116  assert!(!PropertyFilter::SKIP_SYMBOLS.is_only_writable());
117  assert!(!PropertyFilter::SKIP_SYMBOLS.is_only_enumerable());
118  assert!(!PropertyFilter::SKIP_SYMBOLS.is_only_configurable());
119  assert!(!PropertyFilter::SKIP_SYMBOLS.is_skip_strings());
120  assert!(PropertyFilter::SKIP_SYMBOLS.is_skip_symbols());
121
122  assert_eq!(PropertyFilter::ALL_PROPERTIES, Default::default());
123  assert_eq!(
124    PropertyFilter::ONLY_WRITABLE,
125    PropertyFilter::ALL_PROPERTIES | PropertyFilter::ONLY_WRITABLE
126  );
127
128  let attr = PropertyFilter::ONLY_WRITABLE
129    | PropertyFilter::ONLY_WRITABLE
130    | PropertyFilter::SKIP_STRINGS;
131  assert!(!attr.is_all_properties());
132  assert!(attr.is_only_writable());
133  assert!(!attr.is_only_enumerable());
134  assert!(!attr.is_only_configurable());
135  assert!(attr.is_skip_strings());
136  assert!(!attr.is_skip_symbols());
137}