Skip to content

Commit d9b2863

Browse files
authored
Merge pull request #77699 from glessard/span-gardening-6.1
[6.1] Span gardening
2 parents 05786df + 4ab2d5d commit d9b2863

File tree

2 files changed

+8
-86
lines changed

2 files changed

+8
-86
lines changed

stdlib/public/core/Span/RawSpan.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ extension RawSpan {
412412
@available(SwiftStdlib 6.1, *)
413413
extension RawSpan {
414414

415-
//FIXME: mark closure parameter as non-escaping
416415
/// Calls the given closure with a pointer to the underlying bytes of
417416
/// the viewed contiguous storage.
418417
///
@@ -608,7 +607,7 @@ extension RawSpan {
608607
}
609608
}
610609

611-
//MARK: one-sided slicing operations
610+
//MARK: prefixes and suffixes
612611
@_disallowFeatureSuppression(NonescapableTypes)
613612
@available(SwiftStdlib 6.1, *)
614613
extension RawSpan {

stdlib/public/core/Span/Span.swift

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ extension Span where Element: ~Copyable {
9999
public init(
100100
_unsafeElements buffer: borrowing UnsafeBufferPointer<Element>
101101
) {
102-
let baseAddress = buffer.baseAddress //FIXME: rdar://138665760
102+
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
103+
let baseAddress = buffer.baseAddress
103104
_precondition(
104105
((Int(bitPattern: baseAddress) &
105106
(MemoryLayout<Element>.alignment &- 1)) == 0),
@@ -201,7 +202,8 @@ extension Span where Element: BitwiseCopyable {
201202
public init(
202203
_unsafeBytes buffer: borrowing UnsafeRawBufferPointer
203204
) {
204-
let baseAddress = buffer.baseAddress //FIXME: rdar://138665760
205+
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
206+
let baseAddress = buffer.baseAddress
205207
_precondition(
206208
((Int(bitPattern: baseAddress) &
207209
(MemoryLayout<Element>.alignment &- 1)) == 0),
@@ -313,87 +315,6 @@ extension Span where Element: BitwiseCopyable {
313315
}
314316
}
315317

316-
@available(SwiftStdlib 6.1, *)
317-
extension Span where Element: Equatable {
318-
319-
/// Returns a Boolean value indicating whether this and another span
320-
/// contain equal elements in the same order.
321-
///
322-
/// - Parameters:
323-
/// - other: A span to compare to this one.
324-
/// - Returns: `true` if this sequence and `other` contain equivalent items,
325-
/// using `areEquivalent` as the equivalence test; otherwise, `false.`
326-
///
327-
/// - Complexity: O(*m*), where *m* is the lesser of the length of the
328-
/// sequence and the length of `other`.
329-
@_disallowFeatureSuppression(NonescapableTypes)
330-
@_alwaysEmitIntoClient
331-
public func _elementsEqual(_ other: Self) -> Bool {
332-
guard count == other.count else { return false }
333-
if count == 0 { return true }
334-
335-
// This could be short-cut with a layout constraint
336-
// where stride equals size, as long as there is
337-
// at most 1 unused bit pattern, e.g.:
338-
// if Element is BitwiseEquatable {
339-
// return _swift_stdlib_memcmp(lhs.baseAddress, rhs.baseAddress, count) == 0
340-
// }
341-
if _pointer != other._pointer {
342-
for o in 0..<count {
343-
if self[unchecked: o] != other[unchecked: o] { return false }
344-
}
345-
}
346-
return true
347-
}
348-
349-
/// Returns a Boolean value indicating whether this span and a Collection
350-
/// contain equal elements in the same order.
351-
///
352-
/// - Parameters:
353-
/// - other: A Collection to compare to this span.
354-
/// - Returns: `true` if this sequence and `other` contain equivalent items,
355-
/// using `areEquivalent` as the equivalence test; otherwise, `false.`
356-
///
357-
/// - Complexity: O(*m*), where *m* is the lesser of the length of the
358-
/// sequence and the length of `other`.
359-
@_disallowFeatureSuppression(NonescapableTypes)
360-
@_alwaysEmitIntoClient
361-
public func _elementsEqual(_ other: some Collection<Element>) -> Bool {
362-
let equal = other.withContiguousStorageIfAvailable {
363-
_elementsEqual(Span(_unsafeElements: $0))
364-
}
365-
if let equal { return equal }
366-
367-
guard count == other.count else { return false }
368-
if count == 0 { return true }
369-
370-
return _elementsEqual(AnySequence(other))
371-
}
372-
373-
/// Returns a Boolean value indicating whether this span and a Sequence
374-
/// contain equal elements in the same order.
375-
///
376-
/// - Parameters:
377-
/// - other: A Sequence to compare to this span.
378-
/// - Returns: `true` if this sequence and `other` contain equivalent items,
379-
/// using `areEquivalent` as the equivalence test; otherwise, `false.`
380-
///
381-
/// - Complexity: O(*m*), where *m* is the lesser of the length of the
382-
/// sequence and the length of `other`.
383-
@_disallowFeatureSuppression(NonescapableTypes)
384-
@_alwaysEmitIntoClient
385-
public func _elementsEqual(_ other: some Sequence<Element>) -> Bool {
386-
var offset = 0
387-
for otherElement in other {
388-
if offset >= count { return false }
389-
if self[unchecked: offset] != otherElement { return false }
390-
offset += 1
391-
}
392-
return offset == count
393-
}
394-
}
395-
396-
@_disallowFeatureSuppression(NonescapableTypes)
397318
@available(SwiftStdlib 6.1, *)
398319
extension Span where Element: ~Copyable {
399320

@@ -674,7 +595,9 @@ extension Span where Element: BitwiseCopyable {
674595
public func withUnsafeBytes<E: Error, Result: ~Copyable>(
675596
_ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result
676597
) throws(E) -> Result {
677-
try RawSpan(_elements: self).withUnsafeBytes(body)
598+
try body(
599+
.init(start: _pointer, count: _count * MemoryLayout<Element>.stride)
600+
)
678601
}
679602
}
680603

0 commit comments

Comments
 (0)