serde/test_suite/tests/macros/mod.rs

80 lines
1.7 KiB
Rust
Raw Normal View History

#![allow(unused_macro_rules)]
use serde_test::Token;
use std::iter;
2015-08-27 22:01:09 -05:00
macro_rules! btreeset {
() => {
BTreeSet::new()
};
2022-01-20 21:30:34 -06:00
($($value:expr),+) => {{
let mut set = BTreeSet::new();
$(set.insert($value);)+
set
}};
2015-08-27 22:01:09 -05:00
}
macro_rules! btreemap {
() => {
BTreeMap::new()
};
2022-01-20 21:30:34 -06:00
($($key:expr => $value:expr),+) => {{
let mut map = BTreeMap::new();
$(map.insert($key, $value);)+
map
}};
2015-08-27 22:01:09 -05:00
}
macro_rules! hashset {
() => {
HashSet::new()
};
2022-01-20 21:30:34 -06:00
($($value:expr),+) => {{
let mut set = HashSet::new();
$(set.insert($value);)+
set
}};
($hasher:ident @ $($value:expr),+) => {{
use std::hash::BuildHasherDefault;
let mut set = HashSet::with_hasher(BuildHasherDefault::<$hasher>::default());
$(set.insert($value);)+
set
}};
2015-08-27 22:01:09 -05:00
}
macro_rules! hashmap {
() => {
HashMap::new()
};
2022-01-20 21:30:34 -06:00
($($key:expr => $value:expr),+) => {{
let mut map = HashMap::new();
$(map.insert($key, $value);)+
map
}};
($hasher:ident @ $($key:expr => $value:expr),+) => {{
use std::hash::BuildHasherDefault;
let mut map = HashMap::with_hasher(BuildHasherDefault::<$hasher>::default());
$(map.insert($key, $value);)+
map
}};
2015-08-27 22:01:09 -05:00
}
pub trait SingleTokenIntoIterator {
fn into_iter(self) -> iter::Once<Token>;
}
impl SingleTokenIntoIterator for Token {
fn into_iter(self) -> iter::Once<Token> {
iter::once(self)
}
}
2022-01-20 21:25:06 -06:00
macro_rules! seq {
($($elem:expr),* $(,)?) => {{
use crate::macros::SingleTokenIntoIterator;
let mut vec = Vec::new();
$(<Vec<Token> as Extend<Token>>::extend(&mut vec, $elem.into_iter());)*
vec
}};
}