Skip to content

Commit

Permalink
#49: new macro: copy
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 27, 2022
1 parent c462265 commit 66d31db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
44 changes: 42 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,60 @@ macro_rules! da {
};
}

/// Add a new vertex to the universe and return its ID:
///
/// ```
/// use reo::universe::Universe;
/// use reo::add;
/// let mut uni = Universe::empty();
/// let v1 = add!(uni);
/// ```
#[macro_export]
macro_rules! add {
($uni:expr) => {{
let v = $uni.next_v();
$uni.add(v)?;
$uni.add(v).unwrap();
v
}};
}

/// Adds a new edge between two vertices:
///
/// ```
/// use reo::universe::Universe;
/// use reo::{add, bind};
/// let mut uni = Universe::empty();
/// let v1 = add!(uni);
/// let v2 = add!(uni);
/// let e = bind!(uni, v1, v2, "foo");
/// ```
#[macro_export]
macro_rules! bind {
($uni:expr, $v1:expr, $v2:expr, $a:expr) => {{
let e = $uni.next_e();
$uni.bind(e, $v1, $v2, $a)?;
$uni.bind(e, $v1, $v2, $a).unwrap();
e
}};
}

/// Makes a copy of an existing vertex, by looking at the edge
/// that leads to it:
///
/// ```
/// use reo::universe::Universe;
/// use reo::{add, bind, copy};
/// let mut uni = Universe::empty();
/// let v1 = add!(uni);
/// let v2 = add!(uni);
/// let e = bind!(uni, v1, v2, "foo");
/// copy!(uni, e);
/// ```
#[macro_export]
macro_rules! copy {
($uni:expr, $e1:expr) => {{
let v3 = $uni.next_v();
let e2 = $uni.next_e();
$uni.copy($e1, v3, e2).unwrap();
v3
}};
}
6 changes: 2 additions & 4 deletions src/universe/i_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Universe {
}

#[cfg(test)]
use crate::{add, bind};
use crate::{add, bind, copy};

#[test]
fn makes_simple_copy() -> Result<()> {
Expand All @@ -85,9 +85,7 @@ fn makes_simple_copy() -> Result<()> {
let e1 = bind!(uni, v1, v2, "x");
let v4 = add!(uni);
bind!(uni, v2, v4, "y");
let v3 = uni.next_v();
let e2 = uni.next_e();
uni.copy(e1, v3, e2)?;
copy!(uni, e1);
assert!(uni.inconsistencies().is_empty());
assert_eq!(v2, uni.find(v1, "x.π")?);
assert_eq!(v4, uni.find(v1, "x.y")?);
Expand Down

0 comments on commit 66d31db

Please sign in to comment.