Signaling/forcing end of stream in serde::from_read()

I am using sockets from socket2 crate to implement a server that will receive stream of data from clients and then use serde to deserialize. serde has a from_reader that can deserialize directly from the stream without buffering in memory

Since the sockets from socket2 crate implement Read trait, I've been trying something similar to this:

let (sock, addr) = server_socket.accept().unwrap();
let payload: MyStruct = serde_json::from_reader(sock).unwrap();

This only works if the client drops its socket to signal an EOF which then causes from_reader() to finish & return.
But if client keeps the socket active (say we want to have more back and forth communication), then the above from_reader does not return and blocks forever.

So do I have to kill/shutdown my client's socket every time to just send one payload and create a new socket for another payload? (doesn't seem right to me!)

Is there a way I can keep the socket active/open but signal to from_reader an EOF?

By the way the socket type I am using are Unix-domain socket since the client & server will always be on the same machine.

Do you know of any example/source that achieves something similar to this in Rust so I can take a look?
Should I implement my own version of Read?

Thanks in advance for you response!

This will give you a reusable deserializer:

let mut de = serde_json::Deserializer::from_reader(sock);
let payload1 = MyStruct::deserialize(&mut de)?;
2 Likes

@dtolnay Thank you very much for your reply. It solved a big portion of my problem.

I am guessing this solution like my original one works on stream without creating memory buffers.

I am just curious how this works without 'sock' receiving EOF.
Does MyStruct::deserialize stops the deserialization operation as soon as sock inform it that number of bytes read is 0?

It reads one JSON value. We can tell where a value ends without there needing to be any eof or bytes=0.