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

merge functionality of io::Sink into io::Empty #49

Closed
vidhanio opened this issue Jun 16, 2022 · 4 comments
Closed

merge functionality of io::Sink into io::Empty #49

vidhanio opened this issue Jun 16, 2022 · 4 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@vidhanio
Copy link

vidhanio commented Jun 16, 2022

Proposal

Problem statement

Many times, there is a need for a simple dummy io::Reader io::Writer, but currently the only options are io::Empty and io::Sink respectively. Having both of their functionality together requires writing your own boilerplate for something that makes sense to have in the standard library. This change adds the functionality of io::Sink to io::Empty, making io::Empty be able to perform the tasks of both of the previous structs.

Motivation, use-cases

A simple idea for this is when a library requires a full stream, but as the developer, you don't really care about the output/input the library requires.

use std::io;

struct NeedsStream<S: io::Write   io::Read> {
    stream: T,
}

impl<S: io::Write   io::Read> NeedsStream<S> {
    fn new(s: S) -> Self { unimplemented!() } 

    // conducts some process, reading from the stream, and
    // sending some output to it on events.
    fn process(&mut self) -> bool { unimplemented!() } 
}

// I just want the output `bool` of the processing, and 
// don't care about the stream processing.
let mut n = io::null();
let ns = NeedsStream::new(n);
ns.process();

Solution sketches

I recently had a use for this, but I had to create my own entire struct for such a simple use case:

struct Null {
    empty: Empty,
    sink: Sink,
}

impl Write for Null {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.sink.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.sink.flush()
    }
}

impl Read for Null {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.empty.read(buf)
    }
}

Links and related work

Pull Request: rust-lang/rust#98154

This idea was proposed in rust-lang/rust#24235 from a quick search.

This idea is also similar to the /dev/null on Unix.

@vidhanio vidhanio added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jun 16, 2022
@vidhanio
Copy link
Author

I seem to have put this in the wrong place, as it is not a change to an unstable API. Apologies.

@vidhanio vidhanio closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2022
@m-ou-se
Copy link
Member

m-ou-se commented Jun 20, 2022

It's a proposal for a new (initially unstable) api, which also belongs here. :)

@m-ou-se m-ou-se reopened this Jun 20, 2022
@vidhanio
Copy link
Author

Oh, interesting, sorry for closing it. Thank you for the guidance! :)

@vidhanio vidhanio changed the title io::Null: a combination of io::Sink and io::Empty merge functionality of io::Sink into io::Empty Jul 7, 2022
@dtolnay
Copy link
Member

dtolnay commented Oct 11, 2022

This is in FCP now over at rust-lang/rust#98154 (comment) :)

@dtolnay dtolnay closed this as completed Oct 11, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 30, 2023
merge functionality of `io::Sink` into `io::Empty`

Many times, there is a need for a simple dummy `io::Read`er   `io::Write`r, but currently the only options are `io::Empty` and `io::Sink` respectively. Having both of their functionality together requires writing your own boilerplate for something that makes sense to have in the standard library. This PR adds the functionality of `io::Sink` to `io::Empty`, making `io::Empty` be able to perform the tasks of both of the previous structs. (This idea was first mentioned in rust-lang#24235)

Note: I also updated some doc comments in `io::utils` in this pull request to fix inconsistencies between `io::Sink` and `io::Empty`.

API Change Proposal: rust-lang/libs-team#49
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 30, 2023
merge functionality of `io::Sink` into `io::Empty`

Many times, there is a need for a simple dummy `io::Read`er   `io::Write`r, but currently the only options are `io::Empty` and `io::Sink` respectively. Having both of their functionality together requires writing your own boilerplate for something that makes sense to have in the standard library. This PR adds the functionality of `io::Sink` to `io::Empty`, making `io::Empty` be able to perform the tasks of both of the previous structs. (This idea was first mentioned in rust-lang#24235)

Note: I also updated some doc comments in `io::utils` in this pull request to fix inconsistencies between `io::Sink` and `io::Empty`.

API Change Proposal: rust-lang/libs-team#49
thomcc pushed a commit to tcdi/postgrestd that referenced this issue Oct 17, 2023
merge functionality of `io::Sink` into `io::Empty`

Many times, there is a need for a simple dummy `io::Read`er   `io::Write`r, but currently the only options are `io::Empty` and `io::Sink` respectively. Having both of their functionality together requires writing your own boilerplate for something that makes sense to have in the standard library. This PR adds the functionality of `io::Sink` to `io::Empty`, making `io::Empty` be able to perform the tasks of both of the previous structs. (This idea was first mentioned in #24235)

Note: I also updated some doc comments in `io::utils` in this pull request to fix inconsistencies between `io::Sink` and `io::Empty`.

API Change Proposal: rust-lang/libs-team#49
@dtolnay dtolnay added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants