Skip to content

Commit e29b425

Browse files
authored
Merge pull request #82277 from glessard/rdar153219174-UMBP-mutableSpan
[stdlib] add missing computed properties
2 parents 66b8f35 + 62b58a8 commit e29b425

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,18 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
583583
return unsafe _overrideLifetime(span, borrowing: self)
584584
}
585585
}
586+
%if Mutable:
587+
588+
@unsafe
589+
@available(SwiftStdlib 6.2, *)
590+
public var mutableSpan: MutableSpan<Element> {
591+
@lifetime(borrow self)
592+
@_alwaysEmitIntoClient
593+
get {
594+
unsafe MutableSpan(_unsafeElements: self)
595+
}
596+
}
597+
%end
586598
}
587599

588600
extension Unsafe${Mutable}BufferPointer {

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,16 @@ extension Unsafe${Mutable}RawBufferPointer {
11621162
}
11631163
}
11641164

1165+
@unsafe
1166+
@available(SwiftStdlib 6.2, *)
1167+
public var mutableBytes: MutableRawSpan {
1168+
@lifetime(borrow self)
1169+
@_alwaysEmitIntoClient
1170+
get {
1171+
unsafe MutableRawSpan(_unsafeBytes: self)
1172+
}
1173+
}
1174+
11651175
% end
11661176
@_alwaysEmitIntoClient
11671177
public func withContiguousStorageIfAvailable<R>(

test/abi/macOS/arm64/stdlib.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,8 @@ Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
934934
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
935935
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
936936
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
937+
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
938+
Added: _$sSw12mutableBytess14MutableRawSpanVvr
937939

938940
// _SwiftifyInfo enum for _SwiftifyImports macro
939941
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,8 @@ Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
935935
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
936936
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
937937
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
938+
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
939+
Added: _$sSw12mutableBytess14MutableRawSpanVvr
938940

939941
// _SwiftifyInfo enum for _SwiftifyImports macro
940942
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC

test/stdlib/Span/MutableRawSpanTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,26 @@ suite.test("extracting suffixes")
514514
expectEqual(span.extracting(droppingFirst: 1).byteCount, b.count)
515515
}
516516
}
517+
518+
suite.test("MutableRawSpan from UnsafeMutableRawBufferPointer")
519+
.require(.stdlib_6_2).code {
520+
guard #available(SwiftStdlib 6.2, *) else { return }
521+
522+
let capacity = 4
523+
let b = UnsafeMutableRawBufferPointer.allocate(
524+
byteCount: capacity*MemoryLayout<Int64>.stride,
525+
alignment: MemoryLayout<Int64>.alignment
526+
)
527+
defer {
528+
b.deallocate()
529+
}
530+
_ = b.initializeMemory(as: Int64.self, fromContentsOf: 0..<Int64(capacity))
531+
532+
var span = b.mutableBytes
533+
span.storeBytes(of: 3, toByteOffset: 10, as: UInt16.self)
534+
535+
_ = consume span
536+
537+
let v = b.load(fromByteOffset: 8, as: Int64.self)
538+
expectNotEqual(v, 1)
539+
}

test/stdlib/Span/MutableSpanTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,3 +720,25 @@ suite.test("extracting suffixes")
720720
expectEqual(span.extracting(droppingFirst: 1).count, b.count)
721721
}
722722
}
723+
724+
suite.test("MutableSpan from UnsafeMutableBufferPointer")
725+
.require(.stdlib_6_2).code {
726+
guard #available(SwiftStdlib 6.2, *) else { return }
727+
728+
let capacity = 4
729+
let b = UnsafeMutableBufferPointer<Int>.allocate(capacity: capacity)
730+
defer {
731+
b.deallocate()
732+
}
733+
_ = b.initialize(fromContentsOf: 0..<capacity)
734+
735+
var span = b.mutableSpan
736+
expectEqual(span.count, capacity)
737+
738+
span.swapAt(0, 3)
739+
span.swapAt(1, 2)
740+
741+
_ = consume span
742+
743+
expectTrue(b.elementsEqual((0..<capacity).reversed()))
744+
}

0 commit comments

Comments
 (0)