#4411·sqlx

Expose a trait to opt in for impl Type for PgRange<NewType>

Author: dennsvaCreated Sep 16, 2026Updated Sep 16, 2026
Labelsenhancement

I have found these related issues/pull requests

It seems the same problem has been encountered before in #1258.

Description

My familiarity of SQLx is with Postgres exclusively, and hence my question uses that as frame of reference. The PgHasArrayType, which provides blanket impls like this has often been very useful to me:

rust
impl<T> Type<Postgres> for Vec<T>
where
    T: PgHasArrayType,

On the other hand, since impl Type for PgRange<T> is done only for explicit types such as T = i64, there is no way for me to achieve impl Type for PgRange<NewType>, where NewType is a wrapper around i64 providing some invariant, but still being encoded as a BIGINT in Postgres.

I am using SQLx version 0.9.0.

Prefered solution

If this was also handled through a blanket impl

rust
impl<T> Type<Postgres> for PgRange<T>
where
    T: PgHasRangeType,

where the proposed trait PgHasRangeType would provide everything the blanket impl needs, I could easily add my newtype to the mix.

I guess a nontrivial question would then also be how arrays of ranges are handled. The idea of a third trait such as PgRangeHasArrayType would be a bit out there, but on the other hand passing that information through PgHasRangeType would require some undesirable type system hacks, in my opinion. I really hope the inverse, a range of arrays, is not something anyone routinely needs.

Is this a breaking change? Why or why not?

No. The existing impl Type for PgRange<T> cases can be converted to impl PgHasRangeType for T, still indirectly providing the desired impl Type for PgRange<T>, or left as they are if desired.