From cbe80886ab1d4008213d2f4cbfd09fd657cb1d3c Mon Sep 17 00:00:00 2001 From: Adam Chalmers <adam.s.chalmers@gmail.com> Date: Mon, 13 Nov 2023 11:21:56 -0600 Subject: [PATCH] Impl Bake for BTreeMap --- utils/databake/src/alloc.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/utils/databake/src/alloc.rs b/utils/databake/src/alloc.rs index 95d615cbe12..694ccbcd147 100644 --- a/utils/databake/src/alloc.rs +++ b/utils/databake/src/alloc.rs @@ -79,6 +79,33 @@ fn btree_set() { ); } +impl<K, V> Bake for alloc::collections::BTreeMap<K, V> +where + K: Bake, + V: Bake, +{ + fn bake(&self, ctx: &CrateEnv) -> TokenStream { + ctx.insert("alloc"); + let data = self.iter().map(|(k, v)| { + let k = k.bake(ctx); + let v = v.bake(ctx); + quote!((#k, #v)) + }); + quote! { + alloc::collections::BTreeMap::from([#(#data),*]) + } + } +} + +#[test] +fn btree_map() { + test_bake!( + alloc::collections::BTreeMap<u8, u8>, + alloc::collections::BTreeMap::from([(1u8, 2u8), (2u8, 4u8)]), + alloc + ); +} + impl Bake for String { fn bake(&self, _: &CrateEnv) -> TokenStream { quote! {