Skip to content

Instantly share code, notes, and snippets.

View mayoff's full-sized avatar
😷
status messages are fun

Rob Mayoff mayoff

😷
status messages are fun
View GitHub Profile
//
// ContentView.swift
// Layout
//
// Created by Matt Gallagher on 7/6/19.
// Copyright © 2019 Matt Gallagher. All rights reserved.
//
import SwiftUI
func testIsBidirectional() {
func assert<C: Collection>(_ collection: C, isBidirectional: Bool) {
XCTAssertEqual(collection.isBidirectional, isBidirectional)
}
assert([1, 2, 3], isBidirectional: true)
assert(Set([1, 2, 3]), isBidirectional: false)
}
extension Collection {
@dabrahams
dabrahams / Dispatch.swift
Created April 25, 2020 13:37
Post-hoc specialized behavior based on refinement
/// A repository of functionality depending on the conformances of `Model`.
///
/// Conditional conformances provide implementation functions that take a
/// generic argument type with the safe assumption that the argument's concrete
/// type is `Model`.
struct Dispatch<Model> {
/// Returns `f(a as! Model) as! R1`
///
/// Used by implementation functions to avoid the clutter of casting
/// explicitly.
@dabrahams
dabrahams / FactoryInitialization.swift
Last active October 22, 2022 13:46
Class factory initializers
/// Classes whose initializers actually create derived classes
protocol FactoryInitializable {
/// The type of the least-derived class declared to be FactoryInitializable.
///
/// - Warning: Do not define this in your FactoryInitializable type!
associatedtype FactoryBase: AnyObject, FactoryInitializable = Self
// This associatedtype is a trick that captures `Self` at the point where
// `FactoryInitializable` enters a class hierarchy; in other contexts, `Self`
// refers to the most-derived type.
}
import SwiftUI
import Combine
public struct ChangeObserver<V: Equatable>: ViewModifier {
public init(newValue: V, action: @escaping (V) -> Void) {
self.newValue = newValue
self.newAction = action
}
private typealias Action = (V) -> Void
@schwa
schwa / libfuzzer.md
Last active February 27, 2024 18:45
Using LLVM's libfuzzer with Swift

Using LLVM's libfuzzer with Swift

Background

Of the many tools available for fuzzing, I've found that LLVM's libfuzzer is the easiest to use for Swift on macOS. Other tools seem to have a list of requirements taht are difficult to meet. libfuzzer on the other hand is built into LLVM and is available on macOS in the custom Swift toolchains: https://www.swift.org/download/

In this document I'll describe how to use libfuzzer with Swift and Swift Packages.

I used this setup to fuzz an SVG Renderer package that I am building. I was able to find and fix a number of bugs in my SVG parsing code using libfuzzer in basically no time at all.