runtime_format/
alloc_impls.rs1use core::{
2 borrow::Borrow,
3 fmt,
4 hash::{BuildHasher, Hash},
5};
6use std::collections::{BTreeMap, HashMap};
7
8use crate::{FormatKey, FormatKeyError};
9
10impl<K, V, S> FormatKey for HashMap<K, V, S>
11where
12 K: Borrow<str> + Eq + Hash,
13 V: fmt::Display,
14 S: BuildHasher,
15{
16 fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
17 match self.get(key) {
18 Some(v) => v.fmt(f).map_err(FormatKeyError::Fmt),
19 None => Err(FormatKeyError::UnknownKey),
20 }
21 }
22}
23
24impl<K, V> FormatKey for BTreeMap<K, V>
25where
26 K: Borrow<str> + Ord,
27 V: fmt::Display,
28{
29 fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
30 match self.get(key) {
31 Some(v) => v.fmt(f).map_err(FormatKeyError::Fmt),
32 None => Err(FormatKeyError::UnknownKey),
33 }
34 }
35}