Skip to content

Commit 5464492

Browse files
committed
[SE-0101] Migrate sizeof family to MemoryLayout
1 parent 44174d9 commit 5464492

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/swift/Data.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public struct DispatchData : RandomAccessCollection {
8080
var size = 0
8181
let data = CDispatch.dispatch_data_create_map(__wrapped.__wrapped, &ptr, &size)
8282
let contentPtr = ptr!.bindMemory(
83-
to: ContentType.self, capacity: size / strideof(ContentType.self))
83+
to: ContentType.self, capacity: size / MemoryLayout<ContentType>.stride)
8484
defer { _fixLifetime(data) }
8585
return try body(contentPtr)
8686
}
@@ -118,10 +118,10 @@ public struct DispatchData : RandomAccessCollection {
118118
///
119119
/// - parameter buffer: The buffer of bytes to append. The size is calculated from `SourceType` and `buffer.count`.
120120
public mutating func append<SourceType>(_ buffer : UnsafeBufferPointer<SourceType>) {
121-
let count = buffer.count * sizeof(SourceType.self)
122-
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: count) {
123-
self.append($0, count: count)
124-
}
121+
let count = buffer.count * MemoryLayout<SourceType>.stride;
122+
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: count) {
123+
self.append($0, count: count)
124+
}
125125
}
126126

127127
private func _copyBytesHelper(to pointer: UnsafeMutablePointer<UInt8>, from range: CountableRange<Index>) {
@@ -154,7 +154,7 @@ public struct DispatchData : RandomAccessCollection {
154154

155155
/// Copy the contents of the data into a buffer.
156156
///
157-
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `sizeof(DestinationType) * buffer.count` then the first N bytes will be copied into the buffer.
157+
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `MemoryLayout<DestinationType>.size * buffer.count` then the first N bytes will be copied into the buffer.
158158
/// - precondition: The range must be within the bounds of the data. Otherwise `fatalError` is called.
159159
/// - parameter buffer: A buffer to copy the data into.
160160
/// - parameter range: A range in the data to copy into the buffer. If the range is empty, this function will return 0 without copying anything. If the range is nil, as much data as will fit into `buffer` is copied.
@@ -172,18 +172,17 @@ public struct DispatchData : RandomAccessCollection {
172172
precondition(r.endIndex >= 0)
173173
precondition(r.endIndex <= cnt, "The range is outside the bounds of the data")
174174

175-
copyRange = r.startIndex..<(r.startIndex + Swift.min(buffer.count * sizeof(DestinationType.self), r.count))
175+
copyRange = r.startIndex..<(r.startIndex + Swift.min(buffer.count * MemoryLayout<DestinationType>.size, r.count))
176176
} else {
177-
copyRange = 0..<Swift.min(buffer.count * sizeof(DestinationType.self), cnt)
177+
copyRange = 0..<Swift.min(buffer.count * MemoryLayout<DestinationType>.size, cnt)
178178
}
179179

180180
guard !copyRange.isEmpty else { return 0 }
181181

182-
let bufferCapacity = buffer.count * sizeof(DestinationType.self)
183-
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: bufferCapacity) {
184-
185-
_copyBytesHelper(to: $0, from: copyRange)
186-
}
182+
let bufferCapacity = buffer.count * MemoryLayout<DestinationType>.stride
183+
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: bufferCapacity) {
184+
_copyBytesHelper(to: $0, from: copyRange)
185+
}
187186
return copyRange.count
188187
}
189188

0 commit comments

Comments
 (0)