Commit 35f2618
authored
Make
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]>`~~
`Test.Attachment<Test.AnyAttachable>`) 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.~~
I replaced `any Test.Attachable & Sendable & Copyable` with a dedicated
type-erasing box type, `Test.AnyAttachable`, that wraps the existential.
In Embedded Swift, it wraps a `[UInt8]` instead.
3. There is some code duplication necessary (i.e. multiple
implementations of `attach()` ~~and `write()`~~.)
4. Non-final classes cannot conform to `Test.Attachable`. You can work
around this with a box type that's generic over the class and conforms
to `Test.Attachable`: the `Test.AttachableContainer` protocol now exists
to make this easier.
### 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.Test.Attachment generic. (#814)1 parent 1e65cf1 commit 35f2618
File tree
13 files changed
+275
-184
lines changed- Documentation/Proposals
- Sources/Testing
- ABI/v0/Encoded
- Attachments
- Events
- Issues
- Running
- Tests/TestingTests
13 files changed
+275
-184
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| 127 | + | |
127 | 128 | | |
128 | 129 | | |
129 | 130 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
| |||
61 | 65 | | |
62 | 66 | | |
63 | 67 | | |
64 | | - | |
| 68 | + | |
65 | 69 | | |
66 | 70 | | |
67 | 71 | | |
| |||
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
106 | | - | |
| 110 | + | |
107 | 111 | | |
108 | 112 | | |
109 | 113 | | |
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
113 | | - | |
| 117 | + | |
114 | 118 | | |
115 | 119 | | |
116 | 120 | | |
117 | 121 | | |
118 | 122 | | |
119 | 123 | | |
120 | | - | |
| 124 | + | |
121 | 125 | | |
122 | 126 | | |
123 | 127 | | |
124 | 128 | | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | 129 | | |
154 | 130 | | |
155 | | - | |
| 131 | + | |
156 | 132 | | |
157 | 133 | | |
158 | 134 | | |
| |||
162 | 138 | | |
163 | 139 | | |
164 | 140 | | |
165 | | - | |
| 141 | + | |
166 | 142 | | |
167 | 143 | | |
168 | 144 | | |
| |||
Lines changed: 37 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
0 commit comments