I have code that compiles and works shown at the (current) bottom of my last topic: clean way to terminate tcpstream
That code works fine, but I don't like how it requires the use of a struct to represent the connection as so:
struct Connection {
opt_stream: Option<TcpStream>,
}
So my goal now is to change the code to simply work with a type for the connection like this:
type Connection = Option<TcpStream>;
My question amounts to how to fix the compiler error that I get when I go from this code block that works with the struct definition for Connection:
let mut safe_stream = mt_stream.lock().unwrap();
if let None = safe_stream.opt_stream {
safe_stream.opt_stream =
Some(TcpStream::connect("127.0.0.1:5555").unwrap());
}
To this (obviously naive) attempt to work with the type definition for Connenction :
let mut safe_stream = mt_stream.lock().unwrap();
if let None = safe_stream {
safe_stream =
Some(TcpStream::connect("127.0.0.1:5555").unwrap());
}
gets the following compiler error:
22 | if let None = safe_stream {
| ^^^^ ----------- this expression has type `MutexGuard<'_, Option<TcpStream>>`
| |
| expected struct `MutexGuard`, found enum `Option`
|
= note: expected struct `MutexGuard<'_, Option<TcpStream>, >`
found enum `Option<_>`
error[E0308]: mismatched types
My attempts to fix this so far have failed, and I'm hoping there's some obvious fix.
Thanks so much for any ideas.