georust / geozero

Zero-Copy reading and writing of geospatial data.
Apache License 2.0
321 stars 30 forks source link

Trait error using sqlx example #204

Open nk9 opened 3 months ago

nk9 commented 3 months ago

I'm using the SQLX geo-types example, and it's failing. Has the required code changed?

use geo::{Geometry, Point};

let geom: Geometry<f64> = Point::new(10.0, 20.0).into();
let _ = sqlx::query(
    "INSERT INTO point2d (datetimefield,geom) VALUES(now(),ST_SetSRID($1,4326))",
)
.bind(wkb::Encode(geom))
.execute(&pool)
.await?;
 1  error[E0277]: the trait bound `geozero::wkb::Encode<geo::Geometry>: sqlx::Encode<'_, _>` is not satisfied
    --> rust/load_all.rs:245:19
     |
 245 |             .bind(wkb::Encode(geom))
     |              ---- ^^^^^^^^^^^^^^^^^ the trait `sqlx::Encode<'_, _>` is not implemented for `geozero::wkb::Encode<geo::Geometry>`
     |              |
     |              required by a bound introduced by this call
     |
     = help: the following other types implement trait `sqlx::Encode<'q, DB>`:
               <bool as sqlx::Encode<'q, sqlx::Any>>
               <bool as sqlx::Encode<'_, Postgres>>
               <i8 as sqlx::Encode<'_, Postgres>>
               <i16 as sqlx::Encode<'q, sqlx::Any>>
               <i16 as sqlx::Encode<'_, Postgres>>
               <i32 as sqlx::Encode<'q, sqlx::Any>>
               <i32 as sqlx::Encode<'_, Postgres>>
               <i64 as sqlx::Encode<'q, sqlx::Any>>
             and 34 others
 note: required by a bound in `Query::<'q, DB, <DB as HasArguments<'q>>::Arguments>::bind`
    --> /Users/nick/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sqlx-core-0.7.3/src/query.rs:79:32
     |
 79  |     pub fn bind<T: 'q + Send + Encode<'q, DB> + Type<DB>>(mut self, value: T) -> Self {
     |                                ^^^^^^^^^^^^^^ required by this bound in `Query::<'q, DB, <DB as HasArguments<'q>>::Arguments>::bind`
nk9 commented 3 months ago

Ah, this works if you install the required feature:

cargo add geozero --features with-postgis-sqlx

Is it possible to provide an error that points out the problem in this situation? I'm sure I've seen other crates where the compiler suggests adding the feature(s) that contain the code needed to make yours compile. Either way, it would be great if this requirement were mentioned in the README.

michaelkirk commented 3 months ago

Can you point me to a crate that recommends missing features? Sounds interesting.On Mar 16, 2024, at 09:11, Nick Kocharhook @.***> wrote: Ah, this works if you install the required feature: cargo add geozero --features with-postgis-sqlx

Is it possible to provide an error that points out the problem in this situation? I'm sure I've seen other crates where the compiler suggests adding the feature(s) that contain the code needed to make yours compile. Either way, it would be great if this was mentioned in the README.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

nk9 commented 3 months ago

Here's an example, not sure if it's applicable? The error looks like this:

error: The default runtime flavor is `multi_thread`, but the `rt-multi-thread` feature is disabled.
  --> rust/load_all.rs:32:1
   |
32 | #[tokio::main]
   | ^^^^^^^^^^^^^^
   |
   = note: this error originates in the attribute macro `tokio::main` (in Nightly builds, run with -Z macro-backtrace for more info)

You can see it for yourself by installing tokio with no features and creating a file with just this code:

#[tokio::main]
async fn main() -> Result<()> {
    Ok(())
}
michaelkirk commented 3 months ago

Oh that's nice. Looking at that example, it's within the context of a procedural macro, so I don't think we can use that technique. I wonder if it's possible outside of that context.