reopen

Struct Reopen

Source
pub struct Reopen<FD> { /* private fields */ }
Expand description

A Read/Write proxy that can reopen the underlying object.

It is constructed with a function that can open a new instance of the object. If it is signaled to reopen it (though handle), it drops the old instance and uses the function to create a new one at the next IO operation.

§Error handling

The reopening is performed lazily, on the first operation done to the object. Opening a new instance can fail with an error. If this happens, the error is returned as part of the operation being performed ‒ therefore, you can get an error like File not found while performing read.

If an error happens, the operation is aborted. Next time an operation is performed, another attempt to open the object is made (which in turn can fail again).

§Scheduling of a reopen

The implementation tries to ensure whole operations happen on the same FD. For example, even if multiple read calls need to be performed as part of read_exact, the Reopen will check for reopening flags only once before the whole operation and then will keep the same FD.

If this is not enough, the Reopen can be locked to bundle multiple operations without reopening.

§Handling of ends

Certain operations make ordinary file descriptors „finished“ ‒ for example, read_to_end. Usually, further calls to any read operations would produce EOF from then on.

While this reaches the end of the currently opened FD and further read operations would still produce EOF, reopening the FD may lead to it being readable again. Therefore, reaching EOF is not necessarily final for Reopen.

Implementations§

Source§

impl<FD> Reopen<FD>

Source

pub fn new( constructor: Box<dyn Fn() -> Result<FD, Error> + Send>, ) -> Result<Self, Error>

Creates a new instance.

Source

pub fn with_handle( handle: Handle, constructor: Box<dyn Fn() -> Result<FD, Error> + Send>, ) -> Result<Self, Error>

Creates a new instance from the given handle.

This might come useful if you want to create the handle beforehand with Handle::stub (eg. in once_cell).

Note that using the same handle for multiple Reopens will not work as expected (the first one to be used resets the signal and the others don’t reopen).

§Examples
// Something that implements `Write`, for example.
struct Writer;

let handle = Handle::stub();
let reopen = Reopen::with_handle(handle.clone(), Box::new(|| Ok(Writer)));

handle.reopen();
Source

pub fn handle(&self) -> Handle

Returns a handle to signal this Reopen to perform the reopening.

Source

pub fn lock(&mut self) -> Result<&mut FD, Error>

Lock the Reopen against reopening in the middle of operation.

In case of needing to perform multiple operations without reopening in the middle, it can be locked by this method. This provides access to the inner FD.

§Errors

This can result in an error in case the FD needs to be reopened (or wasn’t opened previously) and the reopening results in an error.

§Examples
let mut writer = Reopen::new(Box::new(|| {
    // Vec::<u8> is an in-memory writer
    Ok(Vec::new())
}))?;
let handle = writer.handle();
let mut lock = writer.lock()?;
write!(&mut lock, "Hello ")?;

// Request reopening. But as we locked, it won't happen until we are done with it.
handle.reopen();

write!(&mut lock, "world")?;

// See? Both writes are here now.
assert_eq!(b"Hello world", &lock[..]);

// But when we return to using the writer directly (and drop the lock by that), it gets
// reopened and we get a whole new Vec to play with.
write!(&mut writer, "Another message")?;
assert_eq!(b"Another message", &writer.lock()?[..]);

Trait Implementations§

Source§

impl<FD: Debug> Debug for Reopen<FD>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
Source§

impl<FD: Read> Read for Reopen<FD>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

impl<FD: Write> Write for Reopen<FD>

Source§

fn flush(&mut self) -> Result<(), Error>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<FD> Freeze for Reopen<FD>
where FD: Freeze,

§

impl<FD> !RefUnwindSafe for Reopen<FD>

§

impl<FD> Send for Reopen<FD>
where FD: Send,

§

impl<FD> !Sync for Reopen<FD>

§

impl<FD> Unpin for Reopen<FD>
where FD: Unpin,

§

impl<FD> !UnwindSafe for Reopen<FD>

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.