Here's my tuppence, picking up the sadly closed part I:
Introducing new global macros like map! and set! being a breaking change, edition 2024 would be a good moment.
While I don't mind a Perl-style mapping with =>, it's a pity that it's different from the Debug output with a JSON-style :. I wonder what would break if that were allowed after an :expr.
I'm trying to make the macros generic, with the most common variant being the default, where the context doesn't hint at a specific collection.
Since I can't push an alternate From with a default for T under the different map types, I was thinking about something like the following. I seem to be stuck on the fact that I don't know how to express that T must be of the form T<K, V>. As the Rust maps don't share a trait that has these generics, it would have to be done here.
struct MapFrom<K, V, const N: usize, T = std::collections::HashMap<K, V>>(
PhantomData<(K, V, T)>
);
impl<K, V, const N: usize, T> MapFrom<K, V, N, T>
where
K: Eq + std::hash::Hash,
{
fn from(arr: &[(K, V); N]) -> T {
T::from(arr)
}
}
macro_rules! map {
($($k:expr => $v:expr),+ $(,)?) => {{
MapFrom::from([$(($k, $v)),+])
}}
}
Anybody have a clue how to make this compile and work?
If so set! could also be generic over all set kinds, defaulting to HashSet. Likewise vec! should then be enlarged to queues and lists, defaulting to Vec.