Howto TcpStream in async fn?

I pass a state object as self to an async fn handler(mut self, ...) function. As one of self's fields I want to have an log: Arc<TcpStream> so that I can log.write(json_msg) to a tcp connected logging service. Invariably, I encounter the error:

the trait `serde::Serialize` is not implemented for `std::net::TcpStream`

I assume there is an idiomatic way of doing this or should I impl De-Serializer for the TcpStream?

You should use your runtime's implementation of TcpStream, such as tokio's tokio::net::TcpStream or async_std's async_std::net::TcpStream.

The standard library doesn't implement futures, it just provides the Future trait - The standard library doesn't provide anything that is async.

I think you may want to skip serialization for the log field, Although I don't really have any context about your program...
See #[serde(skip)].

Serialization is for data such as strings. It doesn't make sense to serialize an open tcp connection.

I want the TcpStream created before entering the async fn. In the async fn I only want to call the write(...) as method of a stream object in one of self's fields. The problem therefore is how to move a TcpStream field of an object into an async fc?

SOLVED: Easy ( the end...): removed serde serialization from the self struct. As a singleton it didn't need any so serde was putting unnecessary constraints on the fields (among which the TcpStream).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.