* refactor(fakes): clean up add_terminal_input * refactor(fakes): append whole buf to output_buffer in FakeStdoutWriter::write * refactor(fakes): append whole buf to output_buffer in FakeInputOutput::write_to_tty_stdin * fix(fakes): allow partial reads in read_from_tty_stdout This patch fixes two bugs in read_from_tty_stdout: * if there was a partial read (ie. `bytes.read_position` is not 0 but less than `bytes.content.len()`), subsequent calls to would fill `buf` starting at index `bytes.read_position` instead of 0, leaving range 0..`bytes.read_position` untouched. * if `buf` was smaller than `bytes.content.len()`, a panic would occur. * refactor(channels): use crossbeam instead of mpsc This patch replaces mpsc with crossbeam channels because crossbeam supports selecting on multiple channels which will be necessary in a subsequent patch. * refactor(threadbus): allow multiple receivers in Bus This patch changes Bus to use multiple receivers. Method `recv` returns data from all of them. This will be used in a subsequent patch for receiving from bounded and unbounded queues at the same time. * refactor(channels): remove SenderType enum This enum has only one variant, so the entire enum can be replaced with the innards of said variant. * refactor(channels): remove Send+Sync trait implementations The implementation of these traits is not necessary, as SenderWithContext is automatically Send and Sync for every T and ErrorContext that's Send and Sync.
85 lines
2.9 KiB
Rust
85 lines
2.9 KiB
Rust
//! Definitions and helpers for sending and receiving messages between threads.
|
|
|
|
use crate::{
|
|
os_input_output::ServerOsApi, pty::PtyInstruction, screen::ScreenInstruction,
|
|
wasm_vm::PluginInstruction, ServerInstruction,
|
|
};
|
|
use zellij_utils::{channels, channels::SenderWithContext, errors::ErrorContext};
|
|
|
|
/// A container for senders to the different threads in zellij on the server side
|
|
#[derive(Clone)]
|
|
pub(crate) struct ThreadSenders {
|
|
pub to_screen: Option<SenderWithContext<ScreenInstruction>>,
|
|
pub to_pty: Option<SenderWithContext<PtyInstruction>>,
|
|
pub to_plugin: Option<SenderWithContext<PluginInstruction>>,
|
|
pub to_server: Option<SenderWithContext<ServerInstruction>>,
|
|
}
|
|
|
|
impl ThreadSenders {
|
|
pub fn send_to_screen(
|
|
&self,
|
|
instruction: ScreenInstruction,
|
|
) -> Result<(), channels::SendError<(ScreenInstruction, ErrorContext)>> {
|
|
self.to_screen.as_ref().unwrap().send(instruction)
|
|
}
|
|
|
|
pub fn send_to_pty(
|
|
&self,
|
|
instruction: PtyInstruction,
|
|
) -> Result<(), channels::SendError<(PtyInstruction, ErrorContext)>> {
|
|
self.to_pty.as_ref().unwrap().send(instruction)
|
|
}
|
|
|
|
pub fn send_to_plugin(
|
|
&self,
|
|
instruction: PluginInstruction,
|
|
) -> Result<(), channels::SendError<(PluginInstruction, ErrorContext)>> {
|
|
self.to_plugin.as_ref().unwrap().send(instruction)
|
|
}
|
|
|
|
pub fn send_to_server(
|
|
&self,
|
|
instruction: ServerInstruction,
|
|
) -> Result<(), channels::SendError<(ServerInstruction, ErrorContext)>> {
|
|
self.to_server.as_ref().unwrap().send(instruction)
|
|
}
|
|
}
|
|
|
|
/// A container for a receiver, OS input and the senders to a given thread
|
|
pub(crate) struct Bus<T> {
|
|
receivers: Vec<channels::Receiver<(T, ErrorContext)>>,
|
|
pub senders: ThreadSenders,
|
|
pub os_input: Option<Box<dyn ServerOsApi>>,
|
|
}
|
|
|
|
impl<T> Bus<T> {
|
|
pub fn new(
|
|
receivers: Vec<channels::Receiver<(T, ErrorContext)>>,
|
|
to_screen: Option<&SenderWithContext<ScreenInstruction>>,
|
|
to_pty: Option<&SenderWithContext<PtyInstruction>>,
|
|
to_plugin: Option<&SenderWithContext<PluginInstruction>>,
|
|
to_server: Option<&SenderWithContext<ServerInstruction>>,
|
|
os_input: Option<Box<dyn ServerOsApi>>,
|
|
) -> Self {
|
|
Bus {
|
|
receivers,
|
|
senders: ThreadSenders {
|
|
to_screen: to_screen.cloned(),
|
|
to_pty: to_pty.cloned(),
|
|
to_plugin: to_plugin.cloned(),
|
|
to_server: to_server.cloned(),
|
|
},
|
|
os_input: os_input.clone(),
|
|
}
|
|
}
|
|
|
|
pub fn recv(&self) -> Result<(T, ErrorContext), channels::RecvError> {
|
|
let mut selector = channels::Select::new();
|
|
self.receivers.iter().for_each(|r| {
|
|
selector.recv(r);
|
|
});
|
|
let oper = selector.select();
|
|
let idx = oper.index();
|
|
oper.recv(&self.receivers[idx])
|
|
}
|
|
}
|