From a3c57e9336955a59beccccf499765298c45deb1a Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Tue, 15 Jul 2025 22:12:02 -0600 Subject: [PATCH] EndpointRead: Error type for EndpointReadUntilShortPacket::consume_end --- src/io/read.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/io/read.rs b/src/io/read.rs index 4b69b0f..d9d01d0 100644 --- a/src/io/read.rs +++ b/src/io/read.rs @@ -1,4 +1,5 @@ use std::{ + error::Error, io::{BufRead, Read}, time::Duration, }; @@ -384,6 +385,19 @@ pub struct EndpointReadUntilShortPacket<'a, EpType: BulkOrInterrupt> { reader: &'a mut EndpointRead, } +/// Error returned by [`EndpointReadUntilShortPacket::consume_end()`] +/// when the reader is not at the end of a short packet. +#[derive(Debug)] +pub struct ExpectedShortPacket; + +impl std::fmt::Display for ExpectedShortPacket { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "expected short packet") + } +} + +impl Error for ExpectedShortPacket {} + impl EndpointReadUntilShortPacket<'_, EpType> { /// Check if the underlying endpoint has reached the end of a short packet. /// @@ -402,12 +416,12 @@ impl EndpointReadUntilShortPacket<'_, EpType> { /// to read the next message. /// /// Returns an error and does nothing if the reader [is not at the end of a short packet](Self::is_end). - pub fn consume_end(&mut self) -> Result<(), ()> { + pub fn consume_end(&mut self) -> Result<(), ExpectedShortPacket> { if self.is_end() { self.reader.reading.as_mut().unwrap().clear_short_packet(); Ok(()) } else { - Err(()) + Err(ExpectedShortPacket) } } }