Skip to content

Instantly share code, notes, and snippets.

View jayrhynas's full-sized avatar

Jayson Rhynas jayrhynas

View GitHub Profile
import Foundation
extension URLSession {
private func convert<Value>(_ work: (@escaping (Value?, URLResponse?, Error?) -> Void) -> Void) async throws -> (Value, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
work { value, response, error in
continuation.resume(with: Result {
guard let value = value, let response = response else {
throw error ?? URLError(.badServerResponse)
}
@jayrhynas
jayrhynas / ValidatingDecodable.swift
Last active November 17, 2021 16:30
Enforce a constant value for decoded fields. Heavliy inspired by: https://gist.github.com/IanKeen/4f7717d7afd266137042b86983d04bc1
protocol DecodableType {
func decode(container: KeyedDecodingContainer<AnyCodingKey>, key: String) throws
}
protocol ValidatingDecodable: Decodable {
init()
}
extension ValidatingDecodable {
init(from decoder: Decoder) throws {
import re
def findNeedles(haystack, needles):
if len(needles) > 5:
print "Too many needles"
else:
countArray = [0] * len(needles)
for index, needle in enumerate(needles):
private let configKey = CodingUserInfoKey(rawValue: UUID().uuidString)!
private struct ConfigWrapper<Value> {
let value: Value
}
extension ConfigWrapper: Decodable where Value: DecodableWithConfiguration {
init(from decoder: Decoder) throws {
let config = decoder.userInfo[configKey]! as! Value.DecodingConfiguration
value = try Value(from: decoder, configuration: config)
@jayrhynas
jayrhynas / CustomKeyCodable.swift
Last active February 25, 2021 16:23 — forked from IanKeen/CustomKeyCodable.swift
PropertyWrapper: CustomKeyCodable allows defining the keys for decoding _per property_
protocol CustomKeyCodable: Codable {
static var keyEncodingStrategy: ([CodingKey]) -> CodingKey { get }
static var keyDecodingStrategy: ([CodingKey]) -> CodingKey { get }
init()
}
extension CustomKeyCodable {
init(from decoder: Decoder) throws {
self.init()
import Foundation
class ThreadQueue {
private var mutex = pthread_mutex_t()
private var cond = pthread_cond_t()
// these are shared variables and should only be modified within a `lock` block
private var threadId: pthread_t? = nil
private var isStopped = true
private var queue: [() -> Void] = []
import UIKit
class NavigationController: UINavigationController, UINavigationControllerDelegate {
static let enableMasking = true
static let transitionSpeed: Float = 0.1
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .clear
@jayrhynas
jayrhynas / link.sh
Last active May 6, 2020 14:51
Swift & ObjC CLI Linking
import AppKit
struct Icon {}
struct Canvas {}
protocol ToolController {
var icon: Icon { get }
var name: String { get }
func apply(/*...*/)