From 9c4626684dd94cdb60667f535656589b602f77d2 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Fri, 16 Aug 2024 17:06:40 +0200 Subject: [PATCH] feat: Impl ToStatic for more standard collection types Namely: Vec, VecDeque, HashSet, HashMap --- merde_json/src/lib.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/merde_json/src/lib.rs b/merde_json/src/lib.rs index 57525a5..9576670 100644 --- a/merde_json/src/lib.rs +++ b/merde_json/src/lib.rs @@ -966,6 +966,50 @@ impl ToStatic for Option { } } +impl ToStatic for Vec { + type Output = Vec; + + fn to_static(&self) -> Self::Output { + self.iter().map(|v| v.to_static()).collect() + } +} + +impl ToStatic for HashMap +where + K: ToStatic + Eq + Hash, + V: ToStatic, + K::Output: Eq + Hash, +{ + type Output = HashMap; + + fn to_static(&self) -> Self::Output { + self.iter() + .map(|(k, v)| (k.to_static(), v.to_static())) + .collect() + } +} + +use std::collections::{HashSet, VecDeque}; + +impl ToStatic for HashSet +where + T::Output: Eq + Hash, +{ + type Output = HashSet; + + fn to_static(&self) -> Self::Output { + self.iter().map(|v| v.to_static()).collect() + } +} + +impl ToStatic for VecDeque { + type Output = VecDeque; + + 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: