From 94768d3f7094297e807f14e553e5b981469ca223 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Thu, 15 Aug 2024 21:43:01 +0200 Subject: [PATCH] Add `set_position` method to `Cursor` Without the ability to seek, the `Cursor` type is only useful for wrapping owned buffers. In `std` the equivalent type also allows seeking. We currently do not have the `Seek` trait so this simply adds a method to set the position. Further, this adds a method to access inner type so seeking from the end can be implemented (to compute the position relative to the end). Closes #3174 --- io/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/io/src/lib.rs b/io/src/lib.rs index 581d73289..8de9f28d9 100644 --- a/io/src/lib.rs +++ b/io/src/lib.rs @@ -202,11 +202,28 @@ impl> Cursor { #[inline] pub fn position(&self) -> u64 { self.pos } + /// Sets the internal position. + /// + /// This method allows seeking within the wrapped memory by setting the position. + /// + /// Note that setting a position that is larger than the buffer length will cause reads to + /// return no bytes (EOF). + #[inline] + pub fn set_position(&mut self, position: u64) { + self.pos = position; + } + /// Returns the inner buffer. /// /// This is the whole wrapped buffer, including the bytes already read. #[inline] pub fn into_inner(self) -> T { self.inner } + + /// Returns a reference to the inner buffer. + /// + /// This is the whole wrapped buffer, including the bytes already read. + #[inline] + pub fn inner(&self) -> &T { &self.inner } } impl> Read for Cursor {