From 59615a655bc91bd48fc365015a85f73e3a7b9083 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Sun, 27 Oct 2019 21:48:23 +0900 Subject: [PATCH] feat: Add StderrLock and StdoutLock struct --- src/io/stderr.rs | 27 +++++++++++++++++++++++++-- src/io/stdin.rs | 2 +- src/io/stdout.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/io/stderr.rs b/src/io/stderr.rs index 8193eeb7..1ea33616 100644 --- a/src/io/stderr.rs +++ b/src/io/stderr.rs @@ -44,6 +44,11 @@ pub fn stderr() -> Stderr { #[derive(Debug)] pub struct Stderr(Mutex); +#[derive(Debug)] +pub struct StderrLock<'a>(std::io::StderrLock<'a>); + +unsafe impl Send for StderrLock<'_> {} + /// The state of the asynchronous stderr. /// /// The stderr can be either idle or busy performing an asynchronous operation. @@ -98,12 +103,12 @@ impl Stderr { /// # /// # Ok(()) }) } /// ``` - pub async fn lock(&self) -> std::io::StderrLock<'static> { + pub async fn lock(&self) -> StderrLock<'static> { lazy_static! { static ref STDERR: std::io::Stderr = std::io::stderr(); } - STDERR.lock() + blocking::spawn(move || StderrLock(STDERR.lock())).await } } @@ -209,3 +214,21 @@ cfg_windows! { } } } + +impl Write for StderrLock<'_> { + fn poll_write( + self: Pin<&mut Self>, + _cx: &mut Context<'_>, + _buf: &[u8], + ) -> Poll> { + unimplemented!() + } + + fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + unimplemented!() + } + + fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + unimplemented!() + } +} diff --git a/src/io/stdin.rs b/src/io/stdin.rs index 93d91700..f869cbf6 100644 --- a/src/io/stdin.rs +++ b/src/io/stdin.rs @@ -165,7 +165,7 @@ impl Stdin { static ref STDIN: std::io::Stdin = std::io::stdin(); } - blocking::spawn(move || { StdinLock(STDIN.lock()) }).await + blocking::spawn(move || StdinLock(STDIN.lock())).await } } diff --git a/src/io/stdout.rs b/src/io/stdout.rs index cbe14b8b..360bfe86 100644 --- a/src/io/stdout.rs +++ b/src/io/stdout.rs @@ -44,6 +44,11 @@ pub fn stdout() -> Stdout { #[derive(Debug)] pub struct Stdout(Mutex); +#[derive(Debug)] +pub struct StdoutLock<'a>(std::io::StdoutLock<'a>); + +unsafe impl Send for StdoutLock<'_> {} + /// The state of the asynchronous stdout. /// /// The stdout can be either idle or busy performing an asynchronous operation. @@ -98,12 +103,12 @@ impl Stdout { /// # /// # Ok(()) }) } /// ``` - pub async fn lock(&self) -> std::io::StdoutLock<'static> { + pub async fn lock(&self) -> StdoutLock<'static> { lazy_static! { static ref STDOUT: std::io::Stdout = std::io::stdout(); } - STDOUT.lock() + blocking::spawn(move || StdoutLock(STDOUT.lock())).await } } @@ -209,3 +214,21 @@ cfg_windows! { } } } + +impl Write for StdoutLock<'_> { + fn poll_write( + self: Pin<&mut Self>, + _cx: &mut Context<'_>, + _buf: &[u8], + ) -> Poll> { + unimplemented!() + } + + fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + unimplemented!() + } + + fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + unimplemented!() + } +}