Skip to content

Instantly share code, notes, and snippets.

View mattmassicotte's full-sized avatar

Matt Massicotte mattmassicotte

View GitHub Profile
import AsyncAlgorithms
enum Event<Element: Sendable> {
case element(Element)
case timeout
}
struct TimeoutAsyncSequence<Base, C: Clock>: AsyncSequence where Base : AsyncSequence, Base.Element : Sendable, Base: Sendable {
let base: Base
let interval: C.Instant.Duration
import Foundation
struct TimeoutError: Error {
}
extension AsyncThrowingStream.Continuation where Failure == Error {
func timeoutTask(with interval: TimeInterval) -> Task<Void, Error> {
let sleepNS = UInt64(interval * 1_000_000_000)
return Task {
class NonSendable {
var value = 1
}
class NonIsolated {
private var mutableState = NonSendable()
func aFunc() async {
mutableState.value += 1
}
---Constraint solving at [/Users/matt/Developer/Empire/Tests/EmpireTests/IndexKeyRecordTests.swift:6:1 - line:6:25]---
(overload set choice binding $T2 := (StaticString, StaticString...) -> ())
---Initial constraints for the given expression---
(macro_expansion_expr type="()" location=/Users/matt/Developer/Empire/Tests/EmpireTests/IndexKeyRecordTests.swift:6:1 range=[/Users/matt/Developer/Empire/Tests/EmpireTests/IndexKeyRecordTests.swift:6:1 - line:6:25] name="IndexKeyRecord" discriminator=0
(argument_list
(argument
(string_literal_expr type="$T0" location=/Users/matt/Developer/Empire/Tests/EmpireTests/IndexKeyRecordTests.swift:6:17 range=[/Users/matt/Developer/Empire/Tests/EmpireTests/IndexKeyRecordTests.swift:6:17 - line:6:17] encoding=utf8 value="a" builtin_initializer="**NULL**" initializer="**NULL**"))
(argument
(string_literal_expr type="$T1" location=/Users/matt/Developer/Empire/Tests/EmpireTests/IndexKeyRecordTests.swift:6:22 range=[/Users/matt/Developer/Empire/Tests/EmpireTe
import Foundation
extension NotificationCenter {
public func addOIsolatedObserver<Payload>(
forName name: Notification.Name,
object obj: Any? = nil,
isolation: (any Actor)? = #isolation,
process: @Sendable @escaping (Notification) -> Payload,
using block: @Sendable @escaping (Payload) -> Void
) -> any NSObjectProtocol where Payload : Sendable {

Set value

GCC_PREPROCESSOR_DEFINITIONS = FOO=My Value

Add Constants.m

It is particularly important to check that the definition of your value exists here, as it will not cause a compilation failure, but instead result in the generation of @"FOO".

@import Foundation;
import SwiftUI
import AsyncAlgorithms
struct AsyncChanges<V>: ViewModifier where V : Equatable, V: Sendable {
typealias Element = (oldValue: V, newValue: V)
typealias Action = (AsyncStream<Element>) async -> Void
@State private var streamPair = AsyncStream<Element>.makeStream()
private let action: Action
private let value: V
@mattmassicotte
mattmassicotte / UsesAsyncSequence.swift
Created January 26, 2024 11:52
MainActor + AsyncSequence
@MainActor
final class UsesAsyncSequence {
func original() {
let stream = AsyncStream { 42 }
Task {
// Passing argument of non-sendable type
// 'inout AsyncStream<Int>.Iterator' outside of main
// actor-isolated context may introduce data races
for await value in stream {
@mattmassicotte
mattmassicotte / license.diff
Created December 31, 2023 12:44
Modified 3-Clause BSD
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+1. Redistributions of source code must retain the above copyright notice, this list of conditions, a reference to the original project, and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, a reference to the original project, and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products deri
@mattmassicotte
mattmassicotte / nonglobalactor.swift
Created October 25, 2023 15:14
Use non-global actor protocols with global actor types
import AppKit
// Make a wrapper for the delegate that maps delegate methods to functions
// (Making it a class/inherit from NSObject as needed)
final class TextLayoutManagerDelegateAdapter: NSObject {
public typealias TextLayoutFragmentProvider = (_ location: NSTextLocation, _ textElement: NSTextElement) -> NSTextLayoutFragment
public let textLayoutFragmentProvider: TextLayoutFragmentProvider
public init(