sqlx
sqlx copied to clipboard
Lifetime does not live long enough for deriving enum types.
Bug Description
I'm trying to derive postgres / sqlx types for:
- An enum which is represented as an integer in postgres
- A struct which uses that enum as one of its fields
This is creating a compiler error due to lifetime constraints which is weird to me since I'm not using references. Even when I implement copy semantics I get the same error.
Minimal Reproduction
#[derive(sqlx::Type)]
#[repr(i32)]
pub enum Inner {
V1 = 1,
V2 = 2,
}
#[derive(sqlx::Type)]
pub struct Outer {
inner: Inner,
other: i32,
}
Error 1:
error: lifetime may not live long enough
--> rust/year_quarter/src/lib.rs:8:10
|
8 | #[derive(sqlx::Type)]
| ^^^^^^^^^^
| |
| lifetime `'r` defined here
| requires that `'r` must outlive `'static`
|
= note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)
Error 2:
error: implementation of `sqlx::Decode` is not general enough
--> rust/year_quarter/src/lib.rs:8:10
|
8 | #[derive(sqlx::Type)]
| ^^^^^^^^^^ implementation of `sqlx::Decode` is not general enough
|
= note: `Inner` must implement `sqlx::Decode<'0, Postgres>`, for any lifetime `'0`...
= note: ...but it actually implements `sqlx::Decode<'1, Postgres>`, for some specific lifetime `'1`
= note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)
Info
- SQLx version:
version = "0.6" - SQLx features enabled:
features = ["postgres", "runtime-actix-rustls"] - Database server and version: Postgres 13+
- Operating system: ARM Mac
rustc --version:rustc 1.71.0 (8ede3aae2 2023-07-12)
- If I remove
#[repr(i32)]it compiles. - If I remove
other: i32,it compiles.
With both of those, it does not compile.
Same happens with Option enum.
I suppose there is no movement on this? It's a blocking issue for me so just wondering
I upgraded rust from nightly-2023-11-09 to nightly-2024-07-02 and started facing the same issue with f32
#[derive(Debug, sqlx::Type, Serialize, Deserialize)]
#[sqlx(type_name = "margin_setting")]
pub struct MarginSetting {
pub min_price: f32,
pub max_price: f32,
pub margin: f32,
}
error: implementation of `sqlx::Decode` is not general enough
--> shared\src\database\custom_types\margin_settings.rs:4:17
|
4 | #[derive(Debug, sqlx::Type, Serialize, Deserialize)]
| ^^^^^^^^^^ implementation of `sqlx::Decode` is not general enough
|
= note: `f32` must implement `sqlx::Decode<'0, Postgres>`, for any lifetime `'0`...
= note: ...but it actually implements `sqlx::Decode<'1, Postgres>`, for some specific lifetime `'1`
= note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)
error: lifetime may not live long enough
--> shared\src\database\custom_types\margin_settings.rs:4:17
|
4 | #[derive(Debug, sqlx::Type, Serialize, Deserialize)]
| ^^^^^^^^^^
| |
| lifetime `'r` defined here
| requires that `'r` must outlive `'static`
|
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
--> C:\Users\Tim\.cargo\registry\src\index.crates.io-6f17d22bba15001f\sqlx-postgres-0.7.4\src\types\record.rs:97:12
|
97 | T: for<'a> Decode<'a, Postgres> + Type<Postgres>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the derive macro `sqlx::Type` (in Nightly builds, run with -Z macro-backtrace for more info)