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
| import os | |
| /// A property wrapper that turns a stored property into an async stream. | |
| /// | |
| /// Semantics are similar to Combine's `CurrentValueSubject`. Multiple clients | |
| /// are allowed. Each client gets an individual stream. | |
| /// | |
| /// The wrapped value itself inherits isolation from the surrounding type. | |
| /// The stream is nonisolated, so it can be accessed from any execution context. | |
| /// |
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
| import os | |
| @propertyWrapper | |
| public struct Streaming<Wrapped: Sendable> { | |
| public var wrappedValue: Wrapped { | |
| get { value } | |
| set { | |
| value = newValue | |
| let continuations = protectedContinuations.withLock { $0.values } | |
| for continuation in continuations { |
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
| import os.lock | |
| /// `CancellingContinuation` is built on top of `CheckedContinuation` and | |
| /// provides some additional features. It can be used as a drop-in replacement, | |
| /// providing a similar API. | |
| /// | |
| /// ## Automatic cancellation | |
| /// When the suspended task is cancelled the continuation is automatically | |
| /// resumed with a `CancellationError`. After that, normally resuming the | |
| /// continuation from client is silently ignored. |
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
| // NRDelegateProxy is a base class for proxy objects that are used as intercepting | |
| // delegates in other objects. | |
| // | |
| // NRDelegateProxy is subclassed to customize delegate behavior by selectively responding | |
| // to some delegate methods while passing on other methods. It can thus be used to implement | |
| // a chain of delegates where each delegate is responsible for some specific task. | |
| // | |
| // Below is an example using a UITextField. It sets up a chain of delegates that looks as follows: | |
| // | |
| // UITextField -> NRTextFilterProxy -> NRKeyboardHiderProxy -> ViewController |
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 (c) 2016, 2018 Nikolai Ruhe. All rights reserved. | |
| // | |
| import Foundation | |
| public extension FileManager { | |
| /// Calculate the allocated size of a directory and all its contents on the volume. |
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
| import XCTest | |
| final class BarrierTests: XCTestCase { | |
| func test() async { | |
| let subsystem = Subsystem() | |
| await withTaskGroup(of: Void.self) { group in | |
| for index in 0 ..< 100 { | |
| group.addTask { subsystem.performWork(id: index) } | |
| } |
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
| actor A { | |
| var iterator: AsyncStream<Void>.Iterator | |
| init() { | |
| iterator = AsyncStream<Void> { _ in }.makeAsyncIterator() | |
| } | |
| func f() async { | |
| await iterator.next() // Cannot call mutating async function 'next()' on actor-isolated property 'iterator' | |
| } | |
| } |
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 Binding where Value: MutableCollection, Value.Element: Identifiable { | |
| subscript(_ id: Value.Element.ID) -> Binding<Value.Element> { | |
| let index = wrappedValue.firstIndex { $0.id == id } | |
| guard let index else { | |
| fatalError("id not found: \(id)") | |
| } | |
| return self[index] | |
| } | |
| } |
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
| import XCTest | |
| import SwiftUI | |
| final class CaptureListTests: XCTestCase { | |
| func testCaptureList() throws { | |
| withBound("foo") { $boundValue in | |
| print("value: \(boundValue), binding type: \(type(of: $boundValue))") | |
| } | |
| } |
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
| // TaskMeeting | |
| // (c) 2022, Nikolai Ruhe | |
| /// A type that synchronizes progress of two tasks. | |
| public final actor TaskMeeting: Sendable { | |
| private var completion: CheckedContinuation<Void, Error>? = nil | |
| /// This method synchronizes two tasks so that both perform the closure at | |
| /// the same time. Both tasks need to call `rendezvous`. | |
| /// |
NewerOlder