From c895b582207bd922392fe1bd45e0172eb7ef417e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Feb 2025 21:51:33 +0000 Subject: [PATCH] chore: simplify get_or_insert --- actix-http/src/extensions.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/actix-http/src/extensions.rs b/actix-http/src/extensions.rs index a7736572c..9c85caf37 100644 --- a/actix-http/src/extensions.rs +++ b/actix-http/src/extensions.rs @@ -119,11 +119,7 @@ impl Extensions { /// assert_eq!(map.get::>(), Some(&vec![1,2])); /// ``` pub fn get_or_insert(&mut self, value: T) -> &mut T { - self.map - .entry(TypeId::of::()) - .or_insert(Box::new(value)) - .downcast_mut() - .expect("extensions map to always contain value T") + self.get_or_insert_with(|| value) } /// Inserts a value computed from `f` into the extensions if the given `value` is not present, @@ -145,7 +141,7 @@ impl Extensions { .entry(TypeId::of::()) .or_insert_with(|| Box::new(default())) .downcast_mut() - .expect("extensions map to always contain value T") + .expect("extensions map should now contain a T value") } /// Remove an item from the map of a given type.