Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: Add close_{read,write}() methods to I/O #13743

Closed
wants to merge 1 commit into from

Conversation

alexcrichton
Copy link
Member

Two new methods were added to TcpStream and UnixStream:

fn close_read(&mut self) -> IoResult<()>;
fn close_write(&mut self) -> IoResult<()>;

These two methods map to shutdown()'s behavior (the system call on unix),
closing the reading or writing half of a duplex stream. These methods are
primarily added to allow waking up a pending read in another task. By closing
the reading half of a connection, all pending readers will be woken up and will
return with EndOfFile. The close_write() method was added for symmetry with
close_read(), and I imagine that it will be quite useful at some point.

Implementation-wise, librustuv got the short end of the stick this time. The
native versions just delegate to the shutdown() syscall (easy). The uv versions
can leverage uv_shutdown() for tcp/unix streams, but only for closing the
writing half. Closing the reading half is done through some careful dancing to
wake up a pending reader.

cc #11165

Two new methods were added to TcpStream and UnixStream:

    fn close_read(&mut self) -> IoResult<()>;
    fn close_write(&mut self) -> IoResult<()>;

These two methods map to shutdown()'s behavior (the system call on unix),
closing the reading or writing half of a duplex stream. These methods are
primarily added to allow waking up a pending read in another task. By closing
the reading half of a connection, all pending readers will be woken up and will
return with EndOfFile. The close_write() method was added for symmetry with
close_read(), and I imagine that it will be quite useful at some point.

Implementation-wise, librustuv got the short end of the stick this time. The
native versions just delegate to the shutdown() syscall (easy). The uv versions
can leverage uv_shutdown() for tcp/unix streams, but only for closing the
writing half. Closing the reading half is done through some careful dancing to
wake up a pending reader.

cc rust-lang#11165
@alexcrichton
Copy link
Member Author

Oops, I'll bring this back in a bit, forgot to implement this for windows pipes.

@LorenVS
Copy link

LorenVS commented Jun 14, 2014

Is there a corresponding implementation of the close_{read,write} that applies to UDP streams? I believe the goal of this commit was to allow graceful shutdown of blocking TCP receive loops. I don't believe the same love was shown to UdpStream

@alexcrichton
Copy link
Member Author

Aha! I was modeling off what go provides, but I'm unaware of a fundamental reason for not offering these functions, so I'll open an issue.

@alexcrichton
Copy link
Member Author

#14926

flip1995 pushed a commit to flip1995/rust that referenced this pull request Dec 15, 2024
Now we can lint for the expressions like `_&_>>_`, `_<<_^_`, etc. And
will suggest to add parentheses like `_&(_>>_)` and `(_<<_)^_`.
I get implementation suggestions from
[https://github.com/rust-lang/rust-clippy/pull/8735#pullrequestreview-954273477](https://github.com/rust-lang/rust-clippy/pull/8735#pullrequestreview-954273477).
changelog: extended [`precedence`] to lint for bit masking and bit
shifting without parentheses
fixes rust-lang#6632
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants