Skip to content

Why does predicate::str::diff require a 'static input? #146

Description

@zackw

Most of the string predicates accept an argument that can vary at runtime (usually as Into<String>) but str::diff asks instead for an argument that is Into<Cow<'static, str>>, which is not satisfied by strings created at runtime. This means I can't do something like

use std::str;

use assert_cmd::Command;
use predicates::prelude::*;

fn my_program() -> Command {
    Command::cargo_bin("my_program").unwrap()
}

#[test]
fn help_options() {
    let help_output =
        my_program().arg("--help").assert()
        .success().stderr("").get_output().to_owned();

    my_program().arg("-h").assert()
        .success()
        .stderr("")
        .stdout(predicate::str::diff(
            str::from_utf8(&help_output.stdout).unwrap()
        ));
}

I don't see why this couldn't be made to work, but with the current implementation it's a compile-time error:

error[E0277]: the trait bound `std::borrow::Cow<'static, str>: std::convert::From<std::vec::Vec<u8>>` is not satisfied
  --> my_crate/tests/cli_tests.rs:26:38
   |
26 |         .stdout(predicate::str::diff(help_status.get_output().stdout));
   |                 -------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<std::vec::Vec<u8>>` is not implemented for `std::borrow::Cow<'static, str>`
   |                 |
   |                 required by a bound introduced by this call
   |

Meta

predicates-rs version: 3.0.3
rustc --version --verbose:

rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: aarch64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions