Skip to content

Instantly share code, notes, and snippets.

@russbishop
russbishop / DictionaryAppend.swift
Last active November 15, 2016 16:00
Append to array inside a dictionary without copying
/// Appends a value to an array within a dictionary without triggering a copy.
/// Necessary in Swift 3 but expected to be obsoleted as the way inout works
/// will be changed (eliminating the need for the write-back copy)
func append<K, V>(value: V, toKey key: K, in dict: inout [K : Array<V>]) {
var a: [V]? = []
swap(&a, &dict[key])
a = a ?? []
a!.append(value)
swap(&a, &dict[key])
@russbishop
russbishop / OptionalCompare.swift
Last active October 28, 2016 18:20
OptionalCompare.swift
func < <T: Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}
@russbishop
russbishop / StringLiteral.swift
Created September 9, 2016 18:31
Make ExpressibleByStringLiteral tolerable
// If you want types initializable from String literals
// but don't want to implement three separate initializers.
extension ExpressibleByUnicodeScalarLiteral where Self: ExpressibleByStringLiteral, Self.StringLiteralType == String {
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
}
@russbishop
russbishop / TypeErasure.swift
Last active May 18, 2022 01:20
Type erasure with multiple adopting types
// Paste me into a playground!
import Cocoa
//: # Basic Setup
protocol FancyProtocol {
associatedtype Thing
func holdPinkyUp(x: Thing)
}
@russbishop
russbishop / _readme.txt
Last active February 22, 2023 05:59
Swift Toolchain & Build Files
Use at your own risk! Swift moves quickly so it is possible
info in these scripts is out of date.
To use build-redist-package.sh you'll need a Mac Developer ID certificate.
The build will take a long time but the custom my-presets.ini
file skips tests to speed things up a fair amount.
rebuild-toolchain.sh builds your own Personal local toolchain
over top of the previous one, so if you symlink it into the
actual Toolchains directory you can always have the most
@russbishop
russbishop / NSFileHandleExtensions.swift
Created May 31, 2016 19:52
Allow use of NSFileHandle with print()
extension NSFileHandle: OutputStreamType {
public func write(string: String) {
if let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) {
self.writeData(data)
}
}
}
@russbishop
russbishop / PointEncoder.swift
Last active May 17, 2016 17:49
PointEncoder from russbishop.net/packing-bytes-in-swift
// Written by Russ Bishop
// MIT licensed, use freely.
// No warranty, not suitable for any purpose. Use at your own risk!
struct PointEncoder {
// When parsing if we get a wildly large value we can
// assume denial of service or corrupt data.
static let MaxPoints = 1_310_719
// How big an Int64 is

Build Swift first using utils/build-script -R, other settings will need different directories below. Add these lines to ~/.bashrc, call with run-test path/to/test:

LLVM_DIR="/path/to/Swift/llvm"
SWIFT_BUILD_DIR="/path/to/Swift/build/Ninja-ReleaseAssert/swift-macosx-x86_64"

run-validation-test() {
    $LLVM_DIR/utils/lit/lit.py -a --param swift_site_config=$SWIFT_BUILD_DIR/validation-test-macosx-x86_64/lit.site.cfg $1
}
@russbishop
russbishop / UnsafeArray.swift
Last active October 2, 2016 00:49
UnsafeArray
// MIT license, no warranty.
// Now updated for Swift 3.0
// Warning: This is just example code. In production you would want
// unit tests and ASAN tests.
/**
A thin wrapper over an unsafe buffer.
- warning: For the rare case where performance profiling has proven native Swift `Array` to be a problem.
@russbishop
russbishop / QuickExtensions.swift
Created March 17, 2016 00:49
Make Quick & Nimble tests much more pleasant in Swift
//
// QuickExtensions.swift
// PlanLib
//
// Created by Russ Bishop on 3/16/16.
// Copyright © 2016 PlanGrid. All rights reserved.
// MIT Licensed, use freely.
import Foundation
import Quick