History

[protocol] FAIL2: lifetime issues with loop

The loop is needed to handle interrupt errors correctly. Writing it differently has no effect either, that's good I guess

Sebastian Thiel committed 6 years ago

Commit c2ff0701 parent 0f468f93 files · +19 −18Browse files

Showing the changes to git-protocol/src (1 of 3 files). Show all

Unified · Split

git-protocol/src/packet_line/mod.rs+18−17
@@ -26,8 +26,9 @@ mod read { · 25 unmodified lines
2626 /// start of whatever comes next.
2727 pub struct Reader<T> {
2828 pub inner: T,
29− buf: Option<Vec<u8>>,
29+ buf: Vec<u8>,
3030 occupied: Option<std::ops::Range<usize>>,
31+ is_done: bool,
3132 }
3233
3334 impl<T> Reader<T>
@@ -37,14 +38,16 @@ mod read { · 3 unmodified lines
3738 pub fn new(inner: T) -> Self {
3839 Reader {
3940 inner,
40− buf: Some({
41+ buf: {
4142 let mut v = Vec::with_capacity(MAX_LINE_LEN);
4243 v.resize(MAX_LINE_LEN, 0);
4344 v
44− }),
45+ },
4546 occupied: None,
47+ is_done: false,
4648 }
4749 }
50+
4851 fn read_line_inner<'a>(
4952 reader: &mut T,
5053 occupied: &mut Option<std::ops::Range<usize>>,
@@ -60,22 +63,20 @@ mod read { · 9 unmodified lines
6063 }
6164
6265 pub fn read_line(&mut self) -> Option<io::Result<Result<Borrowed, decode::Error>>> {
63− match self.buf.as_mut() {
64− Some(buf) => {
65− loop {
66− match Self::read_line_inner(&mut self.inner, &mut self.occupied, buf) {
67− Ok(Ok(line)) if line == Borrowed::Flush => {
68− self.buf = None;
69− return None;
70− }
71− Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
72− // res => return Some(res),
73− res => unimplemented!("tst"),
74− }
66+ if self.is_done {
67+ return None;
68+ }
69+ Some(loop {
70+ match Self::read_line_inner(&mut self.inner, &mut self.occupied, &mut self.buf) {
71+ Ok(Ok(line)) if line == Borrowed::Flush => {
72+ self.is_done = true;
73+ return None;
7574 }
75+ Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
76+ res => break res,
77+ // res => unimplemented!("tst"),
7678 }
77− None => None,
78− }
79+ })
7980 }
8081 }
8182 }