Fix externals busy waiting (#3280)

This commit is contained in:
Jonathan Turner
2021-04-08 07:25:15 +12:00
committed by GitHub
parent 42fac722bb
commit 5fcf11fcb0
3 changed files with 26 additions and 166 deletions

View File

@ -1,7 +1,7 @@
use crate::futures::ThreadedReceiver;
use crate::prelude::*;
use nu_engine::{evaluate_baseline_expr, BufCodecReader};
use nu_engine::{MaybeTextCodec, StringOrBinary};
use parking_lot::Mutex;
use std::io::Write;
use std::ops::Deref;
@ -431,7 +431,7 @@ fn spawn(
Ok(())
});
let stream = ThreadedReceiver::new(rx);
let stream = ChannelReceiver::new(rx);
Ok(stream.to_input_stream())
} else {
Err(ShellError::labeled_error(
@ -442,6 +442,30 @@ fn spawn(
}
}
struct ChannelReceiver {
rx: Arc<Mutex<mpsc::Receiver<Result<Value, ShellError>>>>,
}
impl ChannelReceiver {
pub fn new(rx: mpsc::Receiver<Result<Value, ShellError>>) -> Self {
Self {
rx: Arc::new(Mutex::new(rx)),
}
}
}
impl Iterator for ChannelReceiver {
type Item = Result<Value, ShellError>;
fn next(&mut self) -> Option<Self::Item> {
let rx = self.rx.lock();
match rx.recv() {
Ok(v) => Some(v),
Err(_) => None,
}
}
}
fn expand_tilde<SI: ?Sized, P, HD>(input: &SI, home_dir: HD) -> std::borrow::Cow<str>
where
SI: AsRef<str>,