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::selector_map::{MaybeCaseInsensitiveHashMap, PrecomputedHashMap};
6use crate::{Atom, LocalName, ShrinkIfNeeded};
7
8/// A map for filtering by easily-discernable features in a selector.
9#[derive(Clone, Debug, MallocSizeOf)]
10pub struct SimpleBucketsMap<T> {
11    pub classes: MaybeCaseInsensitiveHashMap<Atom, T>,
12    pub ids: MaybeCaseInsensitiveHashMap<Atom, T>,
13    pub local_names: PrecomputedHashMap<LocalName, T>,
14}
15
16impl<T> Default for SimpleBucketsMap<T> {
17    fn default() -> Self {
18        // TODO(dshin): This is a bit annoying - even if these maps would be empty,
19        // deriving this trait requires `T: Default`
20        // This is a known issue - See https://github.com/rust-lang/rust/issues/26925.
21        Self {
22            classes: Default::default(),
23            ids: Default::default(),
24            local_names: Default::default(),
25        }
26    }
27}
28
29impl<T> SimpleBucketsMap<T> {
30    /// Clears the map.
31    #[inline(always)]
32    pub fn clear(&mut self) {
33        self.classes.clear();
34        self.ids.clear();
35        self.local_names.clear();
36    }
37
38    /// Shrink the capacity of the map if needed.
39    #[inline(always)]
40    pub fn shrink_if_needed(&mut self) {
41        self.classes.shrink_if_needed();
42        self.ids.shrink_if_needed();
43        self.local_names.shrink_if_needed();
44    }
45
46    /// Returns whether there's nothing in the map.
47    #[inline(always)]
48    pub fn is_empty(&self) -> bool {
49        self.classes.is_empty() && self.ids.is_empty() && self.local_names.is_empty()
50    }
51}