style/
simple_buckets_map.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use crate::derives::*;
6use crate::selector_map::{MaybeCaseInsensitiveHashMap, PrecomputedHashMap};
7use crate::{Atom, LocalName, ShrinkIfNeeded};
8
9/// A map for filtering by easily-discernable features in a selector.
10#[derive(Clone, Debug, MallocSizeOf)]
11pub struct SimpleBucketsMap<T> {
12    pub classes: MaybeCaseInsensitiveHashMap<Atom, T>,
13    pub ids: MaybeCaseInsensitiveHashMap<Atom, T>,
14    pub local_names: PrecomputedHashMap<LocalName, T>,
15}
16
17impl<T> Default for SimpleBucketsMap<T> {
18    fn default() -> Self {
19        // TODO(dshin): This is a bit annoying - even if these maps would be empty,
20        // deriving this trait requires `T: Default`
21        // This is a known issue - See https://github.com/rust-lang/rust/issues/26925.
22        Self {
23            classes: Default::default(),
24            ids: Default::default(),
25            local_names: Default::default(),
26        }
27    }
28}
29
30impl<T> SimpleBucketsMap<T> {
31    /// Clears the map.
32    #[inline(always)]
33    pub fn clear(&mut self) {
34        self.classes.clear();
35        self.ids.clear();
36        self.local_names.clear();
37    }
38
39    /// Shrink the capacity of the map if needed.
40    #[inline(always)]
41    pub fn shrink_if_needed(&mut self) {
42        self.classes.shrink_if_needed();
43        self.ids.shrink_if_needed();
44        self.local_names.shrink_if_needed();
45    }
46
47    /// Returns whether there's nothing in the map.
48    #[inline(always)]
49    pub fn is_empty(&self) -> bool {
50        self.classes.is_empty() && self.ids.is_empty() && self.local_names.is_empty()
51    }
52}