Skip to content

Commit

Permalink
Revert "[git-packetline] It compiles with parent as option, even with…
Browse files Browse the repository at this point in the history
… state machine"

This reverts commit a97bbfd.
  • Loading branch information
Byron committed May 18, 2021
1 parent a97bbfd commit 890cc50
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions git-packetline/src/read/sidebands/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ where
T: AsyncRead,
{
fn drop(&mut self) {
if let State::Idle { ref mut parent } = self.state {
parent.as_mut().unwrap().reset();
if let State::Idle { parent } = self.state {
parent.reset();
}
}
}
Expand All @@ -43,7 +43,7 @@ where
/// Create a new instance with the given provider as `parent`.
pub fn new(parent: &'a mut StreamingPeekableIter<T>) -> Self {
WithSidebands {
state: State::Idle { parent: Some(parent) },
state: State::Idle { parent },
handle_progress: None,
pos: 0,
cap: 0,
Expand All @@ -53,7 +53,7 @@ where

enum State<'a, T> {
Idle {
parent: Option<&'a mut StreamingPeekableIter<T>>,
parent: &'a mut StreamingPeekableIter<T>,
},
ReadLine {
read_line: Pin<Box<dyn Future<Output = ReadLineResult<'a>> + 'a>>,
Expand All @@ -72,7 +72,7 @@ where
/// being true in case the `text` is to be interpreted as error.
pub fn with_progress_handler(parent: &'a mut StreamingPeekableIter<T>, handle_progress: F) -> Self {
WithSidebands {
state: State::Idle { parent: Some(parent) },
state: State::Idle { parent },
handle_progress: Some(handle_progress),
pos: 0,
cap: 0,
Expand All @@ -82,7 +82,7 @@ where
/// Create a new instance without a progress handler.
pub fn without_progress_handler(parent: &'a mut StreamingPeekableIter<T>) -> Self {
WithSidebands {
state: State::Idle { parent: Some(parent) },
state: State::Idle { parent },
handle_progress: None,
pos: 0,
cap: 0,
Expand All @@ -92,14 +92,14 @@ where
/// Forwards to the parent [StreamingPeekableIter::reset_with()]
pub fn reset_with(&mut self, delimiters: &'static [PacketLine<'static>]) {
if let State::Idle { ref mut parent } = self.state {
parent.as_mut().unwrap().reset_with(delimiters)
parent.reset_with(delimiters)
}
}

/// Forwards to the parent [StreamingPeekableIter::stopped_at()]
pub fn stopped_at(&self) -> Option<PacketLine<'static>> {
match self.state {
State::Idle { ref parent } => parent.as_ref().unwrap().stopped_at,
State::Idle { ref parent } => parent.stopped_at,
_ => None,
}
}
Expand All @@ -113,7 +113,7 @@ where
/// next on a call to [`read_line()`][io::BufRead::read_line()].
pub async fn peek_data_line(&mut self) -> Option<std::io::Result<Result<&[u8], crate::decode::Error>>> {
match self.state {
State::Idle { ref mut parent } => match parent.as_mut().unwrap().peek_line().await {
State::Idle { ref mut parent } => match parent.peek_line().await {
Some(Ok(Ok(crate::PacketLine::Data(line)))) => Some(Ok(Ok(line))),
Some(Ok(Err(err))) => Some(Ok(Err(err))),
Some(Err(err)) => Some(Err(err)),
Expand All @@ -133,21 +133,19 @@ where
use futures_lite::FutureExt;
use std::io;
{
let this = self.as_mut().get_mut();
let this = self.get_mut();
if this.pos >= this.cap {
let (ofs, cap) = loop {
match this.state {
State::Idle { ref mut parent } => {
let parent = parent.take().unwrap();
let inactive = parent as *mut _;
this.state = State::ReadLine {
read_line: parent.read_line().boxed(),
parent_inactive: inactive,
parent_inactive: *parent as *mut _,
}
}
State::ReadLine {
ref mut read_line,
parent_inactive: _,
parent_inactive,
} => {
let line = match ready!(read_line.poll(cx)) {
Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
Expand Down Expand Up @@ -191,15 +189,14 @@ where
}
}
let range = self.pos..self.cap;
match self.get_mut().state {
State::Idle { ref parent } => Poll::Ready(Ok(&parent.as_ref().unwrap().buf[range])),
match self.state {
State::Idle { parent } => Poll::Ready(Ok(&parent.buf[range])),
State::ReadLine { .. } => unreachable!("at least in theory"),
}
}

fn consume(self: Pin<&mut Self>, amt: usize) {
let this = self.get_mut();
this.pos = std::cmp::min(this.pos + amt, this.cap);
self.pos = std::cmp::min(self.pos + amt, self.cap);
}
}

Expand Down

0 comments on commit 890cc50

Please sign in to comment.