vimalloc
vimalloc

Reputation: 4177

Handling TcpStream or SslStream idiomatically

I'm working on a project that can use either TcpStreams or SslStreams, depending on what the user configured. I have a few simple methods such as send_cmd(stream) and recv_msg(stream) which operates on these streams.

Would it be more idiomatic to create an enum that could be either TcpStream or SslStream and pass that to these methods? Or would it be better to do something with traits, such as requiring the stream argument to these methods implement the Read and Write traits?

My thought is that the enum solution would be better, as it explicitly says what kind of data we expect to be handling instead of allowing for anything that implements Read or Write, such as files. On the flip side though, it would require a match on all of these methods before using the stream. Thoughts?

Upvotes: 0

Views: 352

Answers (1)

vimalloc
vimalloc

Reputation: 4177

Turns out there is a solution for this built into the openssl library: openssl::ssl::MaybeSslStream.

Upvotes: 1

Related Questions