Skip to main content

GraphTransaction

Struct GraphTransaction 

Source
pub struct GraphTransaction {
    pub label: Option<String>,
    pub ops: Vec<GraphOp>,
}
Expand description

A batch of edit operations that should be applied and undone as one unit.

Fields§

§label: Option<String>

Optional human-readable label for history UI.

§ops: Vec<GraphOp>

Operations in order.

Implementations§

Source§

impl GraphTransaction

Source

pub fn new() -> Self

Creates an empty transaction.

Examples found in repository?
examples/build_graph.rs (line 36)
32fn main() {
33    let node_id = NodeId::from_u128(2);
34    let mut graph = Graph::new(GraphId::from_u128(1));
35
36    let mut tx = GraphTransaction::new().with_label("add source node");
37    tx.push(GraphOp::AddNode {
38        id: node_id,
39        node: make_node("demo.source", 10.0, 20.0),
40    });
41    tx.apply_to(&mut graph).expect("transaction applies");
42
43    assert_eq!(graph.nodes.len(), 1);
44    assert_eq!(graph.nodes[&node_id].kind, NodeKindKey::new("demo.source"));
45}
Source

pub fn from_ops(ops: impl IntoIterator<Item = GraphOp>) -> Self

Creates an unlabeled transaction from ops.

Source

pub fn from_parts( label: Option<String>, ops: impl IntoIterator<Item = GraphOp>, ) -> Self

Creates a transaction from metadata and ops.

Source

pub fn diff(from: &Graph, to: &Graph) -> Self

Builds a deterministic transaction that transforms from into to.

Source

pub fn with_label(self, label: impl Into<String>) -> Self

Sets a label.

Examples found in repository?
examples/build_graph.rs (line 36)
32fn main() {
33    let node_id = NodeId::from_u128(2);
34    let mut graph = Graph::new(GraphId::from_u128(1));
35
36    let mut tx = GraphTransaction::new().with_label("add source node");
37    tx.push(GraphOp::AddNode {
38        id: node_id,
39        node: make_node("demo.source", 10.0, 20.0),
40    });
41    tx.apply_to(&mut graph).expect("transaction applies");
42
43    assert_eq!(graph.nodes.len(), 1);
44    assert_eq!(graph.nodes[&node_id].kind, NodeKindKey::new("demo.source"));
45}
Source

pub fn with_optional_label(self, label: Option<String>) -> Self

Sets or clears the optional label.

Source

pub fn with_ops(self, ops: impl IntoIterator<Item = GraphOp>) -> Self

Adds ops in order and returns this transaction.

Source

pub fn map_ops(self, f: impl FnOnce(Vec<GraphOp>) -> Vec<GraphOp>) -> Self

Transforms this transaction’s ops while preserving metadata.

Source

pub fn push(&mut self, op: GraphOp)

Pushes an op.

Examples found in repository?
examples/build_graph.rs (lines 37-40)
32fn main() {
33    let node_id = NodeId::from_u128(2);
34    let mut graph = Graph::new(GraphId::from_u128(1));
35
36    let mut tx = GraphTransaction::new().with_label("add source node");
37    tx.push(GraphOp::AddNode {
38        id: node_id,
39        node: make_node("demo.source", 10.0, 20.0),
40    });
41    tx.apply_to(&mut graph).expect("transaction applies");
42
43    assert_eq!(graph.nodes.len(), 1);
44    assert_eq!(graph.nodes[&node_id].kind, NodeKindKey::new("demo.source"));
45}
Source

pub fn extend(&mut self, ops: impl IntoIterator<Item = GraphOp>)

Extends this transaction with ops in order.

Source

pub fn retain_ops(&mut self, f: impl FnMut(&GraphOp) -> bool)

Retains ops that match f.

Source

pub fn clear_ops(&mut self)

Removes all ops while preserving transaction metadata.

Source

pub fn is_empty(&self) -> bool

Source

pub fn label(&self) -> Option<&str>

Returns the optional human-readable label.

Source

pub fn ops(&self) -> &[GraphOp]

Returns ops in order.

Source

pub fn into_ops(self) -> Vec<GraphOp>

Consumes this transaction and returns its ops in order.

Source

pub fn into_parts(self) -> (Option<String>, Vec<GraphOp>)

Consumes this transaction and returns its metadata and ops.

Source

pub fn len(&self) -> usize

Returns the number of ops.

Source

pub fn apply_to(&self, graph: &mut Graph) -> Result<(), ApplyError>

Applies this transaction atomically to graph.

Examples found in repository?
examples/build_graph.rs (line 41)
32fn main() {
33    let node_id = NodeId::from_u128(2);
34    let mut graph = Graph::new(GraphId::from_u128(1));
35
36    let mut tx = GraphTransaction::new().with_label("add source node");
37    tx.push(GraphOp::AddNode {
38        id: node_id,
39        node: make_node("demo.source", 10.0, 20.0),
40    });
41    tx.apply_to(&mut graph).expect("transaction applies");
42
43    assert_eq!(graph.nodes.len(), 1);
44    assert_eq!(graph.nodes[&node_id].kind, NodeKindKey::new("demo.source"));
45}
Source

pub fn inverse(&self) -> Self

Builds the inverse transaction for undo/redo.

Trait Implementations§

Source§

impl Clone for GraphTransaction

Source§

fn clone(&self) -> GraphTransaction

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GraphTransaction

Source§

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

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

impl Default for GraphTransaction

Source§

fn default() -> GraphTransaction

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for GraphTransaction

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for GraphTransaction

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.