From 2bb117587394208b0b3d06f035565671b520ac63 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Thu, 21 Nov 2024 22:08:57 +0300 Subject: [PATCH] tabled/ Add From HashMap impl for Builder --- tabled/src/builder/table_builder.rs | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tabled/src/builder/table_builder.rs b/tabled/src/builder/table_builder.rs index 24d02492..ff922c37 100644 --- a/tabled/src/builder/table_builder.rs +++ b/tabled/src/builder/table_builder.rs @@ -371,6 +371,64 @@ impl From for Vec>> { } } +impl From> for Builder +where + K: ToString, + V: ToString, +{ + fn from(m: std::collections::HashMap) -> Self { + let mut b = Self::with_capacity(m.len(), 2); + for (k, v) in m { + b.push_record([k.to_string(), v.to_string()]); + } + + b + } +} + +impl From> for Builder +where + K: ToString, + V: ToString, +{ + fn from(m: std::collections::BTreeMap) -> Self { + let mut b = Self::with_capacity(m.len(), 2); + for (k, v) in m { + b.push_record([k.to_string(), v.to_string()]); + } + + b + } +} + +impl From> for Builder +where + V: ToString, +{ + fn from(m: std::collections::HashSet) -> Self { + let mut b = Self::with_capacity(m.len(), 1); + for v in m { + b.push_record([v.to_string()]); + } + + b + } +} + +impl From> for Builder +where + V: ToString, +{ + fn from(m: std::collections::BTreeSet) -> Self { + let mut b = Self::with_capacity(m.len(), 1); + for v in m { + b.push_record([v.to_string()]); + } + + b + } +} + impl FromIterator for Builder where R: IntoIterator,