Cannot `shutdown` TcpStream after upgrading Tokio from 0.x to 1.x

The shutdown signature has changes from &self to &mut self

Tokio 0.x:

pub fn shutdown(&self, how: Shutdown) -> Result<()>

Tokio 1.x

fn shutdown(&mut self) -> Shutdown<'_, Self>

I have a variable write_half with type OwnedWriteHalf, and this code used to work in Tokio 0.x:

write_half.as_ref().shutdown(Shutdown::Both)

But in tokio 1.0, it stops working since write_half.as_ref() returns a TcpStream ref, but shutdown requires a mutable ref. What should I do to access a mutable ref of TcpStream from its OwnedWriteHalf?

Thanks.

You can call shutdown directly on the write_half.

Also, be aware that shutdown(Both) is pretty terrible. It has very platform specific-behavior. It is generally only consistently defined what happens if you shut down the write direction.

@alice But in the tokio doc, the OwnedWriteHalf does not have the shutdown function. It only has reunite and forget

It is a method from the AsyncWriteExt trait that you can call on anything that implements AsyncWrite. Note that the owned write half also calls the method in its destructor, so an alternative is to just drop it.

Oh thank you. I thought that only TcpStream has the shutdown method. It works now.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.