as_any_min

Crates.ioas_any_min
lib.rsas_any_min
version1.0.2
sourcesrc
created_at2020-10-25 00:47:20.180411
updated_at2020-10-25 01:28:54.262586
descriptionA minuscule rust library that allows trait objects to be easily upcast to Any
homepage
repositoryhttps://github.com/liambloom/as_any_min
max_upload_size
id305191
size6,034
(liambloom)

documentation

README

as_any_min

This is a very minimal crate that makes it easier to work with traits that implement Any by allowing you to easily upcast to it.

Example

use core::any::Any;
use as_any_min::AsAny;

struct MyStruct;
trait MyTrait {}
impl MyTrait for MyStruct {}

/* Note that AsAny is automatically implemented for all
    structs that implement Any, so there is no need to
    implement it manually (in fact it won't compile if
    you try to) */

fn main() {
    // my_variable is now a trait object, which is the
    //  main use case for the AsAny trait.
    let my_variable: &dyn MyTrait = &MyStruct;

    let my_any_variable: &dyn Any = my_variable.as_any();
}

Without Using AsAny

Since rust doesn't (currently) have any built in way to upcast from a trait object to another trait (such as Any), this won't compile.

use core::any::Any;

struct MyStruct;
trait MyTrait {}
impl MyTrait for MyStruct {}

fn main() {
    let my_variable: &dyn MyTrait = &MyStruct;

    let my_any_variable: &dyn Any = my_variable;
}

License: MIT

Commit count: 14

cargo fmt