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:
commit
caf2c0652a
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue