//===--- MutableSpanSlicing.swift -----------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2025 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// //MARK: Option 1 extracting() function @available(macOS 9999, *) extension MutableSpan where Element: ~Copyable { @lifetime(borrow self) public mutating func extracting(_ bounds: Range) -> Self { precondition( UInt(bitPattern: bounds.lowerBound) <= UInt(bitPattern: _count) && UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count), "Index range out of bounds" ) let newSpan = MutableSpan(_unchecked: _start(), uncheckedBounds: bounds) return unsafe _overrideLifetime(newSpan, mutating: &self) } } @available(macOS 9999, *) extension MutableSpan where Element: ~Copyable { @_alwaysEmitIntoClient @lifetime(borrow pointer) fileprivate init( _unchecked pointer: UnsafeMutableRawPointer, uncheckedBounds bounds: Range ) { let delta = bounds.lowerBound &* MemoryLayout.stride let newStart = unsafe pointer.advanced(by: delta) let newSpan = Self( _unchecked: newStart, count: bounds.upperBound &- bounds.lowerBound ) self = unsafe _overrideLifetime(newSpan, borrowing: pointer) } } @available(macOS 9999, *) extension Span where Element: ~Copyable { @_alwaysEmitIntoClient @lifetime(borrow pointer) fileprivate init( _unchecked pointer: UnsafeMutableRawPointer, uncheckedBounds bounds: Range ) { let mut = MutableSpan(_unchecked: pointer, uncheckedBounds: bounds) let newSpan = mut.span self = unsafe _overrideLifetime(newSpan, borrowing: pointer) } } //MARK: Option 2 extracting subscript @available(macOS 9999, *) extension MutableSpan where Element: ~Copyable { public subscript(extracting bounds: Range) -> Self { @lifetime(borrow self) mutating get { precondition( UInt(bitPattern: bounds.lowerBound) <= UInt(bitPattern: _count) && UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count), "Index range out of bounds" ) let newSpan = MutableSpan(_unchecked: _start(), uncheckedBounds: bounds) return unsafe _overrideLifetime(newSpan, mutating: &self) } } } //MARK: Option 3 specific slicing wrapper type @available(macOS 9999, *) public struct SubMutableSpan : ~Copyable, ~Escapable { public typealias Index = MutableSpan.Index public fileprivate(set) var offset: MutableSpan.Index public /*exclusive*/ var base: MutableSpan init(_ _offset: Index, _ _base: consuming MutableSpan) { offset = _offset base = _base } public var count: Int { base.count - offset } } @available(macOS 9999, *) extension SubMutableSpan where Element: ~Copyable { public subscript (index: Index) -> Element { unsafeAddress { precondition(offset <= index && index < base.count) return unsafe UnsafePointer(base._unsafeAddressOfElement(unchecked: index)) } unsafeMutableAddress { precondition(offset <= index && index < base.count) return unsafe base._unsafeAddressOfElement(unchecked: index) } } public var span: Span { @lifetime(borrow self) borrowing get { let newSpan = Span( _unchecked: base._start(), uncheckedBounds: offset..) -> SubMutableSpan { @lifetime(borrow self) mutating get { precondition( UInt(bitPattern: bounds.lowerBound) <= UInt(bitPattern: _count) && UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count), "Index range out of bounds" ) let prefix = Self( _unchecked: _start(), uncheckedBounds: 0..(bounds.lowerBound, prefix) return unsafe _overrideLifetime(subSpan, mutating: &self) } } } //MARK: Option 4 range parameters to bulk mutation functions @available(macOS 9999, *) extension MutableSpan where Element: Copyable { public mutating func update( in bounds: Range, repeating repeatedValue: consuming Element ) { precondition( UInt(bitPattern: bounds.lowerBound) <= UInt(bitPattern: _count) && UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count), "Index range out of bounds" ) let start = unsafe _start() + bounds.lowerBound &* MemoryLayout.stride unsafe start.withMemoryRebound(to: Element.self, capacity: bounds.count) { unsafe $0.update(repeating: repeatedValue, count: bounds.count) } } }