Skip to content

Commit 70932ae

Browse files
committed
[stdlib] add URMBP.mutableBytes accessor
1 parent b15d308 commit 70932ae

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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/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+
}

0 commit comments

Comments
 (0)