mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
Package traits already pass defines with their trait name when building. Instead of adding our own conditional defines based on the traits we can use the trait provided defines directly. I observed that various tooling behaves better with the trait provided defines.
119 lines
3.3 KiB
Swift
119 lines
3.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2025 - 2026 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
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// A type that tracks the number of live instances.
|
|
///
|
|
/// `LifetimeTracked` is useful to check for leaks in algorithms and data
|
|
/// structures. The easiest way to produce instances is to use the
|
|
/// `withLifetimeTracking` function:
|
|
///
|
|
/// class FooTests: XCTestCase {
|
|
/// func testFoo() {
|
|
/// withLifetimeTracking([1, 2, 3]) { instances in
|
|
/// _ = instances.sorted(by: >)
|
|
/// }
|
|
/// }
|
|
/// }
|
|
@frozen
|
|
public struct LifetimeTrackedStruct<Payload: ~Copyable>: ~Copyable {
|
|
public let tracker: LifetimeTracker
|
|
|
|
@usableFromInline
|
|
internal var serialNumber: Int = 0
|
|
|
|
public var payload: Payload
|
|
|
|
@inlinable
|
|
public init(_ payload: consuming Payload, for tracker: LifetimeTracker) {
|
|
tracker._instances += 1
|
|
tracker._nextSerialNumber += 1
|
|
self.tracker = tracker
|
|
self.serialNumber = tracker._nextSerialNumber
|
|
self.payload = payload
|
|
}
|
|
|
|
public init(copying payload: Payload, for tracker: LifetimeTracker)
|
|
where Payload: Copyable
|
|
{
|
|
tracker._instances += 1
|
|
tracker._nextSerialNumber += 1
|
|
self.tracker = tracker
|
|
self.serialNumber = tracker._nextSerialNumber
|
|
self.payload = payload
|
|
}
|
|
|
|
@inlinable
|
|
deinit {
|
|
precondition(serialNumber != 0, "Double deinit")
|
|
tracker._instances -= 1
|
|
// Can't mutate in deinit yet
|
|
// serialNumber = -serialNumber
|
|
}
|
|
}
|
|
|
|
#if compiler(>=6.2)
|
|
extension LifetimeTrackedStruct: TestPrintable where Payload: TestPrintable & ~Copyable {
|
|
public var testDescription: String {
|
|
return "\(payload.testDescription)"
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if compiler(>=6.4) && UnstableHashedContainers
|
|
extension LifetimeTrackedStruct: Equatable where Payload: Equatable & ~Copyable {
|
|
public static func == (left: borrowing Self, right: borrowing Self) -> Bool {
|
|
return left.payload == right.payload
|
|
}
|
|
}
|
|
|
|
extension LifetimeTrackedStruct: Hashable where Payload: Hashable & ~Copyable {
|
|
public func _rawHashValue(seed: Int) -> Int {
|
|
payload._rawHashValue(seed: seed)
|
|
}
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
hasher.combine(payload)
|
|
}
|
|
|
|
public var hashValue: Int {
|
|
payload.hashValue
|
|
}
|
|
}
|
|
#else
|
|
extension LifetimeTrackedStruct/*: Equatable*/ where Payload: Equatable /*& ~Copyable*/ {
|
|
public static func == (left: borrowing Self, right: borrowing Self) -> Bool {
|
|
return left.payload == right.payload
|
|
}
|
|
}
|
|
|
|
extension LifetimeTrackedStruct/*: Hashable*/ where Payload: Hashable /*& ~Copyable*/ {
|
|
public func _rawHashValue(seed: Int) -> Int {
|
|
payload._rawHashValue(seed: seed)
|
|
}
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
hasher.combine(payload)
|
|
}
|
|
|
|
public var hashValue: Int {
|
|
payload.hashValue
|
|
}
|
|
}
|
|
#endif
|
|
|
|
extension LifetimeTrackedStruct/*: Comparable*/ where Payload: Comparable /*& ~Copyable*/ {
|
|
public static func < (left: borrowing Self, right: borrowing Self) -> Bool {
|
|
return left.payload < right.payload
|
|
}
|
|
}
|