Home > Back-end >  Select constant based on the generic type in rust function
Select constant based on the generic type in rust function

Time:06-24

Is there any way to select the value of a constant depending on the generic type?

For example, something like (not valid Rust code):

fn foo<T>() {
    let b = match T {
        u32 => 0x01234567u32,
        u64 => 0x0123456789abcdefu64,
        _ => panic!()
    }
}

fn main() {
    foo::<u32>();
    foo::<u64>();
}

This function would be only designed to operate with u16, u32 and u64 types.

CodePudding user response:

You could use an associated constant of a trait:

trait MyTrait {
    const SOME_CONST: Self;
}

impl MyTrait for u32 {
    const SOME_CONST: Self = 0x01234567u32;
}

impl MyTrait for u64 {
    const SOME_CONST: Self = 0x0123456789abcdefu64;
}

fn foo<T>() where T: MyTrait {
    let _b: T = T::SOME_CONST;
}

fn main() {
    foo::<u32>();
    foo::<u64>();
}

Try it on the rust playground.

CodePudding user response:

If you need to do more than just return a value, you could also match on the TypeId of your generic, but you'd have to wrap your return value in an enum because you can't return multiple types from a single match statement:

use std::any::TypeId;

#[derive(Debug)]
enum Value {
    u32(u32),
    u64(u64),
}

fn foo<T: 'static>() -> Value {
    match TypeId::of::<T>() {
        t if t == TypeId::of::<u32>() => {
            // other stuff
            Value::u32(0x01234567u32)
        },
        t if t == TypeId::of::<u64>() => {
            // other stuff
            Value::u64(0x0123456789abcdefu64)
        },
        _ => panic!(),
    }
}

fn main() {
    println!("{:?}", foo::<u32>());
    println!("{:?}", foo::<u64>());
}

The associated constants already mentioned are better if getting a constant value is the only functionality you need though.

  • Related