Skip to content

[stdlib] BitwiseCopyable storeBytes overload. #71625

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1814,15 +1814,14 @@ internal struct RawKeyPathComponent {
}
}

internal func _pop<T>(from: inout UnsafeRawBufferPointer,
internal func _pop<T : _BitwiseCopyable>(from: inout UnsafeRawBufferPointer,
as type: T.Type) -> T {
let buffer = _pop(from: &from, as: type, count: 1)
return buffer.baseAddress.unsafelyUnwrapped.pointee
}
internal func _pop<T>(from: inout UnsafeRawBufferPointer,
internal func _pop<T : _BitwiseCopyable>(from: inout UnsafeRawBufferPointer,
as: T.Type,
count: Int) -> UnsafeBufferPointer<T> {
_internalInvariant(_isPOD(T.self), "should be POD")
from = MemoryLayout<T>._roundingUpBaseToAlignment(from)
let byteCount = MemoryLayout<T>.stride * count
let result = UnsafeBufferPointer(
Expand Down Expand Up @@ -3414,8 +3413,7 @@ internal struct InstantiateKeyPathBuffer: KeyPathPatternVisitor {
}
return (baseAddress, misalign)
}
mutating func pushDest<T>(_ value: T) {
_internalInvariant(_isPOD(T.self))
mutating func pushDest<T : _BitwiseCopyable>(_ value: T) {
let size = MemoryLayout<T>.size
let (baseAddress, misalign) = adjustDestForAlignment(of: T.self)
_withUnprotectedUnsafeBytes(of: value) {
Expand Down
14 changes: 14 additions & 0 deletions stdlib/public/core/UnsafeRawPointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,20 @@ public struct UnsafeMutableRawPointer: _Pointer {
/// - offset: The offset from this pointer, in bytes. `offset` must be
/// nonnegative. The default is zero.
/// - type: The type of `value`.
#if $BitwiseCopyable
@inlinable
@_alwaysEmitIntoClient
public func storeBytes<T : _BitwiseCopyable>(
of value: T, toByteOffset offset: Int = 0, as type: T.Type
) {
withUnsafePointer(to: value) { source in
_memcpy(
dest: (self + offset),
src: source,
size: UInt(MemoryLayout<T>.size))
}
}
#endif
@inlinable
@_alwaysEmitIntoClient
// This custom silgen name is chosen to not interfere with the old ABI
Expand Down