These are chat archives for rust-lang/rust

23rd
Jan 2019
Victor Lopes
@vlopes11
Jan 23 10:05 UTC
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 UTC
i wouldn't think so... but maybe someone else can confirm
Vanquisher Winbringer
@VictoremWinbringer
Jan 23 10:14 UTC
Result enum is generic type
Denis Lisov
@tanriol
Jan 23 10:16 UTC
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 UTC
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 UTC
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 UTC
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 UTC
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 UTC
Yeah, depends on the use case.
Victor Lopes
@vlopes11
Jan 23 10:22 UTC
Ok, thanks
Christiaan
@Christiaan676
Jan 23 12:15 UTC
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 UTC
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 UTC
Don't know their requirements, but you may want to look into rust-lang/rust#35968
Michael(LAI)
@Michael-Lfx
Jan 23 21:52 UTC
Thank you.
Martijn Bakker
@Gladdy
Jan 23 23:47 UTC
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