shapely_core/
hashmap_impl.rs

1use std::{alloc::Layout, collections::HashMap, fmt};
2
3use crate::{mini_typeid, Innards, Shape, Shapely};
4
5impl<V> Shapely for HashMap<String, V>
6where
7    V: Shapely,
8{
9    fn shape() -> Shape {
10        // This name function doesn't need the type parameter
11        fn name<V: Shapely>(f: &mut fmt::Formatter) -> fmt::Result {
12            write!(f, "HashMap<String, ")?;
13            let shape = V::shape();
14            (shape.name)(f)?;
15            write!(f, ">")
16        }
17
18        Shape {
19            name: name::<V> as _,
20            typeid: mini_typeid::of::<Self>(),
21            layout: Layout::new::<HashMap<String, V>>(),
22            innards: Innards::HashMap {
23                value_shape: V::shape_desc(),
24            },
25            set_to_default: Some(|addr: *mut u8| unsafe {
26                *(addr as *mut HashMap<String, V>) = HashMap::new();
27            }),
28            drop_in_place: Some(|addr: *mut u8| unsafe {
29                std::ptr::drop_in_place(addr as *mut HashMap<String, V>);
30            }),
31        }
32    }
33}