# Pre-RFC: make downcast user-friendly

**URL:** <https://internals.rust-lang.org/t/pre-rfc-make-downcast-user-friendly/20092>\
**Category:** libs\
**Created:** [December 28, 2023, 12:03pm UTC](https://internals.rust-lang.org/t/pre-rfc-make-downcast-user-friendly/20092 "2023-12-28T12:03:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhardy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhardy/32/2399_2.png) [@dhardy](https://internals.rust-lang.org/u/dhardy)\
**Post date:** [December 28, 2023, 12:03pm UTC](https://internals.rust-lang.org/t/pre-rfc-make-downcast-user-friendly/20092/1 "2023-12-28T12:03:00Z")

</div>

[Trait-object upcast](https://github.com/rust-lang/rust/issues/65991) was recently merged. Unfortunately downcasting is still not very user-friendly.

# Motivation

Given

```rust
use std::{any::Any, ops::Deref};

trait A: Any {}
trait B: A {}

impl A for () {}
impl B for () {}

```

we can now easily upcast trait objects:

```rust
let b = Box::new(()) as Box<dyn B>;
let a = b as Box<dyn A>; // upcast

```

Type-checking and downcast is supported, but usage is less clear:

```rust
assert!((a.deref() as &dyn Any).is::<()>());
let c = (a as Box<dyn Any>).downcast::<()>();

```

We should be able to improve this to something like the following:

```rust
assert!(a.inner_type_is::<()>()); // method defined on Box
assert!(a.deref().type_is::<()>()); // alternative
// Note that we type-check the contents, not the Box!

let c = a.downcast::<()>();

```

# Implementating details

## Type-checking

[`std::any::Any`](https://doc.rust-lang.org/stable/std/any/trait.Any.html) plus [`TypeId`](https://doc.rust-lang.org/stable/std/any/struct.TypeId.html) provide all we need, but the following is not user-friendly:

```rust
assert_eq!(a.deref().type_id(), TypeId::of::<()>());

```

Currently, `fn` [`is`](https://doc.rust-lang.org/stable/std/any/trait.Any.html#method.is) is defined on `dyn Any`, `dyn Any + Send` and `dyn Any + Send + Sync`. This implies that (1) it is not directly available on `dyn A`, `dyn B` etc. and (2) it is not available on generics like `T: Any`.

Aside: `is` is too short/generic a name for a widely available method, hence I use the longer name `type_is` below.

Options for making `type_is` more accessible:

1. As an additional method on `Any`: `fn type_is<U: Any>(&self) -> bool;`

2. The same method, but on an extension trait `AnyExt: Any`.

3. A similar method placed directly on `Box` (and similar container types) which emphasizes that it tests the _inner_ type: `fn inner_type_is<U: Any>(&self) -> bool;`

## Downcast

Downcast is necessarily implemented on containing types like `Box`. Today, `Box` provides impls for three cases:

- `Box<dyn Any>`
- `Box<dyn Any + Send>`
- `Box<dyn Any + Send + Sync`

This does not cover user-defined traits like `Box<dyn B>`. What we really want is to define `downcast` on `Box<T>` where `T: Any + ?Sized`, but for obscure reasons some types like `str` and `[i32]` technically implement `Any` (unsafely due to length being omitted from the `TypeId`) while not supporting the ability to type-cast to `dyn Any`.

What we need to make the generics work is provided by [`Unsize`](https://doc.rust-lang.org/stable/std/marker/trait.Unsize.html):

```rust
pub trait BoxExt: Sized {
    /// Optional method: type-check inner value
    fn inner_type_is<U: Any>(&self) -> bool;

    /// Attempt to downcast the inner value
    fn downcast<U: Any>(self) -> Result<Box<U>, Self>;
}

impl<T: Any + Unsize<dyn Any> + ?Sized> BoxExt for Box<T> {
    fn inner_type_is<U: Any>(&self) -> bool {
        self.deref().type_is::<U>()
    }

    fn downcast<U: Any>(self) -> Result<Box<U>, Self> {
        if self.inner_type_is::<U>() {
            unsafe {
                // This cast requires T: Unsize<dyn Any> :
                let any: Box<dyn Any> = self as Box<dyn Any>;
                let raw: *mut dyn Any = Box::into_raw(any);
                Ok(Box::from_raw(raw as *mut U))
            }
        } else {
            Err(self)
        }
    }
}

```

[Full example on the playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d201f0a9cfb079d81eb4b861219dd9dc)

## Usage on non-dyn types

Since the above are generic, they work on non-`dyn` types:

```rust
    let b = Box::new(());
    assert!(b.inner_type_is::<()>());
    let b: Box<()> = b.downcast().unwrap();

```

This is fine, and has some utility in generic code.

## Introducing the above

The first potential blocker is that `Unsize` has not yet been stabilized. It is however already widely used within the `std` library so this may not be an issue.

The second issue potential issue is breaking changes. It _is_ possible to implement user-defined methods on `Box` via extension traits as demonstrated above but not inherent methods. While such conflicts cannot be ruled out, they are unlikely.

# Proposal

Implement the following on `Box`, replacing the current impls of `downcast`:

```rust
impl<T: Any + Unsize<dyn Any> + ?Sized, A: Allocator> Box<T, A> {
    /// Type-check inner value
    pub fn inner_type_is<U: Any>(&self) -> bool {
        self.deref().type_id() == TypeId::of::<U>()
    }

    /// Attempt to downcast the inner value
    pub fn downcast<U: Any>(self) -> Result<Box<U, A>, Self> {
        if self.inner_type_is::<U>() {
            unsafe {
                let raw: *mut dyn Any = Box::into_raw(self as Box<dyn Any, A>);
                Ok(Box::from_raw(raw as *mut U))
            }
        } else {
            Err(self)
        }
    }
}

```

Do the same for other container types currently providing `fn downcast` (`Error`, `Rc`, `Arc`).

The method `fn downcast` is strict superset of the existing `downcast` methods on `Box`. There is _some_ potential for collision through user-defined extension trait on `Box` (certainly worth a crater run).

The method `fn inner_type_is` is an optional extra with some applications such as passing messages via a variadic queue/stack (where only messages of known type are removed).

## Alternative: implement via extension traits

Instead of proposal 1, introduce the same methods via an extension trait `BoxExt` (respectively `ErrorExt`, `RcExt`, `ArcExt`) which requires explicit import. Keep the existing `downcast` methods (not a conflict since inherent methods are preferred over trait-provided methods).

This avoids the (low) potential for breakage through name conflicts but has worse usability: discovery, API complexity and need-to-import.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 27, 2024, 12:03pm UTC](https://internals.rust-lang.org/t/pre-rfc-make-downcast-user-friendly/20092/2 "2024-03-27T12:03:52Z")

</div>

Closed on March 27, 2024, 12:03pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
