Skip to content

Commit

Permalink
tabled/ Add From HashMap impl for Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiburt committed Nov 21, 2024
1 parent e1cd73f commit 2bb1175
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tabled/src/builder/table_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,64 @@ impl From<Builder> for Vec<Vec<Text<String>>> {
}
}

impl<K, V, S> From<std::collections::HashMap<K, V, S>> for Builder
where
K: ToString,
V: ToString,
{
fn from(m: std::collections::HashMap<K, V, S>) -> 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<K, V> From<std::collections::BTreeMap<K, V>> for Builder
where
K: ToString,
V: ToString,
{
fn from(m: std::collections::BTreeMap<K, V>) -> 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<V> From<std::collections::HashSet<V>> for Builder
where
V: ToString,
{
fn from(m: std::collections::HashSet<V>) -> Self {
let mut b = Self::with_capacity(m.len(), 1);
for v in m {
b.push_record([v.to_string()]);
}

b
}
}

impl<V> From<std::collections::BTreeSet<V>> for Builder
where
V: ToString,
{
fn from(m: std::collections::BTreeSet<V>) -> Self {
let mut b = Self::with_capacity(m.len(), 1);
for v in m {
b.push_record([v.to_string()]);
}

b
}
}

impl<R> FromIterator<R> for Builder
where
R: IntoIterator,
Expand Down

0 comments on commit 2bb1175

Please sign in to comment.