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

Make GhciWriter public #183

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions src/ghci/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,40 @@ use tokio_util::compat::Compat;
use tokio_util::compat::FuturesAsyncWriteCompatExt;
use tokio_util::compat::TokioAsyncWriteCompatExt;

/// A dynamically reconfigurable sink for `ghci` process output. Built for use in `GhciOpts`, but
/// usable as a general purpose clonable [`AsyncWrite`]r.
#[derive(Debug)]
pub enum GhciWriter {
pub struct GhciWriter(Kind);

#[derive(Debug)]
enum Kind {
Stdout(Stdout),
Stderr(Stderr),
DuplexStream(Compat<Arc<Mutex<Compat<DuplexStream>>>>),
Sink(Sink),
}

impl GhciWriter {
/// Write to `stdout`.
pub fn stdout() -> Self {
Self::Stdout(tokio::io::stdout())
Self(Kind::Stdout(tokio::io::stdout()))
}

/// Write to `stderr`.
pub fn stderr() -> Self {
Self::Stderr(tokio::io::stderr())
Self(Kind::Stderr(tokio::io::stderr()))
}

/// Write to an in-memory buffer.
pub fn duplex_stream(duplex_stream: DuplexStream) -> Self {
Self::DuplexStream(Arc::new(Mutex::new(duplex_stream.compat_write())).compat_write())
Self(Kind::DuplexStream(
Arc::new(Mutex::new(duplex_stream.compat_write())).compat_write(),
))
}

/// Write to the void.
pub fn sink() -> Self {
Self::Sink(tokio::io::sink())
Self(Kind::Sink(tokio::io::sink()))
}
}

Expand All @@ -46,40 +57,40 @@ impl AsyncWrite for GhciWriter {
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
match Pin::into_inner(self) {
Self::Stdout(ref mut x) => Pin::new(x).poll_write(cx, buf),
Self::Stderr(ref mut x) => Pin::new(x).poll_write(cx, buf),
Self::DuplexStream(ref mut x) => Pin::new(x).poll_write(cx, buf),
Self::Sink(ref mut x) => Pin::new(x).poll_write(cx, buf),
match Pin::into_inner(self).0 {
Kind::Stdout(ref mut x) => Pin::new(x).poll_write(cx, buf),
Kind::Stderr(ref mut x) => Pin::new(x).poll_write(cx, buf),
Kind::DuplexStream(ref mut x) => Pin::new(x).poll_write(cx, buf),
Kind::Sink(ref mut x) => Pin::new(x).poll_write(cx, buf),
}
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
match Pin::into_inner(self) {
Self::Stdout(ref mut x) => Pin::new(x).poll_flush(cx),
Self::Stderr(ref mut x) => Pin::new(x).poll_flush(cx),
Self::DuplexStream(ref mut x) => Pin::new(x).poll_flush(cx),
Self::Sink(ref mut x) => Pin::new(x).poll_flush(cx),
match Pin::into_inner(self).0 {
Kind::Stdout(ref mut x) => Pin::new(x).poll_flush(cx),
Kind::Stderr(ref mut x) => Pin::new(x).poll_flush(cx),
Kind::DuplexStream(ref mut x) => Pin::new(x).poll_flush(cx),
Kind::Sink(ref mut x) => Pin::new(x).poll_flush(cx),
}
}

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
match Pin::into_inner(self) {
Self::Stdout(ref mut x) => Pin::new(x).poll_shutdown(cx),
Self::Stderr(ref mut x) => Pin::new(x).poll_shutdown(cx),
Self::DuplexStream(ref mut x) => Pin::new(x).poll_shutdown(cx),
Self::Sink(ref mut x) => Pin::new(x).poll_shutdown(cx),
match Pin::into_inner(self).0 {
Kind::Stdout(ref mut x) => Pin::new(x).poll_shutdown(cx),
Kind::Stderr(ref mut x) => Pin::new(x).poll_shutdown(cx),
Kind::DuplexStream(ref mut x) => Pin::new(x).poll_shutdown(cx),
Kind::Sink(ref mut x) => Pin::new(x).poll_shutdown(cx),
}
}
}

impl Clone for GhciWriter {
fn clone(&self) -> Self {
match self {
Self::Stdout(_) => Self::stdout(),
Self::Stderr(_) => Self::stderr(),
Self::DuplexStream(x) => Self::DuplexStream(x.clone()),
Self::Sink(_) => Self::sink(),
match &self.0 {
Kind::Stdout(_) => Self::stdout(),
Kind::Stderr(_) => Self::stderr(),
Kind::DuplexStream(x) => Self(Kind::DuplexStream(x.clone())),
Kind::Sink(_) => Self::sink(),
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub(crate) use string_case::StringCase;
pub use ghci::manager::run_ghci;
pub use ghci::Ghci;
pub use ghci::GhciOpts;
pub use ghci::GhciWriter;
pub use shutdown::ShutdownError;
pub use shutdown::ShutdownHandle;
pub use shutdown::ShutdownManager;
Expand Down