Skip to content

Instantly share code, notes, and snippets.

Russ Bishop russbishop

Block or report user

Report or block russbishop

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@russbishop
russbishop / DictionaryAppend.swift
Last active Nov 15, 2016
Append to array inside a dictionary without copying
View DictionaryAppend.swift
/// 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 Oct 28, 2016
OptionalCompare.swift
View 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 Sep 9, 2016
Make ExpressibleByStringLiteral tolerable
View StringLiteral.swift
// 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 Aug 2, 2018
Type erasure with multiple adopting types
View TypeErasure.swift
// Paste me into a playground!
import Cocoa
//: # Basic Setup
protocol FancyProtocol {
associatedtype Thing
func holdPinkyUp(x: Thing)
}
@russbishop
russbishop / _readme.txt
Last active Sep 17, 2017
Swift Toolchain & Build Files
View _readme.txt
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
Allow use of NSFileHandle with print()
View NSFileHandleExtensions.swift
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
PointEncoder from russbishop.net/packing-bytes-in-swift
View PointEncoder.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
View individual-swift-tests.md

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
}
View UnsafeArray.swift
// 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 Mar 17, 2016
Make Quick & Nimble tests much more pleasant in Swift
View QuickExtensions.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
You can’t perform that action at this time.