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

Why does .name(str) require a 'static lifetime string? #150

Open
demmerichs opened this issue Aug 29, 2023 · 1 comment
Open

Why does .name(str) require a 'static lifetime string? #150

demmerichs opened this issue Aug 29, 2023 · 1 comment
Labels
breaking-change enhancement Improve the expected question Uncertainty is involved

Comments

@demmerichs
Copy link

I want to add names to predicates I construct inside a loop, e.g.

use predicates::{prelude::*, BoxPredicate};

fn test_predicates() {
    let gt = vec![1, 2, 3];
    let mut predicate: BoxPredicate<str> = BoxPredicate::new(predicate::always());
    for g in gt {
        let name = format!("{}", g);
        predicate = BoxPredicate::new(predicate.and(predicate::never().name(&name)));
    }
    assert!(predicate.eval("Hello World!"));
}

I expected to see this happen:
Allow me to pass Strings, or &str which get cloned inside the naming.

Instead this happened:
Compile Error:

   |
8  |         let name = format!("{}", g);
   |             ---- binding `name` declared here
9  |         predicate = BoxPredicate::new(predicate.and(predicate::never().name(&name)));
   |                                                     ------------------------^^^^^-
   |                                                     |                       |
   |                                                     |                       borrowed value does not live long enough
   |                                                     argument requires that `name` is borrowed for `'static`
10 |     }
   |     - `name` dropped here while still borrowed

Meta

predicates-rs version: 3.0.3
rustc --version --verbose:
rustc 1.71.1 (eb26296b5 2023-08-03)
binary: rustc
commit-hash: eb26296b556cef10fb713a38f3d16b9886080f26
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.71.1
LLVM version: 16.0.5

@epage
Copy link
Contributor

epage commented Aug 29, 2023

Reason: a lot of the features were developed for basic test cases, like with assert_cmd and assert_fs where dynamic data isn't as needed.

That doesn't mean it can't change though it would involve a breaking change.

@epage epage added enhancement Improve the expected question Uncertainty is involved breaking-change labels Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change enhancement Improve the expected question Uncertainty is involved
Projects
None yet
Development

No branches or pull requests

2 participants