Skip to content

Instantly share code, notes, and snippets.

View ts95's full-sized avatar
🦉
🍰

Toni Sučić ts95

🦉
🍰
View GitHub Profile
@ts95
ts95 / Cache.swift
Last active January 15, 2023 21:52
Hashable & Codable struct-based cache implementation in Swift
import OrderedCollections
protocol HasCacheCost {
var cacheCost: Int { get }
}
extension HasCacheCost {
var cacheCost: Int {
type(of: self) == AnyObject.self ? 0 : MemoryLayout.size(ofValue: self)
}
@ts95
ts95 / Result+Codable.swift
Created January 7, 2023 18:41
Makes the Result type in Swift conform to Codable
import Foundation
extension Result: Codable where Success: Codable, Failure: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
switch try container.decode(ResultType.self, forKey: .type) {
case .success:
self = .success(try container.decode(Success.self, forKey: .value))
case .failure:
@ts95
ts95 / AutoId.swift
Created January 5, 2023 09:25
Swift port of the ID generator used in Firebase Firestore
import Foundation
class AutoId {
private init() {}
static func newId() -> String {
// Alphanumeric characters
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
// The largest byte value that is a multiple of `char.length`.
let maxMultiple = Int(256 / chars.count) * chars.count
@ts95
ts95 / Injected.swift
Created September 26, 2022 10:03
Swift basic singleton-based dependency injection
import Foundation
@propertyWrapper
struct Injected<T> {
private let keyPath: WritableKeyPath<InjectedValues, T>
var wrappedValue: T {
get { InjectedValues[keyPath] }
set { InjectedValues[keyPath] = newValue }
}
@ts95
ts95 / AppendOnlyDatabase+TextEditor.swift
Created March 29, 2022 18:51
Append-only database + Text editor
import Foundation
protocol AppendOnlyDatabaseProtocol {
var count: Int { get }
mutating func append(_ other: Data)
subscript(index: Data.Index) -> UInt8 { get }
subscript(bounds: Range<Data.Index>) -> Data { get }
}
@ts95
ts95 / keybase.md
Last active February 8, 2021 10:10
keybase.md

Keybase proof

I hereby claim:

  • I am ts95 on github.
  • I am tonisucic (https://keybase.io/tonisucic) on keybase.
  • I have a public key ASCGFglVtkxTyx1EAXY733WiiJFdpaZ9yOOloqybTnHorAo

To claim this, I am signing this object:

@ts95
ts95 / CLLocationCoordinate2D+Midpoint.swift
Last active January 27, 2024 01:35
CLLocationCoordinate2D + midpoint function extension
import Foundation
import CoreLocation
// based on https://gis.stackexchange.com/a/18740
extension CLLocationCoordinate2D {
var cartesian: CartesianCoordinate3D { .init(from: self) }
// Returns the coordinates of the midpoint between point a and b.
// Limitation: point a and b may not be diametrically opposite.
@ts95
ts95 / swiftlint.sh
Created October 11, 2020 11:40
Only run SwiftLint for chunks that differ from HEAD
#!/bin/bash
# Parameters
source="$(pwd)/" # Track folder source where diffs are checked
project_root=${0%/*} # Track project root folder
swiftlint_path='swiftlint' # Track SwiftLint path
changed_files () {
sort -u \
<(git diff --name-only --diff-filter=d --cached) \
import SwiftUI
import MapKit
extension Map {
init<Items>(
coordinateRegion: Binding<MKCoordinateRegion>,
interactionModes: MapInteractionModes = .all,
showsUserLocation: Bool = false,
userTrackingMode: Binding<MapUserTrackingMode>? = nil,
import SwiftUI
import MapKit
struct AnyMapAnnotation: MapAnnotationProtocol {
let _annotationData: _MapAnnotationData
let base: Any
init<T: MapAnnotationProtocol>(_ base: T) {
self._annotationData = base._annotationData
self.base = base