How can I have a dynamic type based on the user input.

58 points hardfau18

SOLVED: Thanks to u/ALinuxPerson

I am writing a program which takes a input from user which can be std::os::unix::net::UnixStream or std::net::TcpStream depending on whether he gives path or network address. This is used through out the program as a base element.

First i tried with generics, but since this is based on user input not known at compile time It felt like impossible.

Then i tried with enums like

pub enum ConnType{
    Stream(std::os::unix::net::UnixStream),
    Socket(std::net::TcpStream),
}

But this is given to a struct, and everytime I need to use that stream I need to match. But i wanted basic Read + Write. and both has that trait. And I can't use dyn Read + Write because struct member should have known size at compile time.

Basic structure is like this

struct A<t: Read + Write> {
   conn : t
   ...
}

struct B {
    a: A
    ...
}

Here struct A is provided by a library, and I have to make struct B using struct A which type depends on runtime.