Skip to main content

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 ident = $input.expect_ident()?;
31        ::cssparser::match_ignore_ascii_case! { &ident,
32            $( $match_body )*
33            _ => return Err(ParseError::custom(
34                ::selectors::parser::SelectorParseErrorKind::UnexpectedIdent
35            ))
36        }
37    }}
38}
39
40#[cfg(feature = "servo")]
41macro_rules! local_name {
42    ($s:tt) => {
43        $crate::values::GenericAtomIdent(web_atoms::local_name!($s))
44    };
45}
46
47#[cfg(feature = "servo")]
48macro_rules! ns {
49    () => {
50        $crate::values::GenericAtomIdent(web_atoms::ns!())
51    };
52    ($s:tt) => {
53        $crate::values::GenericAtomIdent(web_atoms::ns!($s))
54    };
55}
56
57#[cfg(feature = "gecko")]
58macro_rules! local_name {
59    ($s:tt) => {
60        $crate::values::AtomIdent(atom!($s))
61    };
62}
63
64/// Asserts the size of a type at compile time.
65macro_rules! size_of_test {
66    ($t: ty, $expected_size: expr) => {
67        #[cfg(target_pointer_width = "64")]
68        const_assert_eq!(std::mem::size_of::<$t>(), $expected_size);
69    };
70}