Skip to content

Commit

Permalink
#74 relay by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 11, 2023
1 parent af43dc7 commit 6f9cad8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
14 changes: 7 additions & 7 deletions src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl Sodg {
/// g.add(0).unwrap();
/// g.add(1).unwrap();
/// g.bind(0, 1, "foo").unwrap();
/// assert!(g.find(0, "bar", DeadRelay {}).is_err());
/// let v = g.find(0, "bar", LambdaRelay::new(|v, a, b| {
/// assert!(g.find(0, "bar", &mut DeadRelay::default()).is_err());
/// let v = g.find(0, "bar", &mut LambdaRelay::new(|v, a, b| {
/// assert_eq!(a, "bar");
/// assert_eq!(b, "");
/// Ok("foo".to_string())
Expand All @@ -83,7 +83,7 @@ impl Sodg {
///
/// If target vertex is not found or `v1` is absent,
/// an `Err` will be returned.
pub fn find<T: Relay + Copy>(&self, v1: u32, loc: &str, mut relay: T) -> Result<u32> {
pub fn find<T: Relay>(&self, v1: u32, loc: &str, relay: &mut T) -> Result<u32> {
let mut v = v1;
let mut locator: VecDeque<String> = VecDeque::new();
loc.split('.')
Expand Down Expand Up @@ -158,7 +158,7 @@ fn finds_with_closure() -> Result<()> {
g.find(
1,
"first.second/abc",
LambdaRelay::new(|v, a, b| {
&mut LambdaRelay::new(|v, a, b| {
if v == 1 && !b.is_empty() {
panic!();
}
Expand All @@ -177,7 +177,7 @@ fn finds_with_closure() -> Result<()> {
fn finds_root() -> Result<()> {
let mut g = Sodg::empty();
g.add(0)?;
assert_eq!(0, g.find(0, "", DeadRelay::default())?);
assert_eq!(0, g.find(0, "", &mut DeadRelay::default())?);
Ok(())
}

Expand All @@ -187,12 +187,12 @@ fn closure_return_absolute_vertex() {
g.add(0).unwrap();
g.add(1).unwrap();
g.bind(0, 1, "foo").unwrap();
assert!(g.find(0, "bar", DeadRelay::new()).is_err());
assert!(g.find(0, "bar", &mut DeadRelay::new()).is_err());
let v = g
.find(
0,
"bar",
LambdaRelay::new(|_v, a, b| {
&mut LambdaRelay::new(|_v, a, b| {
assert_eq!(a, "bar");
assert_eq!(b, "");
Ok("ν1".to_string())
Expand Down
2 changes: 1 addition & 1 deletion src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Sodg {
/// of sub-objects and edges. Mostly used for testing.
pub fn inspect(&self, loc: &str) -> Result<String> {
let v = self
.find(0, loc, DeadRelay {})
.find(0, loc, &mut DeadRelay::default())
.context(format!("Can't locate '{loc}'"))?;
let mut seen = HashSet::new();
Ok(format!(
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) use crate::vertex::Vertex;
/// sodg.bind(0, 1, "a").unwrap();
/// sodg.add(2).unwrap();
/// sodg.bind(1, 2, "b").unwrap();
/// assert_eq!(2, sodg.find(0, "a.b", DeadRelay {}).unwrap());
/// assert_eq!(2, sodg.find(0, "a.b", &mut DeadRelay::default()).unwrap());
/// ```
///
/// This package is used in [reo](https://github.com/objectionary/reo)
Expand Down Expand Up @@ -110,7 +110,6 @@ pub trait Relay {

/// This `Relay` doesn't even try to find anything, but returns
/// an error. If you don't know what relay to use, use [`DeadRelay::new()`].
#[derive(Clone, Copy)]
pub struct DeadRelay {}

/// This `Relay` can be made of a lambda function. The function must
Expand All @@ -119,7 +118,6 @@ pub struct DeadRelay {}
/// of the attribute. The function must return a new locator where the
/// search algorithm must continue. It can be just a name of a new attribute,
/// or an absolute locator with dots inside.
#[derive(Clone, Copy)]
pub struct LambdaRelay {
lambda: fn(u32, &str, &str) -> Result<String>,
}
Expand Down
8 changes: 4 additions & 4 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ fn splits_label_correctly(#[case] a: &str, #[case] head: &str, #[case] tail: &st
fn adds_simple_vertex() -> Result<()> {
let mut g = Sodg::empty();
g.add(1)?;
assert_eq!(1, g.find(1, "", DeadRelay {})?);
assert_eq!(1, g.find(1, "", &mut DeadRelay::default())?);
Ok(())
}

Expand All @@ -302,7 +302,7 @@ fn binds_simple_vertices() -> Result<()> {
g.add(2)?;
let k = "hello";
g.bind(1, 2, k)?;
assert_eq!(2, g.find(1, k, DeadRelay {})?);
assert_eq!(2, g.find(1, k, &mut DeadRelay::default())?);
Ok(())
}

Expand All @@ -313,7 +313,7 @@ fn pre_defined_ids() -> Result<()> {
g.add(2)?;
let k = "a-привет";
g.bind(1, 2, k)?;
assert_eq!(2, g.find(1, k, DeadRelay {})?);
assert_eq!(2, g.find(1, k, &mut DeadRelay::default())?);
Ok(())
}

Expand All @@ -324,7 +324,7 @@ fn binds_two_names() -> Result<()> {
g.add(2)?;
g.bind(1, 2, "first")?;
g.bind(1, 2, "second")?;
assert_eq!(2, g.find(1, "first", DeadRelay {})?);
assert_eq!(2, g.find(1, "first", &mut DeadRelay::default())?);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Sodg {
pub fn slice(&mut self, loc: &str) -> Result<Sodg> {
let mut todo = HashSet::new();
let mut done = HashSet::new();
todo.insert(self.find(0, loc, DeadRelay {})?);
todo.insert(self.find(0, loc, &mut DeadRelay::default())?);
loop {
if todo.is_empty() {
break;
Expand Down

0 comments on commit 6f9cad8

Please sign in to comment.