Skip to main content

Undo

Struct Undo 

pub struct Undo<T, F>
where F: FnOnce(T),
{ /* private fields */ }
Expand description

An RAII guard to rollback and undo something on (early) drop.

Dereferences to its inner T and its undo function is given the T on drop.

When all of the changes that need to happen together have happened, you can call Undo::commit to disable the guard and commit the associated side effects.

§Example

use std::cell::Cell;
use wasmtime_internal_core::{error::Result, undo::Undo};

/// Some big ball of state that must always be coherent.
pub struct Context {
    // ...
}

impl Context {
    /// Perform some incremental mutation to `self`, which might not leave
    /// it in a valid state unless its whole batch of work is completed.
    fn do_thing(&mut self, arg: u32) -> Result<()> {
        // ...
    }

    /// Undo the side effects of `self.do_thing(arg)` for when we need to
    /// roll back mutations.
    fn undo_thing(&mut self, arg: u32) {
        // ...
    }

    /// Call `self.do_thing(arg)` for each `arg` in `args`.
    ///
    /// However, if any `self.do_thing(arg)` call fails, make sure that
    /// we roll back to the original state by calling `self.undo_thing(arg)`
    /// for all the `self.do_thing(arg)` calls that already succeeded. This
    /// way we never leave `self` in a state where things got half-done.
    pub fn do_all_or_nothing(&mut self, args: &[u32]) -> Result<()> {
        // Counter for our progress, so that we know how much to work undo upon
        // failure.
        let num_things_done = Cell::new(0);

        // Wrap the `Context` in an `Undo` that rolls back our side effects if
        // we early-exit this function via `?`-propagation or panic unwinding.
        let mut ctx = Undo::new(self, |ctx| {
            for arg in args.iter().take(num_things_done.get()) {
                ctx.undo_thing(*arg);
            }
        });

        // Do each piece of work!
        for arg in args {
            // Note: if this call returns an error that is `?`-propagated or
            // triggers unwinding by panicking, then the work performed thus
            // far will be rolled back when `ctx` is dropped.
            ctx.do_thing(*arg)?;

            // Update how much work has been completed.
            num_things_done.set(num_things_done.get() + 1);
        }

        // We completed all of the work, so commit the `Undo` guard and
        // disable its cleanup function.
        Undo::commit(ctx);

        Ok(())
    }
}

Implementations§

§

impl<T, F> Undo<T, F>
where F: FnOnce(T),

pub fn new(inner: T, undo: F) -> Undo<T, F>

Create a new Undo guard.

This guard will wrap the given inner object and call undo(inner) when dropped, unless the guard is disabled via Undo::commit.

pub fn commit(guard: Undo<T, F>) -> T

Disable this Undo and return its inner value.

This Undo’s cleanup function will never be called.

Trait Implementations§

§

impl<T, F> Debug for Undo<T, F>
where F: FnOnce(T), T: Debug,

§

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

Formats the value using the given formatter. Read more
§

impl<T, F> Deref for Undo<T, F>
where F: FnOnce(T),

§

type Target = T

The resulting type after dereferencing.
§

fn deref(&self) -> &<Undo<T, F> as Deref>::Target

Dereferences the value.
§

impl<T, F> DerefMut for Undo<T, F>
where F: FnOnce(T),

§

fn deref_mut(&mut self) -> &mut <Undo<T, F> as Deref>::Target

Mutably dereferences the value.
§

impl<T, F> Drop for Undo<T, F>
where F: FnOnce(T),

§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, F> Freeze for Undo<T, F>
where T: Freeze, F: Freeze,

§

impl<T, F> RefUnwindSafe for Undo<T, F>

§

impl<T, F> Send for Undo<T, F>
where T: Send, F: Send,

§

impl<T, F> Sync for Undo<T, F>
where T: Sync, F: Sync,

§

impl<T, F> Unpin for Undo<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnwindSafe for Undo<T, F>
where T: UnwindSafe, F: UnwindSafe,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.