Skip to content

Allow single-field named structs to be transparent #3971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Xiretza
Copy link

@Xiretza Xiretza commented Aug 11, 2025

This more closely matches the criteria for e.g. #[repr(transparent)] and #[serde(transparent)].

TODO, if this is deemed a desirable change:

  • Documentation updates
  • Tests (Where should those go? The change isn't backend-specific)

Does your PR solve an issue?

Couldn't find an issue filed already.

Is this a breaking change?

No, code that previously compiled is unchanged, only code that used to be a compile error now does compile.

This more closely matches the criteria for e.g. #[repr(transparent)]
and #[serde(transparent)].
Copy link
Collaborator

@abonander abonander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine with a couple of nits + docs and testing.

Tests (Where should those go? The change isn't backend-specific)

Preferably you should add a test for each backend to be sure the behavior is consistent. But if you don't have the time for that, I'd say just pick one:

@Xiretza Xiretza force-pushed the transparent-named-structs branch 4 times, most recently from c0ae489 to 2eb8165 Compare August 19, 2025 16:18
Comment on lines 588 to 610
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
#[sqlx(transparent)]
struct MyIntNamed {
inner: i64,
}

struct NamedTransparentRecord {
id: MyIntNamed,
}

#[sqlx_macros::test]
async fn test_named_transparent_struct() -> anyhow::Result<()> {
let mut conn = new::<MySql>().await?;
let (mut conn, id) = with_test_row(&mut conn).await?;

let record = sqlx::query_as!(NamedTransparentRecord, "select id as `id: _` from tweet")
.fetch_one(&mut *conn)
.await?;

assert_eq!(record.id, MyIntNamed { inner: id.0 });

Ok(())
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, this is testing the derive behavior more than the query macro behavior, so it should be moved to derives.rs.

When you do that, please add #[cfg(feature = "macros")] to the test like the other tests using query macros there. You might also move the struct definitions into the function so they don't emit "unused code" warnings when the feature is off.

Either that, or just rewrite the test to not use the query macros since that's not really what we're looking to test here.

Comment on lines 664 to 686
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
#[sqlx(transparent)]
struct MyIntNamed {
inner: i64,
}

struct NamedTransparentRecord {
id: MyIntNamed,
}

#[sqlx_macros::test]
async fn test_named_transparent_struct() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut conn = with_test_row(&mut conn).await?;

let record = sqlx::query_as!(NamedTransparentRecord, r#"select id as "id: _" from tweet"#)
.fetch_one(&mut *conn)
.await?;

assert_eq!(record.id, MyIntNamed { inner: 1 });

Ok(())
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing re. moving to derives.rs.

Comment on lines 336 to 357
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
#[sqlx(transparent)]
struct MyIntNamed {
inner: i64,
}

struct NamedTransparentRecord {
id: MyIntNamed,
}

#[sqlx_macros::test]
async fn test_named_transparent_struct() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;

let record = sqlx::query_as!(NamedTransparentRecord, r#"select id as "id: _" from tweet"#)
.fetch_one(&mut conn)
.await?;

assert_eq!(record.id, MyIntNamed { inner: 1 });

Ok(())
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing re. moving to derives.rs.

@Xiretza Xiretza force-pushed the transparent-named-structs branch 5 times, most recently from 53d62c3 to 985d526 Compare August 20, 2025 20:12
@Xiretza Xiretza force-pushed the transparent-named-structs branch from 985d526 to d125276 Compare August 20, 2025 20:17
@Xiretza Xiretza requested a review from abonander August 20, 2025 20:31
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