Skip to content

Commit

Permalink
Work around a source compatibility break in the Swift 6 stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
karwa committed Jul 1, 2024
1 parent 01ad5a1 commit fceb11f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 45 deletions.
32 changes: 17 additions & 15 deletions Sources/WebURL/Util/Pointers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@
// --------------------------------------------


extension UnsafeRawPointer {

/// Returns a new instance of the given type, constructed from the raw memory at the specified offset.
///
/// The memory at this pointer plus offset must be initialized to `T` or another type that is layout compatible with `T`.
/// It does not need to be aligned for access to `T`.
///
@inlinable @inline(__always)
internal func loadUnaligned<T>(fromByteOffset offset: Int = 0, as: T.Type) -> T where T: FixedWidthInteger {
assert(_isPOD(T.self))
var val: T = 0
withUnsafeMutableBytes(of: &val) {
$0.copyMemory(from: UnsafeRawBufferPointer(start: self, count: T.bitWidth / 8))
#if swift(<5.9)
extension UnsafeRawPointer {

/// Returns a new instance of the given type, constructed from the raw memory at the specified offset.
///
/// The memory at this pointer plus offset must be initialized to `T` or another type
/// that is layout compatible with `T`. It does not need to be aligned for access to `T`.
///
@inlinable @inline(__always)
internal func loadUnaligned<T>(fromByteOffset offset: Int = 0, as: T.Type) -> T where T: FixedWidthInteger {
assert(_isPOD(T.self))
var val: T = 0
withUnsafeMutableBytes(of: &val) {
$0.copyMemory(from: UnsafeRawBufferPointer(start: self, count: T.bitWidth / 8))
}
return val
}
return val
}
}
#endif


// --------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,25 @@ extension Sequence where Element == UInt8 {
}
}

extension UnsafeRawPointer {

/// Returns a new instance of the given type, constructed from the raw memory at the specified offset.
///
/// The memory at this pointer plus offset must be initialized to `T` or another type
/// that is layout compatible with `T`. It does not need to be aligned for access to `T`.
///
@inlinable @inline(__always)
internal func loadUnaligned<T>(fromByteOffset offset: Int = 0, as: T.Type) -> T where T: FixedWidthInteger {
assert(_isPOD(T.self))
var val: T = 0
withUnsafeMutableBytes(of: &val) {
$0.copyMemory(from: UnsafeRawBufferPointer(start: self, count: T.bitWidth / 8))
#if swift(<5.9)
extension UnsafeRawPointer {

/// Returns a new instance of the given type, constructed from the raw memory at the specified offset.
///
/// The memory at this pointer plus offset must be initialized to `T` or another type
/// that is layout compatible with `T`. It does not need to be aligned for access to `T`.
///
@inlinable @inline(__always)
internal func loadUnaligned<T>(fromByteOffset offset: Int = 0, as: T.Type) -> T where T: FixedWidthInteger {
assert(_isPOD(T.self))
var val: T = 0
withUnsafeMutableBytes(of: &val) {
$0.copyMemory(from: UnsafeRawBufferPointer(start: self, count: T.bitWidth / 8))
}
return val
}
return val
}
}
#endif

extension UInt64 {

Expand Down
32 changes: 17 additions & 15 deletions Sources/WebURLTestSupport/IPAddressUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,22 @@ extension IPv6Address.Utils {
// --------------------------------------------


extension UnsafeRawPointer {

/// Returns a new instance of the given type, constructed from the raw memory at the specified offset.
///
/// The memory at this pointer plus offset must be initialized to `T` or another type that is layout compatible with `T`.
/// It does not need to be aligned for access to `T`.
///
@inlinable @inline(__always)
internal func loadUnaligned<T>(fromByteOffset offset: Int = 0, as: T.Type) -> T where T: FixedWidthInteger {
assert(_isPOD(T.self))
var val: T = 0
withUnsafeMutableBytes(of: &val) {
$0.copyMemory(from: UnsafeRawBufferPointer(start: self + offset, count: MemoryLayout<T>.stride))
#if swift(<5.9)
extension UnsafeRawPointer {

/// Returns a new instance of the given type, constructed from the raw memory at the specified offset.
///
/// The memory at this pointer plus offset must be initialized to `T` or another type
/// that is layout compatible with `T`. It does not need to be aligned for access to `T`.
///
@inlinable @inline(__always)
internal func loadUnaligned<T>(fromByteOffset offset: Int = 0, as: T.Type) -> T where T: FixedWidthInteger {
assert(_isPOD(T.self))
var val: T = 0
withUnsafeMutableBytes(of: &val) {
$0.copyMemory(from: UnsafeRawBufferPointer(start: self + offset, count: MemoryLayout<T>.stride))
}
return val
}
return val
}
}
#endif

0 comments on commit fceb11f

Please sign in to comment.