These are chat archives for rust-lang/rust

23rd
Jan 2019
Victor Lopes
@vlopes11
Jan 23 10:05
Hello Rustaceans! Is it possible to match the generic type?
trait SomeTrait {}

struct SomeA {}
struct SomeB {}

impl SomeTrait for SomeA {}
impl SomeTrait for SomeB {}

struct SomeC<T: SomeTrait> {}

impl<T: SomeTrait> SomeC<T> {
    pub fn new(contents: T) -> Self {
        match T {
            SomeA => /* do some stuff */,
            SomeB => /* do some stuff */,
        }

        SomeC {}
    }
}
toxicafunk
@toxicafunk
Jan 23 10:09
i wouldn't think so... but maybe someone else can confirm
Vanquisher Winbringer
@VictoremWinbringer
Jan 23 10:14
Result enum is generic type
Denis Lisov
@tanriol
Jan 23 10:16
You cannot match on the concrete type of the generic parameter. There are some possible cheats, but in general this is not a good idea.
Victor Lopes
@vlopes11
Jan 23 10:17
Yep, agreed, just curious. The proper way would be to enforce different concrete implementations for the new function, right?
It's kinda repeating yourself, tho. If we declare several times the new
Michal 'vorner' Vaner
@vorner
Jan 23 10:18
If the code looks the same but is not the same on type level, you can use a macro to generate it.
Denis Lisov
@tanriol
Jan 23 10:19
Don't know your specific use case, but the most proper way here is to move /* do some stuff */ or the parts of it that differ into a method on SomeTrait.
Victor Lopes
@vlopes11
Jan 23 10:21
Ok, got it. Also passing an callable Fn to a macro would be "proper"? Maybe it's a too generic question to answer with a simple yes or no.
Denis Lisov
@tanriol
Jan 23 10:22
Yeah, depends on the use case.
Victor Lopes
@vlopes11
Jan 23 10:22
Ok, thanks
Christiaan676
@Christiaan676
Jan 23 12:15
U can use TypeId to check the type of T. TypeId::of::<String>() == TypeId::of::<T>(). But in general this is not the best solution.
Michael(LAI)
@Michael-Lfx
Jan 23 15:49
Will it be rejected by Apple App Store because the app is written in Rust such as some modules are in Rust?
Denis Lisov
@tanriol
Jan 23 16:34
Don't know their requirements, but you may want to look into rust-lang/rust#35968
Michael(LAI)
@Michael-Lfx
Jan 23 21:52
Thank you.
Martijn Bakker
@Gladdy
Jan 23 23:47
Does anyone know whether there is a module/function that takes std::any::TypeId and turns it into a String?
Without having to manually/macro'ed put the TypeIds of all the types that might possibly occur into a hash table mapped to their string repr