This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @propertyWrapper | |
| struct UnsafeReference<T> { | |
| public let address: UnsafePointer<T> | |
| var projectedValue: Self { self } | |
| init(_ address: UnsafePointer<T>) { | |
| self.address = address | |
| } | |
| var wrappedValue: T { | |
| get { address.pointee } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2020 Penguin Authors | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Protocol to use for type erasure. | |
| protocol Summable { | |
| func sum() -> Int | |
| } | |
| // Concrete model. Type | |
| extension Int: Summable { | |
| func sum() -> Int { self } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol P { var pop: String { get } } | |
| extension P { | |
| var pop: String { "slow" } | |
| var pop1: String { pop } | |
| } | |
| protocol Q: P {} | |
| extension Q { var pop: String { "fastQ" } } | |
| protocol R: P {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Classes whose initializers actually create derived classes | |
| protocol FactoryInitializable { | |
| /// The type of the least-derived class declared to be FactoryInitializable. | |
| /// | |
| /// - Warning: Do not define this in your FactoryInitializable type! | |
| associatedtype FactoryBase: AnyObject, FactoryInitializable = Self | |
| // This associatedtype is a trick that captures `Self` at the point where | |
| // `FactoryInitializable` enters a class hierarchy; in other contexts, `Self` | |
| // refers to the most-derived type. | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// A collection formed by adapting some `Base` collection, typically | |
| /// displaying some altered form of the base collection's behavior. | |
| /// | |
| /// Default implementations of all requirements are provided, forwarding their | |
| /// implementations to the `Base` collection instance. | |
| public protocol CollectionAdapter: Collection { | |
| /// The type of the underlying collection being adapted. | |
| associatedtype Base: Collection | |
| /// The instance of the underlying collection being adapted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if false | |
| Changes to Unmanaged | |
| -public struct Unmanaged<Instance : AnyObject> { | |
| // New API: `UnsafeReference(bitPattern:)`. | |
| - public static func fromOpaque(value: COpaquePointer) -> Unmanaged | |
| // New API: `OpaquePointer(bitPattern:)`. | |
| - public func toOpaque() -> COpaquePointer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// A Dictionary with a nominal `Element` type, that can conform to things. | |
| @frozen public struct Dictionary2<Key: Hashable, Value> { | |
| public typealias Base = [Key : Value] | |
| /// A view of a dictionary's keys. | |
| public typealias Keys = Base.Keys | |
| /// A view of a dictionary's values. | |
| public typealias Values = Base.Values | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // (A) Definition | |
| protocol P { | |
| static var isEquatable: Bool { get } | |
| } | |
| // (B) Default implementation | |
| extension P { | |
| static var isEquatable: Bool { false } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Sequence { | |
| var array: Array<Element> { | |
| print("preallocating", self.underestimatedCount) | |
| return Array(self) | |
| } | |
| } | |
| _ = Array(0..<1000).reversed().array // preallocating 1000 | |
| _ = repeatElement(1..<10, count: 200).joined().array // preallocating 0 |