Skip to content

Commit 94768d3

Browse files
committed
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 rust-bitcoin#3174
1 parent fc7e213 commit 94768d3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

io/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,28 @@ impl<T: AsRef<[u8]>> Cursor<T> {
202202
#[inline]
203203
pub fn position(&self) -> u64 { self.pos }
204204

205+
/// Sets the internal position.
206+
///
207+
/// This method allows seeking within the wrapped memory by setting the position.
208+
///
209+
/// Note that setting a position that is larger than the buffer length will cause reads to
210+
/// return no bytes (EOF).
211+
#[inline]
212+
pub fn set_position(&mut self, position: u64) {
213+
self.pos = position;
214+
}
215+
205216
/// Returns the inner buffer.
206217
///
207218
/// This is the whole wrapped buffer, including the bytes already read.
208219
#[inline]
209220
pub fn into_inner(self) -> T { self.inner }
221+
222+
/// Returns a reference to the inner buffer.
223+
///
224+
/// This is the whole wrapped buffer, including the bytes already read.
225+
#[inline]
226+
pub fn inner(&self) -> &T { &self.inner }
210227
}
211228

212229
impl<T: AsRef<[u8]>> Read for Cursor<T> {

0 commit comments

Comments
 (0)