style/
macros.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
5//! Various macro helpers.
6
7macro_rules! trivial_to_computed_value {
8    ($name:ty) => {
9        impl $crate::values::computed::ToComputedValue for $name {
10            type ComputedValue = $name;
11
12            fn to_computed_value(&self, _: &$crate::values::computed::Context) -> Self {
13                self.clone()
14            }
15
16            fn from_computed_value(other: &Self) -> Self {
17                other.clone()
18            }
19        }
20    };
21}
22
23/// A macro to parse an identifier, or return an `UnexpectedIdent` error
24/// otherwise.
25///
26/// FIXME(emilio): The fact that `UnexpectedIdent` is a `SelectorParseError`
27/// doesn't make a lot of sense to me.
28macro_rules! try_match_ident_ignore_ascii_case {
29    ($input:expr, $( $match_body:tt )*) => {{
30        let location = $input.current_source_location();
31        let ident = $input.expect_ident()?;
32        match_ignore_ascii_case! { &ident,
33            $( $match_body )*
34            _ => return Err(location.new_custom_error(
35                ::selectors::parser::SelectorParseErrorKind::UnexpectedIdent(ident.clone())
36            ))
37        }
38    }}
39}
40
41#[cfg(feature = "servo")]
42macro_rules! local_name {
43    ($s:tt) => {
44        $crate::values::GenericAtomIdent(web_atoms::local_name!($s))
45    };
46}
47
48#[cfg(feature = "servo")]
49macro_rules! ns {
50    () => {
51        $crate::values::GenericAtomIdent(web_atoms::ns!())
52    };
53    ($s:tt) => {
54        $crate::values::GenericAtomIdent(web_atoms::ns!($s))
55    };
56}
57
58#[cfg(feature = "gecko")]
59macro_rules! local_name {
60    ($s:tt) => {
61        $crate::values::AtomIdent(atom!($s))
62    };
63}
64
65/// Asserts the size of a type at compile time.
66macro_rules! size_of_test {
67    ($t: ty, $expected_size: expr) => {
68        #[cfg(target_pointer_width = "64")]
69        const_assert_eq!(std::mem::size_of::<$t>(), $expected_size);
70    };
71}