From 7ab45ca4421d0fe90c7b375405d9a8fea1bf6120 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 12 Jun 2025 17:30:48 -0700 Subject: [PATCH 1/3] [stdlib] add `URMBP.mutableBytes` accessor --- .../core/UnsafeRawBufferPointer.swift.gyb | 10 ++++++++ test/stdlib/Span/MutableRawSpanTests.swift | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb b/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb index 2ee50c1b77af2..c9aba12ce1113 100644 --- a/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb +++ b/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb @@ -1162,6 +1162,16 @@ extension Unsafe${Mutable}RawBufferPointer { } } + @unsafe + @available(SwiftStdlib 6.2, *) + public var mutableBytes: MutableRawSpan { + @lifetime(borrow self) + @_alwaysEmitIntoClient + get { + unsafe MutableRawSpan(_unsafeBytes: self) + } + } + % end @_alwaysEmitIntoClient public func withContiguousStorageIfAvailable( diff --git a/test/stdlib/Span/MutableRawSpanTests.swift b/test/stdlib/Span/MutableRawSpanTests.swift index e64558bd0ef3b..993977e6bcd33 100644 --- a/test/stdlib/Span/MutableRawSpanTests.swift +++ b/test/stdlib/Span/MutableRawSpanTests.swift @@ -514,3 +514,26 @@ suite.test("extracting suffixes") expectEqual(span.extracting(droppingFirst: 1).byteCount, b.count) } } + +suite.test("MutableRawSpan from UnsafeMutableRawBufferPointer") +.require(.stdlib_6_2).code { + guard #available(SwiftStdlib 6.2, *) else { return } + + let capacity = 4 + let b = UnsafeMutableRawBufferPointer.allocate( + byteCount: capacity*MemoryLayout.stride, + alignment: MemoryLayout.alignment + ) + defer { + b.deallocate() + } + _ = b.initializeMemory(as: Int64.self, fromContentsOf: 0.. Date: Thu, 12 Jun 2025 17:31:04 -0700 Subject: [PATCH 2/3] [stdlib] add `UMBP.mutableSpan` accessor --- .../public/core/UnsafeBufferPointer.swift.gyb | 12 ++++++++++ test/stdlib/Span/MutableSpanTests.swift | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/stdlib/public/core/UnsafeBufferPointer.swift.gyb b/stdlib/public/core/UnsafeBufferPointer.swift.gyb index 94897c6afd15b..e552db2438004 100644 --- a/stdlib/public/core/UnsafeBufferPointer.swift.gyb +++ b/stdlib/public/core/UnsafeBufferPointer.swift.gyb @@ -583,6 +583,18 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable { return unsafe _overrideLifetime(span, borrowing: self) } } +%if Mutable: + + @unsafe + @available(SwiftStdlib 6.2, *) + public var mutableSpan: MutableSpan { + @lifetime(borrow self) + @_alwaysEmitIntoClient + get { + unsafe MutableSpan(_unsafeElements: self) + } + } +%end } extension Unsafe${Mutable}BufferPointer { diff --git a/test/stdlib/Span/MutableSpanTests.swift b/test/stdlib/Span/MutableSpanTests.swift index 944c049c5f11b..8e7b6ba5ed325 100644 --- a/test/stdlib/Span/MutableSpanTests.swift +++ b/test/stdlib/Span/MutableSpanTests.swift @@ -720,3 +720,25 @@ suite.test("extracting suffixes") expectEqual(span.extracting(droppingFirst: 1).count, b.count) } } + +suite.test("MutableSpan from UnsafeMutableBufferPointer") +.require(.stdlib_6_2).code { + guard #available(SwiftStdlib 6.2, *) else { return } + + let capacity = 4 + let b = UnsafeMutableBufferPointer.allocate(capacity: capacity) + defer { + b.deallocate() + } + _ = b.initialize(fromContentsOf: 0.. Date: Mon, 16 Jun 2025 17:12:51 -0700 Subject: [PATCH 3/3] [test] acknowledge ABI additions --- test/abi/macOS/arm64/stdlib.swift | 2 ++ test/abi/macOS/x86_64/stdlib.swift | 2 ++ 2 files changed, 4 insertions(+) diff --git a/test/abi/macOS/arm64/stdlib.swift b/test/abi/macOS/arm64/stdlib.swift index 4850646c74890..c90a3038778e4 100644 --- a/test/abi/macOS/arm64/stdlib.swift +++ b/test/abi/macOS/arm64/stdlib.swift @@ -933,6 +933,8 @@ Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr +Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr +Added: _$sSw12mutableBytess14MutableRawSpanVvr // _SwiftifyInfo enum for _SwiftifyImports macro Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC diff --git a/test/abi/macOS/x86_64/stdlib.swift b/test/abi/macOS/x86_64/stdlib.swift index dca1157ee9a5b..7319c348e4db4 100644 --- a/test/abi/macOS/x86_64/stdlib.swift +++ b/test/abi/macOS/x86_64/stdlib.swift @@ -934,6 +934,8 @@ Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr +Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr +Added: _$sSw12mutableBytess14MutableRawSpanVvr // _SwiftifyInfo enum for _SwiftifyImports macro Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC