Crate as_any

source ·
Expand description

This library provides some utility traits to make working with Any smoother. This crate contains similiar functionality to the downcast crate, but simpler, e.g. it isn’t necessary to call some macro to instantiate the downcast methods.

Usage example

use as_any::{AsAny, Downcast};

struct Test;

trait Custom: AsAny {
    // whatever you like to put inside of your trait
}

impl Custom for Test {}

fn lol() {
    let x = Test;
    let y: &dyn Custom = &x;
    // With (extension) trait `Downcast` in scope.
    y.downcast_ref::<Test>().unwrap();
}

Traits

  • This trait is an extension trait to Any, and adds methods to retrieve a &dyn Any
  • This is a shim around AaAny to avoid some boilerplate code. It is a separate trait because it is also implemented on runtime polymorphic traits (which are !Sized).