Skip to content

Instantly share code, notes, and snippets.

enum MyError: Error {
case one
case two
}
struct MyTest {
private func funcWithTypedThrow() async throws(MyError) {…}
// example where typed-throw incorrectly concludes catch is not exhaustive
@propertyWrapper
struct Synchronized<T>: @unchecked Sendable {
private var _wrappedValue: T
private let lock = NSLock()
var wrappedValue: T {
get { lock.withLock { _wrappedValue } }
set { lock.withLock { _wrappedValue = newValue } }
}
final class ComplexData: @unchecked Sendable {
private let lock = NSLock()
private var _name: String
var name: String {
get { lock.withLock { _name } }
set { lock.withLock { _name = newValue } }
}
init(name: String) {
@robertmryan
robertmryan / DispatchQueue+Chunked.swift
Last active August 31, 2024 18:16
Chunking of GCD’s concurrentPerform
import Foundation
extension DispatchQueue {
/// Chunked concurrentPerform
///
/// - Parameters:
///
/// - iterations: How many total iterations.
///
/// - chunks: How many chunks into which these iterations will be divided. This is optional and will default to
func VTT2SRT() {
let targetPattern = /(?<timeStamp>\d\d:\d\d:\d\d)\./
var vtt = vttText.replacing(targetPattern) { $0.timeStamp + "," }
// …
}
extension Task {
static func bar() {
Task<Void, Error> {
try await foo()
}
}
}
extension Task where Success == Void, Failure == Error {
static func baz() {
class PetAnnotation: MKPointAnnotation {
let type: PetType
init(_ type: PetType, title: String? = nil, subtitle: String? = nil, latitude: Double, longitude: Double) {
self.type = type
super.init()
self.title = title
self.subtitle = subtitle
let regex = Regex {
Capture {
ZeroOrMore {
OneOrMore(.word)
"."
}
OneOrMore(.word)
}
"@"
Capture {
let emailRegex = /((?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*"))@((?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))/
let redactText = "…"
let maxUserNameLength = 24
func redactEmail(for line: String) -> String {
line.replacing(emailRegex) { match in
let (_, name, domain) = match.output
// If the username is too long, it might be a false positive for an email.
return name.count > maxUserNameLength ? "\(name)@\(domain)" : redactText
@discardableResult
public func send(_ action: Action) async throws -> State {
try Task.checkCancellation()
let effect = await reducer.reduce(into: &viewState, action: action)
if let nextAction = await effect.run() {
return try await send(nextAction)
}