#cast #any #specialization

identity_cast

A library for specializing on types dynamically via Any

1 stable release

1.0.0 Aug 14, 2021

#14 in #specialization

34 downloads per month

MIT/Apache

7KB
79 lines

This library provides functions for specializing on types dynamically via Any. The basic idea is that it allows you to "cast" any T into any other U, if T and U are actually the same type.

This is exposed both as freestanding functions, as well as an extension trait that can be imported.

Example

use identity_cast::IdentityCast;

fn print_i32_specially<T: 'static>(v: T) {
    match v.into_same::<i32>() {
        Ok(v) => {
            println!("This is a `i32` with value {}", v);
        }
        Err(_) => {
            println!("This is some `T`");
        }
    }
}

No runtime deps