Skip to content

Commit 72433e1

Browse files
committed
Add implementations for Any + Send + Sync
Implement `is`, `downcast_ref`, `downcast_mut` and `Debug` for `Any + Send + Sync`.
1 parent 5342d40 commit 72433e1

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

Diff for: src/libcore/any.rs

+90
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ impl fmt::Debug for Any + Send {
136136
}
137137
}
138138

139+
#[stable(feature = "any_send_sync_methods", since = "1.28.0")]
140+
impl fmt::Debug for Any + Send + Sync {
141+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
142+
f.pad("Any")
143+
}
144+
}
145+
139146
impl Any {
140147
/// Returns `true` if the boxed type is the same as `T`.
141148
///
@@ -325,6 +332,89 @@ impl Any+Send {
325332
}
326333
}
327334

335+
impl Any+Send+Sync {
336+
/// Forwards to the method defined on the type `Any`.
337+
///
338+
/// # Examples
339+
///
340+
/// ```
341+
/// use std::any::Any;
342+
///
343+
/// fn is_string(s: &(Any + Send + Sync)) {
344+
/// if s.is::<String>() {
345+
/// println!("It's a string!");
346+
/// } else {
347+
/// println!("Not a string...");
348+
/// }
349+
/// }
350+
///
351+
/// fn main() {
352+
/// is_string(&0);
353+
/// is_string(&"cookie monster".to_string());
354+
/// }
355+
/// ```
356+
#[stable(feature = "any_send_sync_methods", since = "1.28.0")]
357+
#[inline]
358+
pub fn is<T: Any>(&self) -> bool {
359+
Any::is::<T>(self)
360+
}
361+
362+
/// Forwards to the method defined on the type `Any`.
363+
///
364+
/// # Examples
365+
///
366+
/// ```
367+
/// use std::any::Any;
368+
///
369+
/// fn print_if_string(s: &(Any + Send)) {
370+
/// if let Some(string) = s.downcast_ref::<String>() {
371+
/// println!("It's a string({}): '{}'", string.len(), string);
372+
/// } else {
373+
/// println!("Not a string...");
374+
/// }
375+
/// }
376+
///
377+
/// fn main() {
378+
/// print_if_string(&0);
379+
/// print_if_string(&"cookie monster".to_string());
380+
/// }
381+
/// ```
382+
#[stable(feature = "any_send_sync_methods", since = "1.28.0")]
383+
#[inline]
384+
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
385+
Any::downcast_ref::<T>(self)
386+
}
387+
388+
/// Forwards to the method defined on the type `Any`.
389+
///
390+
/// # Examples
391+
///
392+
/// ```
393+
/// use std::any::Any;
394+
///
395+
/// fn modify_if_u32(s: &mut (Any+ Send)) {
396+
/// if let Some(num) = s.downcast_mut::<u32>() {
397+
/// *num = 42;
398+
/// }
399+
/// }
400+
///
401+
/// fn main() {
402+
/// let mut x = 10u32;
403+
/// let mut s = "starlord".to_string();
404+
///
405+
/// modify_if_u32(&mut x);
406+
/// modify_if_u32(&mut s);
407+
///
408+
/// assert_eq!(x, 42);
409+
/// assert_eq!(&s, "starlord");
410+
/// }
411+
/// ```
412+
#[stable(feature = "any_send_sync_methods", since = "1.28.0")]
413+
#[inline]
414+
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
415+
Any::downcast_mut::<T>(self)
416+
}
417+
}
328418

329419
///////////////////////////////////////////////////////////////////////////////
330420
// TypeID and its methods

0 commit comments

Comments
 (0)