R2D2 Redis deref to wrong type


#1

I’m trying to set up a test where I have a Redis connection pool and use it inside a Rocket app. The only connection pool I’ve found is R2D2 which has a Redis library. Anwyays when I try and dereferences the connection, its type is redis::connection::Connection instead of redis::Connection.

There is no redis::connection::Connection according to the Redis documentation from what I can tell and I’m not sure how to get the proper connection. Anyways here is some really basic code for what I’m doing:

#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate rocket;
extern crate redis;
extern crate time;
extern crate serde;
extern crate r2d2;
extern crate r2d2_redis;

#[macro_use]
extern crate lazy_static;

use std::io::Cursor;
use std::default::Default;
use std::ops::Deref;
use redis::Commands;
use rocket::response::Response;
use rocket::http::{Status};
use rocket::{State};
use r2d2_redis::RedisConnectionManager;

type Pool = r2d2::Pool<RedisConnectionManager>;

lazy_static! {
    pub static ref DB_POOL: r2d2::Pool<RedisConnectionManager> = {
        let config = Default::default();
        let manager = RedisConnectionManager::new("redis://localhost").unwrap();
        r2d2::Pool::new(config, manager).expect("expect db")
    };
}

/// Initializes a database pool.
fn init_pool() -> Pool {
    let config = Default::default();
    let manager = RedisConnectionManager::new("redis://localhost").unwrap();
    r2d2::Pool::new(config, manager).expect("expect db")
}

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[get("/<key>")]
fn key_route(key: String, pool: State<Pool>) -> Result<Response<'static>, Status> {

    // let conn = pool.get().unwrap();
    let conn = DB_POOL.get().unwrap();
    let reply = redis::cmd("PING").query::<String>(conn.deref()).unwrap();

    Response::build()
        .status(Status::Ok)
        .sized_body(Cursor::new("nothing to see here"))
        .ok()
}

fn main() {

    rocket::ignite()
        .manage(init_pool())
        .mount("/", routes![index, key_route])
        .launch();

}

I get the error:

error[E0277]: the trait bound `redis::connection::Connection: redis::ConnectionLike` is not satisfied
  --> src\main.rs:50:52
   |
50 |     let reply = redis::cmd("PING").query::<String>(conn.deref()).unwrap();
   |                                                    ^^^^^^^^^^^^ the trait `redis::ConnectionLike` is not imp
lemented for `redis::connection::Connection`
   |
   = note: required for the cast to the object type `redis::ConnectionLike`

Any help on how to solve this would be much appreciated.


#2

One thing to check first is the versions of the libs you’re using, and make sure they’re recent (and compatible).


#3

Thanks vitalyd!

I did notice looking at the r2d2 Redis cargo that it had a more up to date version of the redis-rs library than I had. Sure enough installing the latest redis-rs solved the problem. The redis-rs github page has an old version listed on it and I blindly copy and pasted it, that’ll teach me.


#4

Gotta love the easy fixes! :slight_smile:


#5

Ah, that slipped through after the last released. I fixed it now