Rollup merge of #80945 - sdroege:downcast-send-sync, r=m-ou-se

Add Box::downcast() for dyn Any + Send + Sync

Looks like a plain omission, but unfortunately I just needed that in my code :)
This commit is contained in:
Jonas Schievink 2021-01-31 01:47:27 +01:00 committed by GitHub
commit caf2c0652a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1372,6 +1372,39 @@ impl<A: Allocator> Box<dyn Any + Send, A> {
}
}
impl<A: Allocator> Box<dyn Any + Send + Sync, A> {
#[inline]
#[stable(feature = "box_send_sync_any_downcast", since = "1.51.0")]
/// Attempt to downcast the box to a concrete type.
///
/// # Examples
///
/// ```
/// use std::any::Any;
///
/// fn print_if_string(value: Box<dyn Any + Send + Sync>) {
/// if let Ok(string) = value.downcast::<String>() {
/// println!("String ({}): {}", string.len(), string);
/// }
/// }
///
/// let my_string = "Hello World".to_string();
/// print_if_string(Box::new(my_string));
/// print_if_string(Box::new(0i8));
/// ```
pub fn downcast<T: Any>(self) -> Result<Box<T, A>, Self> {
if self.is::<T>() {
unsafe {
let (raw, alloc): (*mut (dyn Any + Send + Sync), _) =
Box::into_raw_with_allocator(self);
Ok(Box::from_raw_in(raw as *mut T, alloc))
}
} else {
Err(self)
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: fmt::Display + ?Sized, A: Allocator> fmt::Display for Box<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {