Downcast type alias to common primitive type


I have some types from different crates which are both aliases to the same primitive type [u8;32]

Is it possible to tell the compiler to treat an instance of the aliased type as the primitive type?


// external crate a
pub struct A([0; 32]);

// external crate B
use a::A;
type BA = A;

// external crate C
use a::A;
type CA = A;

// My crate
use B::BA;
use C::CA;
// ..
// Here these types don't know each other are the same primitive type

Well, if aliasing is done like so:

type foo = [u8; 32];
type bar = [u8; 32];

Then they’re interchangeable types, which includes their methods, and as function parameters.

fn returns_foo() -> foo {
    [0u8; 32]
fn returns_bar() -> bar {
    [1u8; 32]
fn takes_two_arrays(a: [u8; 32], b: [u8; 32]) {}
fn main() {
    takes_two_arrays(returns_foo(), returns_bar());

Could you post an example of the aliasing?

1 Like