Skip to main content

PositionGuard

Struct PositionGuard 

Source
pub struct PositionGuard<'a, S>
where S: Seek + ?Sized,
{ /* 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>
where S: Seek + ?Sized,

Source

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.

Source

pub fn position(&self) -> u64

Returns the captured stream position.

§Returns

The position captured when this guard was created.

Source

pub fn get_mut(&mut self) -> &mut S

Returns a mutable reference to the guarded stream.

§Returns

The guarded stream reference.

Source

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.

Source

pub fn dismiss(self)

Disables drop-time restoration without moving the stream.

This is useful when the caller intentionally wants to keep the stream at its current position.

Trait Implementations§

Source§

impl<S> Drop for PositionGuard<'_, S>
where S: Seek + ?Sized,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'a, S> Freeze for PositionGuard<'a, S>
where S: ?Sized,

§

impl<'a, S> RefUnwindSafe for PositionGuard<'a, S>
where S: RefUnwindSafe + ?Sized,

§

impl<'a, S> Send for PositionGuard<'a, S>
where S: Send + ?Sized,

§

impl<'a, S> Sync for PositionGuard<'a, S>
where S: Sync + ?Sized,

§

impl<'a, S> Unpin for PositionGuard<'a, S>
where S: ?Sized,

§

impl<'a, S> UnsafeUnpin for PositionGuard<'a, S>
where S: ?Sized,

§

impl<'a, S> !UnwindSafe for PositionGuard<'a, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.