Skip to content

Commit

Permalink
Add Box::downcast() for dyn Any + Send + Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
sdroege committed Jan 12, 2021
1 parent b6b4616 commit 12014d2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,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 {
Expand Down

0 comments on commit 12014d2

Please sign in to comment.