Skip to content

Instantly share code, notes, and snippets.

@JensAyton
JensAyton / AnyEquatable.swift
Created Mar 23, 2021
A stab at implementing AnyEquatable with implicit conversions, leveraging AnyHashable. Unfortunately it’s not possible to get the full magic of AnyHashable, in particular with type-erased types.
View AnyEquatable.swift
import Foundation
public struct AnyEquatable: Equatable {
private struct EquatableBox<T: Equatable>: Hashable {
let value: T
func hash(into hasher: inout Hasher) {
preconditionFailure("EquatableBox must never be hashed")
}
}
@JensAyton
JensAyton / Tristate.swift
Created Feb 25, 2021
A very useful type
View Tristate.swift
enum Tristate: ExpressibleByNilLiteral, ExpressibleByBooleanLiteral {
case maybe
case no
case yes
init(nilLiteral: Void) {
self = .maybe
}
init(booleanLiteral value: Bool) {
@JensAyton
JensAyton / StringInits.swift
Created Feb 7, 2020
Surprising init resolution in Swift (5.1 and 5.2)
View StringInits.swift
struct Test {}
extension Test: LosslessStringConvertible {
init?(_ description: String) {
print("unlabelled init")
}
var description: String { "Test" }
}
@JensAyton
JensAyton / BrinfArk.swift
Created Oct 17, 2019
How do you write Swift without using the A key? Reformulate the problem in terms of a language without pesky letters, of course. Unfortunately doesn’t tail recurse. :-(
View BrinfArk.swift
let opIncr: UInt8 = 43 // +
let opDecr: UInt8 = 45 // -
let opPrev: UInt8 = 60 // <
let opNext: UInt8 = 62 // >
let opFwd: UInt8 = 91 // [
let opBck: UInt8 = 93 // ]
let opOutput: UInt8 = 46 // .
let opInput: UInt8 = 44 // ,
let opStop: UInt8 = 0
View blah.cpp
extern "C" {
// This is global, but you can call it what you like
inline void MyPrefixedCFShow(const void *object) {
// Redeclared system function is local scope and counts as extern "C"
extern void CFShow(const void * object);
CFShow(object);
}
}
class DumbThing {
@JensAyton
JensAyton / lolif.m
Created Jan 5, 2017
Something something named arguments
View lolif.m
struct ifparams {
#if __has_feature(objc_arc)
__unsafe_unretained
#endif
dispatch_block_t then, otherwise;
};
static inline void iff(bool condition, struct ifparams params) {
((condition?params.then:params.otherwise)?:^{})();
}
@JensAyton
JensAyton / format.h
Created Dec 10, 2015
Macro for pasting ANSI escape codes together, based on https://twitter.com/velartrill/status/674797360774569984. This version requires GCC/Clang extensions for handling zero-length macro __VA_ARGS__es, because I’m lazy, but it could be done without. Note that this is an awful idea because sooner or later you’ll want to output to a text file and …
View format.h
#ifndef format_h
#define format_h
#define reset "0"
#define bold "1"
#define red "31"
#define format(first, ...) \
"\e[" first join_format_codes(__VA_ARGS__) "m"
@JensAyton
JensAyton / gist:4c6df3f1ca7d32c15fef
Created Jun 9, 2014
try() for discriminated-union error handling in Swift
View gist:4c6df3f1ca7d32c15fef
import Foundation
// Because the real thing crashes
enum Result {
case Value(Any)
case Error(NSError?)
}
@JensAyton
JensAyton / JADictionaryOfVariableBindings.h
Created Apr 28, 2014
NSDictionaryOfVariableBindings at compile time, just because.
View JADictionaryOfVariableBindings.h
#import <Foundation/Foundation.h>
#define JADictionaryOfVariableBindings(...) \
[NSDictionary dictionaryWithObjects:(const id []){ __VA_ARGS__ } \
forKeys:(const NSString *[]){ JATEMPLATE_MAP(JATEMPLATE_NAME_FROM_ARG, __VA_ARGS__) } \
count:JATEMPLATE_ARGUMENT_COUNT(__VA_ARGS__)]
#define JATEMPLATE_NAME_FROM_ARG(ITEM) @#ITEM
@JensAyton
JensAyton / oops.c
Last active Aug 29, 2015 — forked from davepeck/oops.c
One example of cleanup without using goto. (This isn't an opinion on what Apple “should have” done, it’s just an answer to a specific question on Twitter. It would have done nothing to avoid the actual bug.) I also removed the unused variable “signedHashes”.
View oops.c
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
uint8_t *signature, UInt16 signatureLen)
{
SSLBuffer hashCtx;
hashCtx.date = 0;
OSStatus err = SSLVerifySignedServerKeyExchangeInner(ctx, isRsa, signedParams, signature, signatureLen, &hashCtx);
SSLFreeBuffer(hashCtx);