Skip to content

Commit e300f9f

Browse files
committed
Revert "Revert "[git-packetline] It compiles with parent as option, even with state machine""
This reverts commit 890cc50.
1 parent 8eb78f5 commit e300f9f

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

git-packetline/src/read/sidebands/async_io.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ where
3030
T: AsyncRead,
3131
{
3232
fn drop(&mut self) {
33-
if let State::Idle { parent } = self.state {
34-
parent.reset();
33+
if let State::Idle { ref mut parent } = self.state {
34+
parent.as_mut().unwrap().reset();
3535
}
3636
}
3737
}
@@ -43,7 +43,7 @@ where
4343
/// Create a new instance with the given provider as `parent`.
4444
pub fn new(parent: &'a mut StreamingPeekableIter<T>) -> Self {
4545
WithSidebands {
46-
state: State::Idle { parent },
46+
state: State::Idle { parent: Some(parent) },
4747
handle_progress: None,
4848
pos: 0,
4949
cap: 0,
@@ -53,7 +53,7 @@ where
5353

5454
enum State<'a, T> {
5555
Idle {
56-
parent: &'a mut StreamingPeekableIter<T>,
56+
parent: Option<&'a mut StreamingPeekableIter<T>>,
5757
},
5858
ReadLine {
5959
read_line: Pin<Box<dyn Future<Output = ReadLineResult<'a>> + 'a>>,
@@ -72,7 +72,7 @@ where
7272
/// being true in case the `text` is to be interpreted as error.
7373
pub fn with_progress_handler(parent: &'a mut StreamingPeekableIter<T>, handle_progress: F) -> Self {
7474
WithSidebands {
75-
state: State::Idle { parent },
75+
state: State::Idle { parent: Some(parent) },
7676
handle_progress: Some(handle_progress),
7777
pos: 0,
7878
cap: 0,
@@ -82,7 +82,7 @@ where
8282
/// Create a new instance without a progress handler.
8383
pub fn without_progress_handler(parent: &'a mut StreamingPeekableIter<T>) -> Self {
8484
WithSidebands {
85-
state: State::Idle { parent },
85+
state: State::Idle { parent: Some(parent) },
8686
handle_progress: None,
8787
pos: 0,
8888
cap: 0,
@@ -92,14 +92,14 @@ where
9292
/// Forwards to the parent [StreamingPeekableIter::reset_with()]
9393
pub fn reset_with(&mut self, delimiters: &'static [PacketLine<'static>]) {
9494
if let State::Idle { ref mut parent } = self.state {
95-
parent.reset_with(delimiters)
95+
parent.as_mut().unwrap().reset_with(delimiters)
9696
}
9797
}
9898

9999
/// Forwards to the parent [StreamingPeekableIter::stopped_at()]
100100
pub fn stopped_at(&self) -> Option<PacketLine<'static>> {
101101
match self.state {
102-
State::Idle { ref parent } => parent.stopped_at,
102+
State::Idle { ref parent } => parent.as_ref().unwrap().stopped_at,
103103
_ => None,
104104
}
105105
}
@@ -113,7 +113,7 @@ where
113113
/// next on a call to [`read_line()`][io::BufRead::read_line()].
114114
pub async fn peek_data_line(&mut self) -> Option<std::io::Result<Result<&[u8], crate::decode::Error>>> {
115115
match self.state {
116-
State::Idle { ref mut parent } => match parent.peek_line().await {
116+
State::Idle { ref mut parent } => match parent.as_mut().unwrap().peek_line().await {
117117
Some(Ok(Ok(crate::PacketLine::Data(line)))) => Some(Ok(Ok(line))),
118118
Some(Ok(Err(err))) => Some(Ok(Err(err))),
119119
Some(Err(err)) => Some(Err(err)),
@@ -133,19 +133,21 @@ where
133133
use futures_lite::FutureExt;
134134
use std::io;
135135
{
136-
let this = self.get_mut();
136+
let this = self.as_mut().get_mut();
137137
if this.pos >= this.cap {
138138
let (ofs, cap) = loop {
139139
match this.state {
140140
State::Idle { ref mut parent } => {
141+
let parent = parent.take().unwrap();
142+
let inactive = parent as *mut _;
141143
this.state = State::ReadLine {
142144
read_line: parent.read_line().boxed(),
143-
parent_inactive: *parent as *mut _,
145+
parent_inactive: inactive,
144146
}
145147
}
146148
State::ReadLine {
147149
ref mut read_line,
148-
parent_inactive,
150+
parent_inactive: _,
149151
} => {
150152
let line = match ready!(read_line.poll(cx)) {
151153
Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
@@ -189,14 +191,15 @@ where
189191
}
190192
}
191193
let range = self.pos..self.cap;
192-
match self.state {
193-
State::Idle { parent } => Poll::Ready(Ok(&parent.buf[range])),
194+
match self.get_mut().state {
195+
State::Idle { ref parent } => Poll::Ready(Ok(&parent.as_ref().unwrap().buf[range])),
194196
State::ReadLine { .. } => unreachable!("at least in theory"),
195197
}
196198
}
197199

198200
fn consume(self: Pin<&mut Self>, amt: usize) {
199-
self.pos = std::cmp::min(self.pos + amt, self.cap);
201+
let this = self.get_mut();
202+
this.pos = std::cmp::min(this.pos + amt, this.cap);
200203
}
201204
}
202205

0 commit comments

Comments
 (0)