1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

inline unsafe

This commit is contained in:
Rob Ede 2021-12-05 04:49:52 +00:00
parent a86c831b89
commit 01885f9954
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933

View File

@ -195,12 +195,6 @@ trait UncheckedAnyExt {
impl UncheckedAnyExt for dyn CloneAny {} impl UncheckedAnyExt for dyn CloneAny {}
fn downcast_cloneable<T: 'static>(boxed: Box<dyn CloneAny>) -> T {
// Safety:
// Box is owned and `T` is known to be true type from map containing TypeId as key.
*unsafe { UncheckedAnyExt::downcast_unchecked::<T>(boxed) }
}
/// A type map for `on_connect` extensions. /// A type map for `on_connect` extensions.
/// ///
/// All entries into this map must be owned types and implement `Clone` trait. /// All entries into this map must be owned types and implement `Clone` trait.
@ -221,6 +215,7 @@ impl CloneableExtensions {
/// ///
/// If an item of this type was already stored, it will be replaced and returned. /// If an item of this type was already stored, it will be replaced and returned.
/// ///
/// # Examples
/// ``` /// ```
/// # use actix_http::Extensions; /// # use actix_http::Extensions;
/// let mut map = Extensions::new(); /// let mut map = Extensions::new();
@ -232,7 +227,11 @@ impl CloneableExtensions {
pub fn insert<T: CloneAny>(&mut self, val: T) -> Option<T> { pub fn insert<T: CloneAny>(&mut self, val: T) -> Option<T> {
self.map self.map
.insert(TypeId::of::<T>(), Box::new(val)) .insert(TypeId::of::<T>(), Box::new(val))
.and_then(downcast_cloneable) .map(|boxed| {
// Safety:
// Box is owned and `T` is known to be true type from map.
*unsafe { UncheckedAnyExt::downcast_unchecked::<T>(boxed) }
})
} }
#[cfg(test)] #[cfg(test)]