@@ -46,6 +46,8 @@ public func loadUnaligned<T: BitwiseCopyable>(
46
46
) -> T
47
47
```
48
48
49
+ And this proposal includes the addition of three overloads of existing standard library functions.
50
+
49
51
## Proposed solution
50
52
51
53
We add a new protocol ` BitwiseCopyable ` to the standard library:
@@ -166,6 +168,52 @@ public struct Coordinate3 {
166
168
```
167
169
to ` BitwiseCopyable ` .
168
170
171
+ ### Standard library API improvements
172
+
173
+ The standard library includes a load method on both ` UnsafeRawPointer ` and ` UnsafeMutableRawPointer `
174
+
175
+ ```
176
+ @inlinable
177
+ @_alwaysEmitIntoClient
178
+ public func loadUnaligned<T>(
179
+ fromByteOffset offset: Int = 0,
180
+ as type: T.Type
181
+ ) -> T
182
+ ```
183
+
184
+ and a corresponding write method on ` UnsafeMutableRawPointer `
185
+
186
+ ```
187
+ @inlinable
188
+ @_alwaysEmitIntoClient
189
+ public func storeBytes<T>(
190
+ of value: T, toByteOffset offset: Int = 0, as type: T.Type
191
+ )
192
+ ```
193
+
194
+ that must be called with a trivial ` T ` .
195
+
196
+ We propose adding overloads of these methods to constrain the value to ` BitwiseCopyable ` :
197
+
198
+ ```
199
+ // on both UnsafeRawPointer and UnsafeMutableRawPointer
200
+ @inlinable
201
+ @_alwaysEmitIntoClient
202
+ public func loadUnaligned<T : BitwiseCopyable>(
203
+ fromByteOffset offset: Int = 0,
204
+ as type: T.Type
205
+ ) -> T
206
+
207
+ // on UnsafeMutableRawPointer
208
+ @inlinable
209
+ @_alwaysEmitIntoClient
210
+ public func storeBytes<T : BitwiseCopyable>(
211
+ of value: T, toByteOffset offset: Int = 0, as type: T.Type
212
+ )
213
+ ```
214
+
215
+ This allows for optimal code generation because ` memcpy ` instead of value witnesses can be used.
216
+
169
217
## Effect on ABI stability
170
218
171
219
The addition of the ` BitwiseCopyable ` constraint to either a type or a protocol in a library will not cause an ABI break for users.
0 commit comments