pub struct PositionGuard<'a, S>{ /* private fields */ }Expand description
Guard that restores a seekable stream to its original position.
PositionGuard captures the stream position when it is created and seeks
back to that position when the guard is dropped, unless
PositionGuard::restore or PositionGuard::dismiss has already
completed. Drop-time restoration errors are ignored because Drop::drop
cannot return a Result; call PositionGuard::restore when the error
must be observed.
§Examples
use std::io::{
Cursor,
Seek,
SeekFrom,
};
use qubit_io::PositionGuard;
let mut stream = Cursor::new(b"abcdef".to_vec());
stream.seek(SeekFrom::Start(2))?;
{
let mut guard = PositionGuard::new(&mut stream)?;
guard.get_mut().seek(SeekFrom::End(0))?;
}
assert_eq!(2, stream.position());Implementations§
Source§impl<'a, S> PositionGuard<'a, S>
impl<'a, S> PositionGuard<'a, S>
Sourcepub fn new(stream: &'a mut S) -> Result<Self>
pub fn new(stream: &'a mut S) -> Result<Self>
Captures the current position of stream.
§Parameters
stream: Seekable stream to guard.
§Returns
A guard that will restore the captured position on drop.
§Errors
Returns the error reported by Seek::stream_position when the current
position cannot be read.
Sourcepub fn restore(&mut self) -> Result<()>
pub fn restore(&mut self) -> Result<()>
Restores the captured position immediately.
After a successful restore, drop-time restoration is disabled. If restoring fails, drop will still make a best-effort restore attempt.
§Errors
Returns the error reported by Seek::seek when the stream cannot seek
back to the captured position.