0

I'm learning Rust, and I can't seem to use the FromSql trait from the r2d2_postgres crate.

Based on this recommendation, I tried adding the feature in my TOML file:

r2d2_postgres = {version = "0.18", features = ["postgres/with-chrono-0_4"]}

But then Rust tells me:

feature `postgres/with-chrono-0_4` in dependency `r2d2_postgres` is not allowed to contain slashes
If you want to enable features of a transitive dependency, the direct dependency needs to re-export those features from the `[features]` table.
Code4R7
  • 2,600
  • 1
  • 19
  • 42
  • Technically, this is answered [here](https://stackoverflow.com/questions/59994525/how-do-i-specify-features-for-a-sub-dependency-in-cargo) already, but the answer isn't very good. In any case, [r2d2_postgres has no features](https://docs.rs/crate/r2d2_postgres/0.18.1/source/Cargo.toml.orig) so you can't specify feature flags on that. You'll have to specify `postgres` as a dependency and add the feature flag there. – Caesar Jul 28 '22 at 11:44
  • Thanks to your answer, I've found a workaround for r2d2. I couldn't get the `postgres` crate to work with data coming from `r2d2_postgres`, so I ended up borrowing the algorithms from https://github.com/sfackler/rust-postgres/blob/master/postgres-types/src/chrono_04.rs – Code4R7 Jul 28 '22 at 19:57
  • If you can't get the two to work together, you may have pulled in two different versions of `postgres`. Check `cargo tree --duplicates`. – Caesar Jul 29 '22 at 00:38
  • Using that command, I saw that crate `r2d2_postgres` version 0.18 uses `postgres` 0.19, while I also imported `postgres` 0.18 in my TOML file. Even the compiler complained to me about it: *"expected struct `postgres::types::Type`, found struct `r2d2_postgres::postgres::types::Type`"* and *"perhaps two different versions of crate `postgres_types` are being used?"*. Funny.. – Code4R7 Jul 29 '22 at 09:33
  • You might want to depend on `postres = ">= 0"` or similar. – Caesar Jul 29 '22 at 11:08

1 Answers1

0

To be able to work with both r2d2 and chrono, an extra dependency on the postgres crate is needed. But the postgres crate must be the same version as the r2d2_postgres::postgress crate in order to work together.

Versions can be compared with the output of cargo tree --duplicates.

At this time of writing, Cargo.toml must contain:

[dependencies]
r2d2_postgres = "0.18"
postgres = {version = "0.19", features = ["with-chrono-0_4"]}

Thank you Caesar.

Code4R7
  • 2,600
  • 1
  • 19
  • 42