Files
swift-collections/Sources/Future/Containers/UnsafeMutableBufferPointer+ContiguousContainer.swift
Karoy Lorentey fea1d24e7b Flesh out DynamicArray
Also:
- Tweak RigidArray.
- Define some default implementations for Container requirements.
- Beef up ContiguousContainer.
- Add support for testing contiguous access on minimal collections.
2025-04-30 19:41:15 -07:00

33 lines
1.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections 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
//
//===----------------------------------------------------------------------===//
@available(SwiftStdlib 6.2, *)
extension UnsafeMutableBufferPointer: @unsafe ContiguousContainer
where Element: ~Copyable
{
/// The contents of `self` must not be mutated while the returned span exists.
public var span: Span<Element> {
@inlinable
@lifetime(borrow self)
get {
unsafe Span(_unsafeElements: self)
}
}
@inlinable
@lifetime(borrow self)
public func borrowElement(at index: Int) -> Borrow<Element> {
precondition(index >= 0 && index < count, "Index out of bounds")
let ptr = unsafe baseAddress.unsafelyUnwrapped + index
return unsafe Borrow(unsafeAddress: ptr, borrowing: self)
}
}