Skip to content

Instantly share code, notes, and snippets.

View swhitty's full-sized avatar

Simon Whitty swhitty

View GitHub Profile
@swhitty
swhitty / WeakSet.swift
Last active February 12, 2021 22:05
Swift container that weakly holds references to objects.
import Foundation
public struct WeakSet<Element>: Sequence, ExpressibleByArrayLiteral, CustomStringConvertible where Element: AnyObject {
private var storage = Set<Box>()
public init(_ members: [Element]) {
members.forEach { insert($0) }
}
@swhitty
swhitty / CGPathApply.swift
Created February 27, 2017 11:15
Bridges CoreGraphics.CGPathApplierFunction with a standard Swift closure
extension CGPath {
func apply(action: @escaping (CGPathElement)->()) {
var action = action
apply(info: &action) {
let action = $0!.bindMemory(to: ((CGPathElement)->()).self, capacity: 1).pointee
action($1.pointee)
}
}
}
struct AnyEquatable: Equatable {
let base: Any
let isEqual: (_ other: AnyEquatable) -> Bool
init<T: Equatable>(_ base: T) {
self.base = base
self.isEqual = { base == $0.base as? T }
}
public static func ==(lhs: AnyEquatable, rhs: AnyEquatable) -> Bool {