From 4a4f7ac31b81fdb1c9b2fe691cf00d5bae752d14 Mon Sep 17 00:00:00 2001 From: David Thomas Date: Wed, 27 Mar 2024 12:21:09 +0000 Subject: [PATCH] [codegen] Add Map::clear_entries --- phf_codegen/src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/phf_codegen/src/lib.rs b/phf_codegen/src/lib.rs index 6673c708..293b9d97 100644 --- a/phf_codegen/src/lib.rs +++ b/phf_codegen/src/lib.rs @@ -198,6 +198,17 @@ impl Map { self } + /// Clears the entries currently in the builder. + /// + /// Using a single builder and clearing it after calls to [`Self::build`] is more efficent + /// than creating a new builder in a loop, as this can reuse heap allocations similar to [`Vec::clear`]. + pub fn clear_entries(&mut self) -> &mut Map { + self.keys.clear(); + self.values.clear(); + + self + } + /// Calculate the hash parameters and return a struct implementing /// [`Display`](::std::fmt::Display) which will print the constructed `phf::Map`. /// @@ -302,6 +313,15 @@ impl Set { self } + /// Clears the entries currently in the builder. + /// + /// Using a single builder and clearing it after calls to [`Self::build`] is more efficent + /// than creating a new builder in a loop, as this can reuse heap allocations similar to [`Vec::clear`]. + pub fn clear_entries(&mut self) -> &mut Set { + self.map.clear_entries(); + self + } + /// Calculate the hash parameters and return a struct implementing /// [`Display`](::std::fmt::Display) which will print the constructed `phf::Set`. /// @@ -358,6 +378,17 @@ impl OrderedMap { self } + /// Clears the entries currently in the builder. + /// + /// Using a single builder and clearing it after calls to [`Self::build`] is more efficent + /// than creating a new builder in a loop, as this can reuse heap allocations similar to [`Vec::clear`]. + pub fn clear_entries(&mut self) -> &mut OrderedMap { + self.keys.clear(); + self.values.clear(); + + self + } + /// Calculate the hash parameters and return a struct implementing /// [`Display`](::std::fmt::Display) which will print the constructed /// `phf::OrderedMap`. @@ -472,6 +503,15 @@ impl OrderedSet { self } + /// Clears the entries currently in the builder. + /// + /// Using a single builder and clearing it after calls to [`Self::build`] is more efficent + /// than creating a new builder in a loop, as this can reuse heap allocations similar to [`Vec::clear`]. + pub fn clear_entries(&mut self) -> &mut OrderedSet { + self.map.clear_entries(); + self + } + /// Calculate the hash parameters and return a struct implementing /// [`Display`](::std::fmt::Display) which will print the constructed /// `phf::OrderedSet`.