Skip to content

make Array<&str> serializable via per-type Serialize impls#2322

Merged
eeeebbbbrrrr merged 2 commits into
pgcentralfoundation:developfrom
isdaniel:feat/support-serde-serialize-array
Jun 25, 2026
Merged

make Array<&str> serializable via per-type Serialize impls#2322
eeeebbbbrrrr merged 2 commits into
pgcentralfoundation:developfrom
isdaniel:feat/support-serde-serialize-array

Conversation

@isdaniel

@isdaniel isdaniel commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Motivation

The blanket impl<T: UnboxDatum> Serialize for Array<'mcx, T> is unreachable for reference element types: the UnboxDatum::As<'src> where Self: 'src combined with the for<'arr> HRTB forces 'mcx: 'static, so Array<&str>: Serialize never resolves. This is why array_tests.rs carries the TODO disabling serde_serialize_array, and why extensions returning JSON over text[] are forced into Vec and lose zero-copy.

How to use

#[pg_extern]
fn serde_serialize_array<'dat>(values: Array<'dat, &'dat str>) -> Json {
    Json(json! { { "values": values } })
}

Breaking change

Downstream UnboxDatum types (custom type) that relied on the blanket to auto-derive Serialize for Array now need a one-line impl:

impl<'mcx> Serialize for Array<'mcx, MyType> {
  fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
	  s.collect_seq(self.iter())
  }
}

Stable Rust has no specialization, so adding Array<&str> impls required dropping the blanket. All in-tree primitive types stay
covered.

The blanket impl<T: UnboxDatum> Serialize for Array<'mcx, T> is unreachable for reference element types: the UnboxDatum::As<'src> where Self: 'src GAT combined with the for<'arr> HRTB forces 'mcx: 'static, so Array<&str>: Serialize never resolves. This is why array_tests.rs carries the TODO disabling serde_serialize_array, and why extensions returning JSON over text[] are forced into Vec<String> and lose zero-copy.
@isdaniel
isdaniel force-pushed the feat/support-serde-serialize-array branch from cc19fbd to ce29178 Compare June 14, 2026 03:25
@isdaniel
isdaniel force-pushed the feat/support-serde-serialize-array branch from 27863f7 to 956fce7 Compare June 14, 2026 04:20
@eeeebbbbrrrr
eeeebbbbrrrr merged commit 7827d0d into pgcentralfoundation:develop Jun 25, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants