Narrow an unsafe block to the minimum

This was necessary in the days of the raw module, when inserting to the
raw map was the unsafe operation rather than getting mutable access to
the raw map, to which I have changed it now.
This commit is contained in:
Chris Morgan 2022-01-26 00:57:40 +11:00
parent 0316c0faea
commit ff0fd8d0cf

View file

@ -221,10 +221,8 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
/// Otherwise, `None` is returned. /// Otherwise, `None` is returned.
#[inline] #[inline]
pub fn insert<T: IntoBox<A>>(&mut self, value: T) -> Option<T> { pub fn insert<T: IntoBox<A>>(&mut self, value: T) -> Option<T> {
unsafe { self.raw.insert(TypeId::of::<T>(), value.into_box())
self.raw.insert(TypeId::of::<T>(), value.into_box()) .map(|any| unsafe { *any.downcast_unchecked::<T>() })
.map(|any| *any.downcast_unchecked::<T>())
}
} }
// rustc 1.60.0-nightly has another method try_insert that would be nice to add when stable. // rustc 1.60.0-nightly has another method try_insert that would be nice to add when stable.