Skip to content

Instantly share code, notes, and snippets.

View KaQuMiQ's full-sized avatar

Kacper Kaliński KaQuMiQ

View GitHub Profile
import OSLog
import class Foundation.Bundle
import class Foundation.ProcessInfo
import struct Foundation.TimeZone
/// Diagnostics allows debugging and gathering usage data.
public enum Diagnostics {
/// Info including application name and version.
public static let info: String = {
@KaQuMiQ
KaQuMiQ / Task+Current.swift
Last active July 25, 2023 10:59
Check if task is currently running one
extension Optional
where Wrapped == Task<Void, Never> {
public mutating func clearIfCurrent() {
switch self {
case .some(let task) where task.isCurrent:
self = .none
case _:
break // noop
@KaQuMiQ
KaQuMiQ / AnyEncodable.swift
Created March 21, 2022 15:01
JSONEncoder + any Encodable
import Foundation
extension JSONEncoder {
public func encode(
any anyEncodable: any Encodable
) throws -> Data {
try self.encode(
JSONEncodingWrapper(encodable: anyEncodable)
)
@KaQuMiQ
KaQuMiQ / AsyncSpinLock.swift
Created March 14, 2022 16:02
SpinLock with async/await
import libkern
@usableFromInline
internal final class AsyncSpinLock {
@TaskLocal private static var taskContext: Array<AsyncSpinLock> = .init()
@usableFromInline
internal let atomicFlagPointer: UnsafeMutablePointer<atomic_flag>
@KaQuMiQ
KaQuMiQ / PublisherAsyncSequence.swift
Last active February 25, 2022 09:09
Combine Publisher to AsyncSequence
import Combine
extension Publisher {
public func asAsyncThrowingSequence() -> StreamedAsyncThrowingSequence<Output> {
StreamedAsyncThrowingSequence(self)
}
}
extension Publisher where Failure == Never {
import struct Foundation.UUID
import os
import libkern
public enum MemoryLeaks {
public static func listActiveMarkers() -> Array<Marker> {
let markersRegistryCopy: Dictionary<AnyHashable, WeakMarker> = markersRegistryLock
.withLock { Self.markersRegistry }
return markersRegistryCopy.values.compactMap(\.marker)
public struct Tagged<RawValue, Type>: RawRepresentable {
public var rawValue: RawValue
public init(
rawValue: RawValue
) {
self.rawValue = rawValue
}
}
import Foundation
import SQLite3
private final class SQLiteHandle {
fileprivate var handle: OpaquePointer?
fileprivate init(handle: OpaquePointer?) {
self.handle = handle
}
@KaQuMiQ
KaQuMiQ / Future.swift
Created April 12, 2021 07:44
Lock-free Future experiment
import Atomics
public final class Future<Value> {
private let fulfillmentStatus: ManagedAtomic<Bool>
private let valueStatus: ManagedAtomic<Bool>
private let handlingStatus: ManagedAtomic<Bool>
private let handlerStatus: ManagedAtomic<Bool>
private let completionStatus: ManagedAtomic<Bool>
@KaQuMiQ
KaQuMiQ / Cancelation.swift
Created March 24, 2021 09:37
Data tunnels
import struct Foundation.TimeInterval
import struct Foundation.Date
import class Atomics.ManagedAtomic
/// Cancelation token that can be used to cancel associated tasks.
public struct Cancelation {
private var status: () -> Bool
private var cancelation: () -> Void
}