Skip to content

Instantly share code, notes, and snippets.

Brent Royal-Gordon brentdax

View GitHub Profile
View NNNN-cross-import-overlays.md

Introduce cross-import overlays to factor out cross-cutting APIs

  • Proposal: SE-NNNN
  • Authors: Brent Royal-Gordon, Varun Gandhi
  • Review Manager: TBD
  • Implementation: In master behind -Xfrontend -enable-cross-import-overlays
  • Status: Prototyped behind a feature flag

Introduction

View WithUninitialized.swift
/// Passes a pointer to an uninitialized instance of `type` to `body`.
/// If `body` returns `true`, indicating that the memory has now been
/// initialized, returns the instance; otherwise returns `nil`.
@usableFromInline func withUninitialized<T>(
_ type: T.Type, do body: (UnsafeMutablePointer<T>) -> Bool
) -> T? {
let ptr = UnsafeMutablePointer<T>.allocate(capacity: 1)
defer { ptr.deallocate() }
guard body(ptr) else {
View NNNN-magic-file.md
@brentdax
brentdax / ExpressibleByAppendingStringInterpolation.swift
Created Mar 16, 2019
A not-quite-functional way to simplify string interpolation conformances.
View ExpressibleByAppendingStringInterpolation.swift
struct _AppendingStringInterpolation<Result: ExpressibleByStringInterpolation> {
var result: Result
}
protocol ExpressibleByAppendingStringInterpolation: ExpressibleByStringInterpolation where StringInterpolation == _AppendingStringInterpolation<Self> {
init(stringLiteral: String)
mutating func append(_ value: Self)
}
extension ExpressibleByAppendingStringInterpolation where StringInterpolation == _AppendingStringInterpolation<Self> {
View raw.md
@brentdax
brentdax / UserDefaults+Codable.swift
Last active Nov 22, 2019
Encode Codable instances into UserDefaults. Use only with small instances.
View UserDefaults+Codable.swift
//
// UserDefaults+Codable.swift
// Converter UltraLight
//
// Created by Brent Royal-Gordon on 8/31/17.
// Copyright © 2017 Architechies. All rights reserved.
// MIT-licensed, go nuts with it.
//
import Foundation
@brentdax
brentdax / UICollectionViewLayout+indexPaths.swift
Created Jul 26, 2017
Collection for iterating through index paths in a collection view. Could be generalized.
View UICollectionViewLayout+indexPaths.swift
//
// UICollectionViewLayout+indexPaths.swift
// Converter Calc
//
// Created by Brent Royal-Gordon on 7/26/17.
// Copyright © 2017 Architechies. All rights reserved.
//
import UIKit
@brentdax
brentdax / Example.swift
Last active Jul 8, 2017
Explicit conversion operator for Swift.
View Example.swift
import Foundation
import ExplicitlyConvertible
let i8: Int8 = 8
let i64: Int64 = 64
i64 + ^i8 // => 72
struct Person {
var name: String
@brentdax
brentdax / Tuple.swift
Last active Jun 8, 2017
Exploring user-space tuple designs in the quest for variadic generics.
View Tuple.swift
// First, let's test out the basic design. This is basically just an
// HList.
// This recurses to the right because that makes subscripting simpler,
// at the cost of making appending impossible to generalize.
public protocol TupleProtocol: RandomAccessCollection
where Index == Int, IndexDistance == Int, Element == Any
{
associatedtype First
associatedtype Rest //: TupleProtocol
@brentdax
brentdax / JSONFeed.swift
Last active Jun 18, 2020
JSON Feed parser in Swift 4—now updated for Xcode 9 Beta 1. Uses the new Codable protocol and JSONDecoder class.
View JSONFeed.swift
import Foundation
let feedURL = URL(string: "https://daringfireball.net/feeds/json")!
// MARK: Basic models
// We do not model the `userComment` field because it is not supposed to be used by
// machines. We do not model extensions because they should be ignored unless
// supported anyway.
struct Feed {
You can’t perform that action at this time.