1

I want to call a Redis function with a binary value of [u8] using the redis-rs crate using the following code:

let _ : () = redis::cmd("...").arg("...").arg(data).query(&mut con)?;

However, it results in this error when data is Vec<u8>:

the trait `redis::ToRedisArgs` is not implemented for `&std::vec::Vec<u8>`

And this error when data is [u8]:

the trait `redis::ToRedisArgs` is not implemented for `[u8]`

When looking at the documentation for ToRedisArgs, there should be one implementation for Vec<T> though.

3
  • 1
    There's an implementation for either &[u8] or Vec<u8>, but you apparently tried with [u8] and &Vec<u8>, so you need to dereference or reference as appropriate. Commented Oct 14, 2020 at 11:30
  • 1
    You can also trigger a deref coercion by explicitly specifying the argument type as &[u8] when passing in a &Vec<u8>. Commented Oct 14, 2020 at 11:32
  • Thanks &data[..] worked. Commented Oct 14, 2020 at 11:40

1 Answer 1

1

Here is a reproducible example of your problem:

fn main() {
    let data: &std::vec::Vec<u8> = &Vec::new();
    let _ = redis::cmd("...").arg(data);
}

Compiling it results in this error:

error[E0277]: the trait bound `&std::vec::Vec<u8>: redis::ToRedisArgs` is not satisfied
 --> src/main.rs:3:33
  |
3 |   let _ = redis::cmd("...").arg(data);
  |                                 ^^^^ the trait `redis::ToRedisArgs` is not implemented for `&std::vec::Vec<u8>`
  |
  = help: the following implementations were found:
            <std::vec::Vec<T> as redis::ToRedisArgs>

As the error message states, ToRedisArgs is implemented for Vec<T>, but not &Vec<T>. To get around this, you can clone data to get a Vec<u8>:

fn main() {
    let data: &std::vec::Vec<u8> = &Vec::new();
    let _ = redis::cmd("...").arg(data.clone());
}

Or, you can extract a slice from the vector because ToRedisArgs is implemented for slices:

fn main() {
    let data: &std::vec::Vec<u8> = &Vec::new();
    let _ = redis::cmd("...").arg(data.as_slice());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.