Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data field in runner to solve hook ownership issues #337

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Bastacyclop
Copy link
Contributor

@Bastacyclop Bastacyclop commented Oct 22, 2024

I ran into the following problem a couple of times: you try to use some owned value inside the Runner hook, but you can't afford to move it into the 'static lifetime closure.

Here's a simple example:

fn runner_issue<W>(mut out: W)
    where W: std::io::Write,
{
    let runner = Runner::<SymbolLang, ()>::default()
        .with_hook(move |r| {
            writeln!(out, "{} iterations", r.iterations.len()).unwrap();
            Ok(())
        });

    writeln!(out, "{} iterations", runner.iterations.len()).unwrap();
}

Which gives the following error:

> cargo build
   Compiling egg v0.9.5 (/home/thomas/egg)
error[E0310]: the parameter type `W` may not live long enough
    --> src/run.rs:1009:18
     |
1009 |       let runner = Runner::<SymbolLang, ()>::default()
     |  __________________^
1010 | |         .with_hook(move |r| {
1011 | |             writeln!(out, "{} iterations", r.iterations.len()).unwrap();
1012 | |             Ok(())
1013 | |         });
     | |          ^
     | |          |
     | |__________the parameter type `W` must be valid for the static lifetime...
     |            ...so that the type `W` will meet its required lifetime bounds
     |
help: consider adding an explicit lifetime bound
     |
1007 |     where W: std::io::Write + 'static,
     |                             +++++++++

error[E0382]: borrow of moved value: `out`
    --> src/run.rs:1015:14
     |
1006 | fn runner_issue<W>(mut out: W)
     |                    ------- move occurs because `out` has type `W`, which does not implement the `Copy` trait
...
1010 |         .with_hook(move |r| {
     |                    -------- value moved into closure here
1011 |             writeln!(out, "{} iterations", r.iterations.len()).unwrap();
     |                      --- variable moved due to use in closure
...
1015 |     writeln!(out, "{} iterations", runner.iterations.len()).unwrap();
     |              ^^^ value borrowed here after move
     |
help: if `W` implemented `Clone`, you could clone the value
    --> src/run.rs:1006:17
     |
1006 | fn runner_issue<W>(mut out: W)
     |                 ^ consider constraining this type parameter with `Clone`
...
1011 |             writeln!(out, "{} iterations", r.iterations.len()).unwrap();
     |                      --- you could clone this value
help: consider further restricting this bound
     |
1007 |     where W: std::io::Write + Copy,
     |                             ++++++

This PR adds a custom data field to the Runner, so that any value can be moved in and out of the runner, and therefore in and out of the hook closure:

fn runner_issue_solved<W>(out: W)
    where W: std::io::Write,
{
    let mut runner = Runner::<SymbolLang, (), (), W>::new((), out)
        .with_hook(move |r| {
            writeln!(r.data, "{} iterations", r.iterations.len()).unwrap();
            Ok(())
        });

    writeln!(runner.data, "{} iterations", runner.iterations.len()).unwrap();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant