From c214c1c82cbf0eb5685e1e31041b5f2dd063e548 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 2 Oct 2020 19:00:30 +0200 Subject: [PATCH] Update return type of Extensions::insert to match documented behavior This is a breaking change, but Extensions::insert was strictly less useful with the previous behavior. --- actix-http/CHANGES.md | 3 +++ actix-http/src/extensions.rs | 6 ++++-- actix-http/src/helpers.rs | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 6a98c4ca7..facbaa470 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,9 @@ ## Unreleased - 2020-xx-xx +### Changed + +* Change the return type of `Extensions::insert` from `()` to `Option` ## 2.0.0 - 2020-09-11 * No significant changes from `2.0.0-beta.4`. diff --git a/actix-http/src/extensions.rs b/actix-http/src/extensions.rs index 96e01767b..0e86b896e 100644 --- a/actix-http/src/extensions.rs +++ b/actix-http/src/extensions.rs @@ -24,8 +24,10 @@ impl Extensions { /// /// If a extension of this type already existed, it will /// be returned. - pub fn insert(&mut self, val: T) { - self.map.insert(TypeId::of::(), Box::new(val)); + pub fn insert(&mut self, val: T) -> Option { + self.map + .insert(TypeId::of::(), Box::new(val)) + .and_then(|boxed| boxed.downcast().ok().map(|boxed| *boxed)) } /// Check if container contains entry diff --git a/actix-http/src/helpers.rs b/actix-http/src/helpers.rs index bbf358b66..7ad272182 100644 --- a/actix-http/src/helpers.rs +++ b/actix-http/src/helpers.rs @@ -63,7 +63,7 @@ pub(crate) struct Data(pub(crate) T); impl DataFactory for Data { fn set(&self, ext: &mut Extensions) { - ext.insert(self.0.clone()) + ext.insert(self.0.clone()); } }