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<()>

https://docs.rs/tokio/0.3.3/tokio/net/struct.TcpStream.html#method.shutdown

Tokio 1.x

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

https://docs.rs/tokio/1.4.0/tokio/io/trait.AsyncWriteExt.html#method.shutdown

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

https://docs.rs/tokio/1.4.0/tokio/net/tcp/struct.OwnedWriteHalf.html

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