From dac86caf0e99e84af3d559cdb2a0b458d33580f1 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Tue, 26 Aug 2025 22:56:02 +0200 Subject: [PATCH] io: add `Seek::seek_relative` --- embedded-io/src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/embedded-io/src/lib.rs b/embedded-io/src/lib.rs index 9e2e528a..e1b6fe4d 100644 --- a/embedded-io/src/lib.rs +++ b/embedded-io/src/lib.rs @@ -469,23 +469,59 @@ pub trait Write: ErrorType { } } -/// Blocking seek within streams. +/// Blocking seek within streams.\ +/// +/// The `Seek` trait provides a cursor which can be moved within a stream of +/// bytes. +/// +/// The stream typically has a fixed size, allowing seeking relative to either +/// end or the current offset. /// /// This trait is the `embedded-io` equivalent of [`std::io::Seek`]. pub trait Seek: ErrorType { /// Seek to an offset, in bytes, in a stream. + /// A seek beyond the end of a stream is allowed, but behavior is defined + /// by the implementation. + /// + /// If the seek operation completed successfully, + /// this method returns the new position from the start of the stream. + /// That position can be used later with [`SeekFrom::Start`]. + /// + /// # Errors + /// + /// Seeking can fail, for example because it might involve flushing a buffer. + /// + /// Seeking to a negative offset is considered an error. fn seek(&mut self, pos: SeekFrom) -> Result; /// Rewind to the beginning of a stream. + /// + /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0))`. + /// + /// # Errors + /// + /// Rewinding can fail, for example because it might involve flushing a buffer. fn rewind(&mut self) -> Result<(), Self::Error> { self.seek(SeekFrom::Start(0))?; Ok(()) } /// Returns the current seek position from the start of the stream. + /// + /// This is equivalent to `self.seek(SeekFrom::Current(0))`. fn stream_position(&mut self) -> Result { self.seek(SeekFrom::Current(0)) } + + /// Seeks relative to the current position. + /// + /// This is equivalent to `self.seek(SeekFrom::Current(offset))` but + /// doesn't return the new position which can allow some implementations + /// to perform more efficient seeks. + fn seek_relative(&mut self, offset: i64) -> Result<(), Self::Error> { + self.seek(SeekFrom::Current(offset))?; + Ok(()) + } } /// Get whether a reader is ready.