crates/gunnar-sendpack/src/transport.rs
//! The seam every wire conversation in this crate runs over: **a blocking
//! reader and a blocking writer**.
//!
//! That is all git-over-SSH is. The remote runs `git-receive-pack` with its
//! stdin and stdout wired to a channel, and the client talks to those two
//! pipes. Nothing above this module knows or cares whether the bytes travel
//! over SSH, over a socketpair, or over a `Vec<u8>` in a unit test, which is
//! exactly why [`send_pack`](crate::send_pack) is proved against the real
//! `git receive-pack` binary without an sshd.
//!
//! # Why the trait yields both halves at once
//!
//! Push is not request/response. The command list is written, the packfile
//! follows it, and `receive-pack` may start reporting (or fail loudly on band
//! 3) while the pack is still being written. A trait that handed out the reader
//! and the writer through separate `&mut self` calls could not hold both, so
//! [`Transport::io`] returns the disjoint pair in one borrow.
use std::io::{Read, Write};
/// A bidirectional blocking byte stream to a remote git command.
pub trait Transport {
/// The reader (remote stdout) and writer (remote stdin) as a disjoint pair.
fn io(&mut self) -> (&mut dyn Read, &mut dyn Write);
}
/// Any `(Read, Write)` pair as a [`Transport`].
///
/// The escape hatch for anything already holding two halves: a pair of pipes to
/// a child process, a `TcpStream` split, or a pair of in-memory buffers.
pub struct IoPair<R, W> {
/// Remote stdout.
pub reader: R,
/// Remote stdin.
pub writer: W,
}
impl<R, W> IoPair<R, W> {
/// Pair a reader with a writer.
pub fn new(reader: R, writer: W) -> Self {
IoPair { reader, writer }
}
}
impl<R: Read, W: Write> Transport for IoPair<R, W> {
fn io(&mut self) -> (&mut dyn Read, &mut dyn Write) {
(&mut self.reader, &mut self.writer)
}
}
impl<T: Transport + ?Sized> Transport for &mut T {
fn io(&mut self) -> (&mut dyn Read, &mut dyn Write) {
(**self).io()
}
}