Skip to content

feat: support borrowed parameter types via borrowed_rs_type override#19

Merged
jrandolf merged 1 commit into
mainfrom
feat/borrowed-mode
May 18, 2026
Merged

feat: support borrowed parameter types via borrowed_rs_type override#19
jrandolf merged 1 commit into
mainfrom
feat/borrowed-mode

Conversation

@jrandolf
Copy link
Copy Markdown
Contributor

Summary

  • Add borrowed_rs_type to type overrides — scalar params take the borrowed form while rows, array contents, and :copyfrom items keep the owned form.
  • Lifetime elision where possible: single-param fns rely on elision; params structs gain <'a> with the fn ref using <'_>; batch streams reuse the existing 'a; :copyfrom introduces 'a because its where Item = ... clause cannot elide.
  • Vec<T> wrappers around borrowed inners become &[T_owned] so a caller holding a Vec<String> can pass &v directly.
  • rs_type becomes optional alongside borrowed_rs_type; the borrowed value is validated at config load and rejected if it does not contain a reference (e.g. Option<String> fails fast).
  • Adds examples/borrowed/ and a "Borrowed parameters" section in the README.

Test plan

  • cargo test --lib -p sqlc-gen-sqlx — 88 unit tests pass
  • cargo test --test codegen -p sqlc-gen-sqlx — 30 codegen tests pass (9 new snapshots: scalar, struct, nullable, array, slice, batch single/struct, copyfrom, owned+borrowed pair, row-unaffected)
  • cargo clippy --workspace --all-targets -- -D warnings clean
  • cargo fmt --all --check clean
  • mise run generate produces the borrowed example's checked-in queries.rs byte-identically
  • cargo test -p borrowed passes end-to-end against a local Postgres (INSERT with &str literals, SELECT with &String, owned row fields round-trip)

Adds a `borrowed_rs_type` field to type overrides so callers can take
parameters by reference without forcing rows to also be borrowed. When
set, the override participates in borrowed mode for scalar parameter
positions; rows, array contents, and copyfrom items continue to use the
owned form.

Lifetimes are threaded via elision wherever possible: single-param
functions take the borrowed type directly; params structs gain `<'a>`
with the fn referencing it as `<'_>`; batch streams reuse the existing
`'a`; copyfrom introduces `'a` because its where clause cannot elide.

Vec wrappers around borrowed inners become `&[T_owned]` so a caller
holding a `Vec<String>` can pass `&v` without re-collecting.

`rs_type` becomes optional when `borrowed_rs_type` is present;
`borrowed_rs_type` is parsed at config load and rejected if it does not
contain a reference, so a value like `Option<String>` fails fast.
@jrandolf jrandolf merged commit 6ce89a3 into main May 18, 2026
2 checks passed
@jrandolf jrandolf deleted the feat/borrowed-mode branch May 18, 2026 09:10
jrandolf added a commit that referenced this pull request May 18, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.2.1](v0.2.0...v0.2.1)
(2026-05-18)


### Features

* Support borrowed parameter types via borrowed_rs_type override
([#19](#19))
([6ce89a3](6ce89a3))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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.

1 participant