Skip to content

Commit

Permalink
[git-transport] naive attempt to make Request async…
Browse files Browse the repository at this point in the history
…and of course there is a lot of issues to look at.
  • Loading branch information
Byron committed Jun 2, 2021
1 parent d4e56c8 commit b819546
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 36 deletions.
2 changes: 0 additions & 2 deletions git-transport/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ mod non_io_types;
pub use non_io_types::{Error, Identity, MessageKind, WriteMode};

///
#[cfg(feature = "blocking-client")]
pub mod request;
#[doc(inline)]
#[cfg(feature = "blocking-client")]
pub use request::RequestWriter;
142 changes: 108 additions & 34 deletions git-transport/src/client/request.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,135 @@
use crate::client::{ExtendedBufRead, MessageKind, WriteMode};
use crate::client::{ExtendedBufRead, MessageKind};
use std::io;

#[cfg(all(not(feature = "blocking-client"), feature = "async-client"))]
use futures_io::AsyncWrite;

/// A [`Write`][io::Write] implementation optimized for writing packet lines.
/// A type implementing `Write` for packet lines, which when done can be transformed into a `Read` for
/// obtaining the response.
pub struct RequestWriter<'a> {
on_into_read: MessageKind,
#[cfg(feature = "blocking-client")]
pub(crate) writer: git_packetline::Writer<Box<dyn io::Write + 'a>>,
#[cfg(all(not(feature = "blocking-client"), feature = "async-client"))]
pub(crate) writer: git_packetline::Writer<Box<dyn AsyncWrite + 'a>>,
pub(crate) reader: Box<dyn ExtendedBufRead + 'a>,
}

impl<'a> io::Write for RequestWriter<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.writer.write(buf)
#[cfg(feature = "blocking-client")]
mod blocking_io {
use crate::client::{ExtendedBufRead, MessageKind, RequestWriter, WriteMode};
use std::io;

impl<'a> io::Write for RequestWriter<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.writer.write(buf)
}

fn flush(&mut self) -> io::Result<()> {
self.writer.flush()
}
}

fn flush(&mut self) -> io::Result<()> {
self.writer.flush()
/// methods with bonds to IO
impl<'a> RequestWriter<'a> {
/// Create a new instance from a `writer` (commonly a socket), a `reader` into which to transform once the
/// writes are finished, along with configuration for the `write_mode` and information about which message to write
/// when this instance is converted into a `reader` to read the request's response.
pub fn new_from_bufread<W: io::Write + 'a>(
writer: W,
reader: Box<dyn ExtendedBufRead + 'a>,
write_mode: WriteMode,
on_into_read: MessageKind,
) -> Self {
let mut writer = git_packetline::Writer::new(Box::new(writer) as Box<dyn io::Write>);
match write_mode {
WriteMode::Binary => writer.enable_binary_mode(),
WriteMode::OneLfTerminatedLinePerWriteCall => writer.enable_text_mode(),
}
RequestWriter {
on_into_read,
writer,
reader,
}
}

/// Write the given message as packet line.
pub fn write_message(&mut self, message: MessageKind) -> io::Result<()> {
match message {
MessageKind::Flush => git_packetline::PacketLine::Flush.to_write(self.writer.inner_mut()),
MessageKind::Delimiter => git_packetline::PacketLine::Delimiter.to_write(self.writer.inner_mut()),
MessageKind::ResponseEnd => git_packetline::PacketLine::ResponseEnd.to_write(self.writer.inner_mut()),
MessageKind::Text(t) => git_packetline::immutable::Text::from(t).to_write(self.writer.inner_mut()),
}
.map(|_| ())
}
}
}

/// methods with bonds to IO
impl<'a> RequestWriter<'a> {
/// Create a new instance from a `writer` (commonly a socket), a `reader` into which to transform once the
/// writes are finished, along with configuration for the `write_mode` and information about which message to write
/// when this instance is converted into a `reader` to read the request's response.
pub fn new_from_bufread<W: io::Write + 'a>(
writer: W,
reader: Box<dyn ExtendedBufRead + 'a>,
write_mode: WriteMode,
on_into_read: MessageKind,
) -> Self {
let mut writer = git_packetline::Writer::new(Box::new(writer) as Box<dyn io::Write>);
match write_mode {
WriteMode::Binary => writer.enable_binary_mode(),
WriteMode::OneLfTerminatedLinePerWriteCall => writer.enable_text_mode(),
#[cfg(all(not(feature = "blocking-client"), feature = "async-client"))]
mod async_io {
use crate::client::{ExtendedBufRead, MessageKind, RequestWriter, WriteMode};

use futures_io::AsyncWrite;
use std::{
io,
pin::Pin,
task::{Context, Poll},
};

impl<'a> futures_io::AsyncWrite for RequestWriter<'a> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
self.writer.poll_write(cx, buf)
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.writer.poll_flush(cx)
}
RequestWriter {
on_into_read,
writer,
reader,

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.writer.poll_close(cx)
}
}

/// Write the given message as packet line.
pub fn write_message(&mut self, message: MessageKind) -> io::Result<()> {
match message {
MessageKind::Flush => git_packetline::PacketLine::Flush.to_write(self.writer.inner_mut()),
MessageKind::Delimiter => git_packetline::PacketLine::Delimiter.to_write(self.writer.inner_mut()),
MessageKind::ResponseEnd => git_packetline::PacketLine::ResponseEnd.to_write(self.writer.inner_mut()),
MessageKind::Text(t) => git_packetline::immutable::Text::from(t).to_write(self.writer.inner_mut()),
/// methods with bonds to IO
impl<'a> RequestWriter<'a> {
/// Create a new instance from a `writer` (commonly a socket), a `reader` into which to transform once the
/// writes are finished, along with configuration for the `write_mode` and information about which message to write
/// when this instance is converted into a `reader` to read the request's response.
pub fn new_from_bufread<W: AsyncWrite + 'a>(
writer: W,
reader: Box<dyn ExtendedBufRead + 'a>,
write_mode: WriteMode,
on_into_read: MessageKind,
) -> Self {
let mut writer = git_packetline::Writer::new(Box::new(writer) as Box<dyn io::Write>);
match write_mode {
WriteMode::Binary => writer.enable_binary_mode(),
WriteMode::OneLfTerminatedLinePerWriteCall => writer.enable_text_mode(),
}
RequestWriter {
on_into_read,
writer,
reader,
}
}

/// Write the given message as packet line.
pub async fn write_message(&mut self, message: MessageKind) -> io::Result<()> {
match message {
MessageKind::Flush => git_packetline::PacketLine::Flush.to_write(self.writer.inner_mut()),
MessageKind::Delimiter => git_packetline::PacketLine::Delimiter.to_write(self.writer.inner_mut()),
MessageKind::ResponseEnd => git_packetline::PacketLine::ResponseEnd.to_write(self.writer.inner_mut()),
MessageKind::Text(t) => git_packetline::immutable::Text::from(t).to_write(self.writer.inner_mut()),
}
.map(|_| ())
.await
}
.map(|_| ())
}
}

/// TODO: make this general
/// General
impl<'a> RequestWriter<'a> {
/// Discard the ability to write and turn this instance into the reader for obtaining the other side's response.
pub fn into_read(mut self) -> io::Result<Box<dyn ExtendedBufRead + 'a>> {
Expand Down

0 comments on commit b819546

Please sign in to comment.