Skip to content

Commit

Permalink
feat: Impl ToStatic for more standard collection types
Browse files Browse the repository at this point in the history
Namely: Vec, VecDeque, HashSet, HashMap
  • Loading branch information
fasterthanlime committed Aug 16, 2024
1 parent 0d3de92 commit 9c46266
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions merde_json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,50 @@ impl<T: ToStatic> ToStatic for Option<T> {
}
}

impl<T: ToStatic> ToStatic for Vec<T> {
type Output = Vec<T::Output>;

fn to_static(&self) -> Self::Output {
self.iter().map(|v| v.to_static()).collect()
}
}

impl<K, V> ToStatic for HashMap<K, V>
where
K: ToStatic + Eq + Hash,
V: ToStatic,
K::Output: Eq + Hash,
{
type Output = HashMap<K::Output, V::Output>;

fn to_static(&self) -> Self::Output {
self.iter()
.map(|(k, v)| (k.to_static(), v.to_static()))
.collect()
}
}

use std::collections::{HashSet, VecDeque};

impl<T: ToStatic> ToStatic for HashSet<T>
where
T::Output: Eq + Hash,
{
type Output = HashSet<T::Output>;

fn to_static(&self) -> Self::Output {
self.iter().map(|v| v.to_static()).collect()
}
}

impl<T: ToStatic> ToStatic for VecDeque<T> {
type Output = VecDeque<T::Output>;

fn to_static(&self) -> Self::Output {
self.iter().map(|v| v.to_static()).collect()
}
}

/// Extension trait to provide `to_rust_value` on `JsonValue<'_>`
///
/// Which allows you to do something like:
Expand Down

0 comments on commit 9c46266

Please sign in to comment.