Skip to content

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Nov 3, 2024

This PR adds experimental support for attachments to some types in Foundation via the (non-functional) cross-import overlay. @stmontgomery is working on setting up said overlay so that it can actually be used; until then, the changes here are speculative only.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

@grynspan grynspan added enhancement New feature or request public-api Affects public API attachments/activities 🖇️ Work related to attachments and/or activities labels Nov 3, 2024
@grynspan grynspan self-assigned this Nov 3, 2024
@grynspan
Copy link
Contributor Author

grynspan commented Nov 3, 2024

@swift-ci test

2 similar comments
@grynspan
Copy link
Contributor Author

grynspan commented Nov 3, 2024

@swift-ci test

@grynspan
Copy link
Contributor Author

grynspan commented Nov 3, 2024

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/foundation-attachment-overlay branch from 81016f9 to 4ebf0f7 Compare November 4, 2024 14:35
@grynspan
Copy link
Contributor Author

grynspan commented Nov 4, 2024

@swift-ci test

@grynspan grynspan added this to the Swift 6.1 milestone Nov 5, 2024
@grynspan grynspan force-pushed the jgrynspan/foundation-attachment-overlay branch from 52ec53d to 992d1a8 Compare November 5, 2024 19:36
@grynspan
Copy link
Contributor Author

grynspan commented Nov 5, 2024

@swift-ci test

4 similar comments
@grynspan
Copy link
Contributor Author

grynspan commented Nov 5, 2024

@swift-ci test

@grynspan
Copy link
Contributor Author

grynspan commented Nov 5, 2024

@swift-ci test

@grynspan
Copy link
Contributor Author

grynspan commented Nov 5, 2024

@swift-ci test

@grynspan
Copy link
Contributor Author

grynspan commented Nov 5, 2024

@swift-ci test

@grynspan grynspan force-pushed the jgrynspan/foundation-attachment-overlay branch from f9b545e to 3b1b340 Compare November 6, 2024 15:20
///
/// The specific format this case corresponds to depends on if we are encoding
/// an `Encodable` value or an `NSSecureCoding` value.
case `default`
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be the first case in the list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can move it if it bothers you—it's an internal type so it's not going to affect client code.

@grynspan grynspan force-pushed the jgrynspan/foundation-attachment-overlay branch from 3b1b340 to def5bbf Compare November 6, 2024 20:24
This PR makes `Test.Attachment` generic over its attachable value type. It gains
conditional conformance to `Copyable` and `Sendable` depending on the attachable
value and if you call `attach()` on a move-only or non-sendable attachment, will
eagerly serialize the attachable value at that point (rather than during
initialization.)

There are a few benefits here:

1. Callers can statically know the type of the attachable value in an attachment
   rather than needing to always deal with an existential box;
2. We can add associated types to `Test.Attachable` that will be readily
   accessible in `withUnsafeBufferPointer(for:_:)` again without needing an
   existential; and
3. When we eventually add support for image attachments, we won't need a bunch
   of additional initializers or intermediate box types or what-have-you; and
4. For Embedded Swift or other environments where existentials are problematic,
   we can eagerly serialize all attachments and pass a consistent type
   (`Test.Attachment<[UInt8]>`) to the event handler.

There are also some drawbacks:

1. Because conformance to `Copyable` and `Sendable` is conditional, we lose a
   bit of flexibility if you have a non-sendable `Test.Attachment` instance or
   whatnot;
2. We still need a lazy, type-erased attachment type that can be passed to the
   event handler. I played around with `Test.Attachment<Any>` but that causes as
   many problems as it solves.

   We end up with `Test.Attachment<any Test.Attachable & Sendable & Copyable>`
   but, because that's an existential type that doesn't conform to itself, the
   generic parameter `AttachableValue` is not constrained to `Test.Attachable`.
   We only provide initializers for types that do conform though (plus the
   existential one internally) so in practice it's not a huge issue.
3. There is some code duplication necessary (i.e. multiple implementations of
   `attach()` and `write()`.)
@grynspan grynspan force-pushed the jgrynspan/foundation-attachment-overlay branch from def5bbf to a7b3030 Compare November 8, 2024 21:36
@grynspan grynspan force-pushed the jgrynspan/foundation-attachment-overlay branch from a7b3030 to 7a9b6da Compare November 8, 2024 22:04
grynspan and others added 23 commits November 8, 2024 17:19
…es since we'll need them for image attachments
…le, don't limit our options more than we have to
This PR adds experimental support for attachments to some types in Foundation
via the (non-functional) cross-import overlay. @stmontgomery is working on
setting up said overlay so that it can actually be used; until then, the changes
here are speculative only.
@grynspan grynspan force-pushed the jgrynspan/foundation-attachment-overlay branch from 7a9b6da to df272e7 Compare November 10, 2024 20:19
@grynspan
Copy link
Contributor Author

Closing for now: I want to sort out generic attachments first.

@grynspan grynspan closed this Nov 11, 2024
grynspan added a commit that referenced this pull request Nov 13, 2024
…819)

This PR adds experimental support for attachments to some types in
Foundation via the (non-functional) cross-import overlay. @stmontgomery
is working on setting up said overlay so that it can actually be used;
until then, the changes here are speculative only.

Replaces #799.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
@grynspan grynspan added the cross-import-overlays 🍰 Cross-import overlays (Foundation, Core Graphics, etc.) label May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
attachments/activities 🖇️ Work related to attachments and/or activities cross-import-overlays 🍰 Cross-import overlays (Foundation, Core Graphics, etc.) enhancement New feature or request public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants