Skip to content

Commit 0dd3140

Browse files
authored
macros: Keep the ability for ascii_case_insensitive_phf_map to reference custom types. (#354)
See rust-lang/rust#114369. use super::* wouldn't be enough.
1 parent 8ff38af commit 0dd3140

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/macros.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,36 @@ macro_rules! ascii_case_insensitive_phf_map {
9393
ascii_case_insensitive_phf_map!($name -> $ValueType = { $( $key => $value, )+ })
9494
};
9595
($name: ident -> $ValueType: ty = { $( $key: tt => $value: expr, )+ }) => {
96-
mod $name {
97-
use $crate::_cssparser_internal_phf as phf;
96+
use $crate::_cssparser_internal_phf as phf;
97+
98+
// See macro above for context.
99+
mod cssparser_internal {
98100
$crate::_cssparser_internal_max_len! {
99101
$( $key )+
100102
}
101-
static MAP: phf::Map<&'static str, $ValueType> = phf::phf_map! {
102-
$(
103-
$key => $value,
104-
)*
105-
};
103+
}
104+
105+
static MAP: phf::Map<&'static str, $ValueType> = phf::phf_map! {
106+
$(
107+
$key => $value,
108+
)*
109+
};
106110

111+
// While the obvious choice for this would be an inner module, it's not possible to
112+
// reference from types from there, see:
113+
// <https://github.com/rust-lang/rust/issues/114369>
114+
//
115+
// So we abuse a struct with static associated functions instead.
116+
#[allow(non_camel_case_types)]
117+
struct $name;
118+
impl $name {
107119
#[allow(dead_code)]
108-
pub fn entries() -> impl Iterator<Item = (&'static &'static str, &'static $ValueType)> {
120+
fn entries() -> impl Iterator<Item = (&'static &'static str, &'static $ValueType)> {
109121
MAP.entries()
110122
}
111123

112-
pub fn get(input: &str) -> Option<&'static $ValueType> {
113-
$crate::_cssparser_internal_to_lowercase!(input, MAX_LENGTH => lowercase);
124+
fn get(input: &str) -> Option<&'static $ValueType> {
125+
$crate::_cssparser_internal_to_lowercase!(input, cssparser_internal::MAX_LENGTH => lowercase);
114126
MAP.get(lowercase?)
115127
}
116128
}

0 commit comments

Comments
 (0)