Skip to content

&str-based FromSql impls for firewall/route types #8303

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

Merged
merged 1 commit into from
Jun 15, 2025

Conversation

FelixMcFelix
Copy link
Contributor

A few types (target & filter specifications, ranges) associated with firewall rules and routes rely on a custom string-based representation in CRDB. However, when deserializing them today we end up cycling through PgValue->String->ParsedEnumType, needlessly allocating and copying strings per element. This PR prevents us from doing so by generating these impls for any type which implements the existing DatabaseString trait.

While there are more types in nexus-db-model using FromSql<Text, _>, most seem to make use of a String internally regardless, so there's no immediate benefit (other than consistency) in changing them up at this time.

Motivated by some of the discussion in #8194.

A few types (target & filter specifications, ranges) associated with
firewall rules and routes rely on a custom string-based representation
in CRDB. However, when deserializing them today we end up cycling
through `PgValue`->`String`->`ParsedEnumType`, needlessly allocating and
copying strings per element. This PR prevents us from doing so by
generating these `impl`s for any type which implements the existing
`DatabaseString` trait.

While there are more types in `nexus-db-model` using `FromSql<Text, _>`,
most seem to make use of a `String` internally regardless, so there's no
immediate benefit (other than consistency) in changing them up at this
time.

Motivated by some of the discussion in #8194.
Comment on lines +397 to +405
impl ::diesel::deserialize::FromSql<::diesel::sql_types::Text, ::diesel::pg::Pg> for $model_type {
fn from_sql(bytes: <::diesel::pg::Pg as ::diesel::backend::Backend>::RawValue<'_>)
-> ::diesel::deserialize::Result<Self>
{
Ok($model_type::from_database_string(::std::str::from_utf8(bytes.as_bytes())?)?)
}
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Worth pointing out that we could have this work for all DB: Backend, but we'd need to rely on a little unsafe to use the *const str: FromSql<sql_types::Text, DB>, bound.

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

Thanks to taking the time to look into this! This looks good to me!

@FelixMcFelix FelixMcFelix merged commit 0d746e0 into main Jun 15, 2025
16 checks passed
@FelixMcFelix FelixMcFelix deleted the felixmcfelix/fromstr-fromsql-derive branch June 15, 2025 07:05
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