Skip to content

Commit 194bb69

Browse files
committed
socket: simplify error propagation
1 parent 3602c06 commit 194bb69

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/socket/eth.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ impl core::fmt::Display for SendError {
2828
}
2929
}
3030

31+
impl From<crate::storage::Full> for SendError {
32+
fn from(_: crate::storage::Full) -> Self {
33+
Self::BufferFull
34+
}
35+
}
36+
3137
#[cfg(feature = "std")]
3238
impl std::error::Error for SendError {}
3339

@@ -48,6 +54,12 @@ impl core::fmt::Display for RecvError {
4854
}
4955
}
5056

57+
impl From<crate::storage::Empty> for RecvError {
58+
fn from(_: crate::storage::Empty) -> Self {
59+
Self::Exhausted
60+
}
61+
}
62+
5163
#[cfg(feature = "std")]
5264
impl std::error::Error for RecvError {}
5365

@@ -202,8 +214,7 @@ impl<'a> Socket<'a> {
202214
let meta = meta.into();
203215
let packet_buf = self
204216
.tx_buffer
205-
.enqueue(size, meta)
206-
.map_err(|_| SendError::BufferFull)?;
217+
.enqueue(size, meta)?;
207218

208219
net_trace!(
209220
"eth:{}: buffer to send {} octets",
@@ -229,8 +240,7 @@ impl<'a> Socket<'a> {
229240
let meta = meta.into();
230241
let size = self
231242
.tx_buffer
232-
.enqueue_with_infallible(max_size, meta, f)
233-
.map_err(|_| SendError::BufferFull)?;
243+
.enqueue_with_infallible(max_size, meta, f)?;
234244

235245
net_trace!(
236246
"eth:{}: buffer to send {} octets",
@@ -257,7 +267,7 @@ impl<'a> Socket<'a> {
257267
///
258268
/// This function returns `Err(Error::Exhausted)` if the receive buffer is empty.
259269
pub fn recv(&mut self) -> Result<(&[u8], EthMetadata), RecvError> {
260-
let (meta, packet_buf) = self.rx_buffer.dequeue().map_err(|_| RecvError::Exhausted)?;
270+
let (meta, packet_buf) = self.rx_buffer.dequeue()?;
261271

262272
net_trace!(
263273
"eth:{}: receive {} buffered octets",
@@ -290,7 +300,7 @@ impl<'a> Socket<'a> {
290300
///
291301
/// It returns `Err(Error::Exhausted)` if the receive buffer is empty.
292302
pub fn peek(&mut self) -> Result<(&[u8], &EthMetadata), RecvError> {
293-
let (meta, packet_buf) = self.rx_buffer.peek().map_err(|_| RecvError::Exhausted)?;
303+
let (meta, packet_buf) = self.rx_buffer.peek()?;
294304

295305
net_trace!(
296306
"eth:{}: receive {} buffered octets",

0 commit comments

Comments
 (0)