Skip to content

Commit

Permalink
#75 immutable relay
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 12, 2023
1 parent 6c0f77d commit 967e911
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
17 changes: 7 additions & 10 deletions src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::collections::VecDeque;
use std::str::FromStr;

impl Relay for DeadRelay {
fn re(&mut self, v: u32, a: &str, b: &str) -> Result<String> {
fn re(&self, v: u32, a: &str, b: &str) -> Result<String> {
Err(anyhow!("Can't find {a}/{b} at ν{v}"))
}
}
Expand Down Expand Up @@ -56,7 +56,7 @@ impl LambdaRelay {
}

impl Relay for LambdaRelay {
fn re(&mut self, v: u32, a: &str, b: &str) -> Result<String> {
fn re(&self, v: u32, a: &str, b: &str) -> Result<String> {
(self.lambda)(v, a, b)
}
}
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>(&self, v1: u32, loc: &str, relay: &mut T) -> Result<u32> {
pub fn find<T: Relay>(&self, v1: u32, loc: &str, relay: &T) -> Result<u32> {
let mut v = v1;
let mut locator: VecDeque<String> = VecDeque::new();
loc.split('.')
Expand Down Expand Up @@ -213,22 +213,19 @@ impl FakeRelay {
pub fn new(g: Sodg) -> FakeRelay {
FakeRelay { g }
}
pub fn find(&mut self, _k: &str) -> Result<u32> {
// self.g.find(0, k, self)
// if you uncomment the line above, the code won't compile
Ok(self.g.next_id())
pub fn find(&mut self, k: &str) -> Result<u32> {
self.g.find(0, k, self)
}
}

#[cfg(test)]
impl Relay for FakeRelay {
fn re(&mut self, _v: u32, _a: &str, _b: &str) -> Result<String> {
Ok("bar".to_string())
fn re(&self, _v: u32, _a: &str, _b: &str) -> Result<String> {
Ok("ν1".to_string())
}
}

#[test]
#[ignore]
fn relay_modifies_sodg_back() -> Result<()> {
let mut g = Sodg::empty();
g.add(0).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Sodg {
/// The function is mostly used for testing.
pub fn inspect(&self, loc: &str) -> Result<String> {
let v = self
.find(0, loc, &mut DeadRelay::default())
.find(0, loc, &DeadRelay::default())
.context(format!("Can't locate '{loc}'"))?;
let mut seen = HashSet::new();
Ok(format!(
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct Sodg {
/// Pretty much anything that the relay returns will be used
/// as a new search string, starting from the `v` vertex.
pub trait Relay {
fn re(&mut self, v: u32, a: &str, b: &str) -> Result<String>;
fn re(&self, v: u32, a: &str, b: &str) -> Result<String>;
}

/// This `Relay` doesn't even try to find anything, but returns
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, &mut DeadRelay::default())?);
todo.insert(self.find(0, loc, &DeadRelay::default())?);
loop {
if todo.is_empty() {
break;
Expand Down

0 comments on commit 967e911

Please sign in to comment.