Files
2024-08-07 19:12:05 -07:00

60 lines
1.7 KiB
Plaintext

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 2024 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
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import *
}%
${autogenerated_warning()}
% for modifier in visibility_levels:
${visibility_boilerplate(modifier)}
extension UnsafeBufferPointer where Element: ~Copyable {
@inlinable
@inline(__always)
${modifier} static var _empty: Self {
.init(start: nil, count: 0)
}
@inlinable
@inline(__always)
${modifier} func _ptr(at index: Int) -> UnsafePointer<Element> {
assert(index >= 0 && index < count)
return baseAddress.unsafelyUnwrapped + index
}
@_alwaysEmitIntoClient
${modifier} func _extracting(unchecked bounds: Range<Int>) -> Self {
assert(bounds.lowerBound >= 0 && bounds.upperBound <= count,
"Index out of range")
guard let start = self.baseAddress else {
return Self(start: nil, count: 0)
}
return Self(start: start + bounds.lowerBound, count: bounds.count)
}
@inlinable
${modifier} func _extracting(first n: Int) -> Self {
assert(n >= 0)
if n >= count { return self }
return extracting(Range(uncheckedBounds: (0, n)))
}
@inlinable
${modifier} func _extracting(last n: Int) -> Self {
assert(n >= 0)
if n >= count { return self }
return extracting(Range(uncheckedBounds: (count - n, count)))
}
}
% end
${visibility_boilerplate("end")}