-
Notifications
You must be signed in to change notification settings - Fork 190
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
Derive Eq
and Hash
wherever possible
#2223
Conversation
In server SDKs, these traits can be implemented by any shape _except_ if the shape's closure contains: 1. A `float`, `double`, or `document` shape: floating point types in Rust do not implement `Eq`. Similarly, [`document` shapes] may contain arbitrary JSON-like data containing floating point values. 2. A [@streaming] shape: all the streaming data would need to be buffered first to compare it. Additionally, the `Hash` trait cannot be implemented by shapes whose closure contains: 1. A `map` shape: we render `map` shapes as `std::collections::HashMap`, which _do not_ implement `Hash`. See smithy-lang/smithy#1567. In **client SDKs, these traits cannot be derived on any code-generated Rust types corresponding to Smithy shapes**, since e.g. adding new optional members to a structure [is a backwards-compatible change], and doing so alters the semantics of these traits. However, this commit does implement these traits for the `aws_smithy_types::date_time::DateTime` and `aws_smithy_types::Blob` runtime types. This change is necessary to efficiently implement the `@uniqueItems` constraint trait in server SDKs. [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html [`document` shapes]: https://smithy.io/2.0/spec/simple-types.html#document [@streaming]: https://smithy.io/2.0/spec/streaming.html [is a backwards-compatible change]: https://smithy.io/2.0/guides/evolving-models.html#updating-structures
I'll try to pull out the changes to constrained types in a separate PR so that this patch is more easily reviewable by the @awslabs/rust-sdk-owners team. |
In server SDKs, these traits can be implemented by any shape _except_ if the shape's closure contains: 1. A `float`, `double`, or `document` shape: floating point types in Rust do not implement `Eq`. Similarly, [`document` shapes] may contain arbitrary JSON-like data containing floating point values. 2. A [@streaming] shape: all the streaming data would need to be buffered first to compare it. Additionally, the `Hash` trait cannot be implemented by shapes whose closure contains: 1. A `map` shape: we render `map` shapes as `std::collections::HashMap`, which _do not_ implement `Hash`. See smithy-lang/smithy#1567. In **client SDKs, these traits cannot be derived on any code-generated Rust types corresponding to Smithy shapes**, since e.g. adding new optional members to a structure [is a backwards-compatible change], and doing so alters the semantics of these traits. However, this commit does implement these traits for the `aws_smithy_types::date_time::DateTime` and `aws_smithy_types::Blob` runtime types. This change is necessary to efficiently implement the `@uniqueItems` constraint trait in server SDKs. This commit also introduces a constrained shape symbol metadata provider (`ConstrainedShapeSymbolMetadataProvider.kt`), to centralize generation of Rust metadata (derives, visibility) in one place, instead of each constrained type generator having to manually adjust metadata. Some constrained type methods are now conditionally generated based on visibility, instead of relying on `#[allow(dead_code)]`. [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html [`document` shapes]: https://smithy.io/2.0/spec/simple-types.html#document [@streaming]: https://smithy.io/2.0/spec/streaming.html [is a backwards-compatible change]: https://smithy.io/2.0/guides/evolving-models.html#updating-structures
Actually, I can't break this PR down any further:
So this patch has to be merged atomically. I've updated the changelog and the commit message to better reflect all changes. @awslabs/rust-sdk-owners team: you just have to review the changes to these files:
@awslabs/smithy-rs-server team: you have to review everything. |
...otlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerSymbolProvider.kt
Show resolved
Hide resolved
A new generated diff is ready to view.
A new doc preview is ready to view. |
...tlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedMapGenerator.kt
Show resolved
Hide resolved
...ftware/amazon/smithy/rust/codegen/server/smithy/DeriveEqAndHashSymbolMetadataProviderTest.kt
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm re: eq/hash derives
A new generated diff is ready to view.
A new doc preview is ready to view. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I'd wait for @unexge review as well on this.
A new generated diff is ready to view.
A new doc preview is ready to view. |
In server SDKs, these traits can be implemented by any shape except if
the shape's closure contains:
float
,double
, ordocument
shape: floating point types inRust do not implement
Eq
. Similarly,document
shapes maycontain arbitrary JSON-like data containing floating point values.
buffered first to compare it.
Additionally, the
Hash
trait cannot be implemented by shapes whoseclosure contains:
map
shape: we rendermap
shapes asstd::collections::HashMap
,which do not implement
Hash
. SeeDisallow
@uniqueItems
on list shapes whose closure reaches a map shape smithy#1567.In client SDKs, these traits cannot be derived on any code-generated
Rust types corresponding to Smithy shapes, since e.g. adding new
optional members to a structure is a backwards-compatible change, and
doing so alters the semantics of these traits.
However, this commit does implement these traits for the
aws_smithy_types::date_time::DateTime
andaws_smithy_types::Blob
runtime types.
This change is necessary to efficiently implement the
@uniqueItems
constraint trait in server SDKs.
Testing
I added tests.
Checklist
CHANGELOG.next.toml
if I made changes to the smithy-rs codegen or runtime cratesBy submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.