Skip to content

Commit ad5ab9b

Browse files
committed
[BitwiseCopyable] Add new stdlib API.
Overload existing public stdlib functions on a BitwiseCopyable constraint to allow for ideal code generation for conforming types.
1 parent 144c927 commit ad5ab9b

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

proposals/nnnn-bitwise-copyable.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public func loadUnaligned<T: BitwiseCopyable>(
4646
) -> T
4747
```
4848

49+
And this proposal includes the addition of three overloads of existing standard library functions.
50+
4951
## Proposed solution
5052

5153
We add a new protocol `BitwiseCopyable` to the standard library:
@@ -166,6 +168,52 @@ public struct Coordinate3 {
166168
```
167169
to `BitwiseCopyable`.
168170

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+
169217
## Effect on ABI stability
170218

171219
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

Comments
 (0)