Skip to main content

AtomicWrite

Struct AtomicWrite 

Source
pub struct AtomicWrite { /* private fields */ }
Expand description

A file being written through a temporary alongside its destination, replaced by an atomic rename only once the content is complete and durable.

Nothing partial ever appears at the destination path. If the process dies mid-write, the destination is untouched and a .strypt-*.tmp file is left behind — visible, obviously incomplete, and adjacent to where it belongs, rather than a silently truncated file the user might publish.

Implementations§

Source§

impl AtomicWrite

Source

pub fn begin( destination: &Path, overwrite: Overwrite, permissions: Permissions, ) -> Result<Self>

Begin writing to destination.

§Errors

StryptError::OutputExists if the destination exists and overwrite is Overwrite::Refuse; StryptError::Io if the temporary file cannot be created.

Source

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

Write bytes into the temporary file.

§Errors

StryptError::Io if the write fails.

Source

pub fn commit(self) -> Result<()>

Flush, synchronise, and atomically move the temporary into place.

The sync_all is not ceremony. Without it the rename can be durable while the content behind it is not, so a crash at the wrong moment leaves a file that exists, has the right name, and contains nothing — which for this tool means a user with a file they believe is a stripped copy of their document.

§Errors

StryptError::Io if syncing or renaming fails. The temporary is removed either way.

Source

pub fn abort(self)

Abandon the write, leaving the destination untouched.

Trait Implementations§

Source§

impl Debug for AtomicWrite

Source§

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

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

impl Drop for AtomicWrite

Source§

fn drop(&mut self)

Removes the temporary if the writer was neither committed nor aborted.

This is the path taken when a handler returns Err mid-write, which is the normal way a fail-closed handler gives up. Without it, every failed strip would litter a partial file next to the user’s document.

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§

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.