Commands

Struct Commands 

Source
pub struct Commands<T: Uniplate, M> { /* private fields */ }
Expand description

A queue of commands (side-effects) to be applied after a successful rule application.

A rule is given a mutable reference to a Commands and can use it to register side-effects. These side-effects are applied in order of registration after the rule itself updates a part of the tree.

§Application

A rule may not be applied due to different reasons, for example:

  • It does not return a new subtree (i.e. it returns None).
  • It returns a new subtree but the resulting Update is not chosen by the user-defined selector function. The function may select a different rule’s update or no update at all.
  • It is part of a lower-priority rule group and a higher-priority rule is applied first.

In these cases, any side-effects which are registered by the rule are not applied and are dropped by the engine.

§Example

use tree_morph::prelude::*;
use uniplate::Uniplate;

#[derive(Debug, Clone, PartialEq, Eq, Uniplate)]
#[uniplate()]
enum Expr {
    A,
    B,
    C,
}

fn rule(cmds: &mut Commands<Expr, bool>, subtree: &Expr, meta: &bool) -> Option<Expr> {
    cmds.transform(Box::new(|t| match t { // A pure transformation (no other side-effects)
        Expr::B => Expr::C,
        _ => t,
    }));
    cmds.mut_meta(Box::new(|m: &mut bool| *m = true)); // Set the metadata to 'true'

    match subtree {
        Expr::A => Some(Expr::B),
        _ => None,
    }
}

// Start with the expression 'A' and a metadata value of 'false'
let engine = EngineBuilder::new()
    .add_rule_group(rule_fns![rule])
    .build();
let (result, meta) = engine.morph(Expr::A, false);

// After applying the rule itself, the commands are applied in order
assert_eq!(result, Expr::C);
assert_eq!(meta, true);

Implementations§

Source§

impl<T: Uniplate, M> Commands<T, M>

Source

pub fn transform(&mut self, f: Box<dyn FnOnce(T) -> T>)

Registers a pure transformation of the whole tree.

In this case, “pure” means that the transformation cannot register additional side-effects. The transformation function is given ownership of the tree and should return the updated tree.

Side-effects are applied in order of registration after the rule is applied.

Source

pub fn mut_meta(&mut self, f: Box<dyn FnOnce(&mut M)>)

Updates the global metadata in-place via a mutable reference.

Side-effects are applied in order of registration after the rule is applied.

Source

pub fn clear(&mut self)

Removes all side-effects previously registered by the rule.

Auto Trait Implementations§

§

impl<T, M> Freeze for Commands<T, M>

§

impl<T, M> !RefUnwindSafe for Commands<T, M>

§

impl<T, M> !Send for Commands<T, M>

§

impl<T, M> !Sync for Commands<T, M>

§

impl<T, M> Unpin for Commands<T, M>

§

impl<T, M> !UnwindSafe for Commands<T, M>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 32 bytes