Hi, I have a 3rd party API which take in a tokio::net::tcpstream which I would like to send back any error occur from the API if any, understand that tokio::net::tcpstream does not have copy trait hence I ended up wrapping it with Arc plus mutex. However, I having difficulties in getting back the tcpstream. This is my current uncompilable code:
loop {
let (stream, addr) = listener.accept().await?;
let stream_share = Arc::new(Mutex::new(stream));
tokio::spawn(async move {
let stream_clone = stream_share.clone();
if let Err(e) = processAPI(param1, stream_clone.lock().await, param3).await {
println!("an error occurred; error = {:?}", e);
//would like to send back the error
//let error_stream = stream_share.clone()
//....
}
});
}
The compilation error for above: expected struct tokio::net::TcpStream
, found struct tokio::sync::MutexGuard
if i deref; i.e *(stream_clone.lock().await), the error is: cannot move out of dereference of tokio::sync::MutexGuard<'_, tokio::net::TcpStream>
(move occurs because value has type tokio::net::TcpStream
, which does not implement the Copy
trait)
Would really appreciate if anyone can help me out. Thanks!