Should I use Diesel's R2D2 for non-Diesel things?


I am using both Diesel and Redis and I want to use R2D2 to manage the connection pools for both. Diesel brings its own R2D2 so now I have two options that I am uncomfortable with:

  1. Use two versions of R2D2. The one bundled with Diesel for Diesel and one in my Cargo.toml for Redis.
  2. Use the R2D2 bundled with Diesel for both Diesel and Redis and pray Diesel R2D2 is compatible with r2d2_redis.

Here are the relevant lines in my [dependencies]:

diesel = { version = "1.2", features = ["r2d2", "postgres", "uuid"] }
# r2d2_redis = "0.7"


diesel does not bundle R2D2, but has it’s own integration deployed as diesel::r2d2, gated behind a feature.

This is the line in diesel/ that imports the external crate, r2d2:

Therefore, you won’t end up with two copies of r2d2, but just reviled from implementing integration of diesel<->r2d2 yourself. So, I would say: enjoy it!

  1. If your semver requirements on r2d2 overlap with diesel’s then you will have a singular version. If they are not compatible you will have two. The majority of rust crates work fine with multiple versions in the dependencies, and Rust was designed to make doing this easy. For example even if you did have multiple versions of the same crate their types shouldn’t be compatible and won’t compile if you mix both versions. The main thing that breaks multi version compatibility is if there is something that must be a singleton, which Rust tries to discourage. I doubt there’s anything requiring a singleton within r2d2.

  2. As previously pointed out diesel just reexports r2d2 + some of it’s own integration functions, so diesel::r2d2 should be fully functional.

But note that r2d2_redis itself depends on r2d2. So it’s semver constraints aren’t compatible with either your crate or diesel’s then you’ll have multiple versions again. But like I said in 1 that shouldn’t cause any issues.