allocative/impls/std/
collections.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under both the MIT license found in the
5 * LICENSE-MIT file in the root directory of this source tree and the Apache
6 * License, Version 2.0 found in the LICENSE-APACHE file in the root directory
7 * of this source tree.
8 */
9
10use std::collections::BTreeMap;
11use std::collections::BTreeSet;
12use std::collections::HashMap;
13use std::collections::HashSet;
14
15use crate::allocative_trait::Allocative;
16use crate::visitor::Visitor;
17
18impl<K: Allocative, V: Allocative> Allocative for BTreeMap<K, V> {
19    fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
20        let mut visitor = visitor.enter_self_sized::<Self>();
21        visitor.visit_generic_map_fields(self);
22        visitor.exit();
23    }
24}
25
26impl<K: Allocative> Allocative for BTreeSet<K> {
27    fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
28        let mut visitor = visitor.enter_self_sized::<Self>();
29        visitor.visit_generic_set_fields(self);
30        visitor.exit();
31    }
32}
33
34impl<K: Allocative, V: Allocative, S> Allocative for HashMap<K, V, S> {
35    fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
36        let mut visitor = visitor.enter_self_sized::<Self>();
37        // TODO: can do better extra capacity.
38        visitor.visit_generic_map_fields(self);
39        visitor.exit();
40    }
41}
42
43impl<K: Allocative, S> Allocative for HashSet<K, S> {
44    fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
45        let mut visitor = visitor.enter_self_sized::<Self>();
46        // TODO: can do better extra capacity.
47        visitor.visit_generic_set_fields(self);
48        visitor.exit();
49    }
50}