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

io::Null: a combination of io::Sink and io::Empty #50

Closed
vidhanio opened this issue Jun 16, 2022 · 1 comment
Closed

io::Null: a combination of io::Sink and io::Empty #50

vidhanio opened this issue Jun 16, 2022 · 1 comment
Labels
invalid This doesn't seem right

Comments

@vidhanio
Copy link

io::Null: a combination of io::Sink and io::Empty

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.

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

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

This idea is also similiar 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 vidhanio closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2022
@vidhanio
Copy link
Author

I think something glitched and it posted it twice, sorry.

@dtolnay dtolnay added invalid This doesn't seem right and removed T-libs-api api-change-proposal A proposal to add or alter unstable APIs in the standard libraries labels Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants