net-ppp: use From and ? to handle write errors.

This commit is contained in:
Dario Nieuwenhuis 2023-08-25 16:04:22 +02:00
parent c2d601abef
commit a026db3f57

View File

@ -55,6 +55,15 @@ pub enum RunError<E> {
Eof, Eof,
} }
impl<E> From<WriteAllError<E>> for RunError<E> {
fn from(value: WriteAllError<E>) -> Self {
match value {
WriteAllError::Other(e) => Self::Write(e),
WriteAllError::WriteZero => Self::WriteZero,
}
}
}
impl<'d> Runner<'d> { impl<'d> Runner<'d> {
/// You must call this in a background task for the driver to operate. /// You must call this in a background task for the driver to operate.
/// ///
@ -112,11 +121,7 @@ impl<'d> Runner<'d> {
buf[..pkt.len()].copy_from_slice(pkt); buf[..pkt.len()].copy_from_slice(pkt);
rx_chan.rx_done(pkt.len()); rx_chan.rx_done(pkt.len());
} }
PPPoSAction::Transmit(n) => match rw.write_all(&tx_buf[..n]).await { PPPoSAction::Transmit(n) => rw.write_all(&tx_buf[..n]).await?,
Ok(()) => {}
Err(WriteAllError::WriteZero) => return Err(RunError::WriteZero),
Err(WriteAllError::Other(e)) => return Err(RunError::Write(e)),
},
} }
match ppp.status().phase { match ppp.status().phase {
@ -126,11 +131,7 @@ impl<'d> Runner<'d> {
} }
Either::Second(pkt) => { Either::Second(pkt) => {
match ppp.send(pkt, &mut tx_buf) { match ppp.send(pkt, &mut tx_buf) {
Ok(n) => match rw.write_all(&tx_buf[..n]).await { Ok(n) => rw.write_all(&tx_buf[..n]).await?,
Ok(()) => {}
Err(WriteAllError::WriteZero) => return Err(RunError::WriteZero),
Err(WriteAllError::Other(e)) => return Err(RunError::Write(e)),
},
Err(BufferFullError) => unreachable!(), Err(BufferFullError) => unreachable!(),
} }
tx_chan.tx_done(); tx_chan.tx_done();