Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile
#!/bin/sh
# For Ubiquiti EdgeOS
#
# This script creates a directory /config/scripts/post-commit.d. Any script you put
# in that directory will get run every time you commit a new configuration.
#
# Installation:
# - Put this script in /config/scripts/post-config.d. Note that this is a different
# directory from the directory this creates.
#!/bin/sh
set -e
# NOTE: This is no longer necessary! When the IPv6 rollout started, the issue went away.
# I'm just keeping the script here for posterity.
# Verizon Fios's router on the other end of the ONT (one hop upstream of this router) responds to
# any ICMP echo request with an ICMP echo reply, no matter who the intended host is. This breaks
# any traceroute that uses ICMP, including mtr, making it look like the destination host is one
@davidbalbert
davidbalbert / EmptyContainer.swift
Created July 27, 2023 19:23
Conflicting conformances
import Cocoa
struct EmptyContainer<T> {
}
typealias IntContainer = EmptyContainer<Int>
extension IntContainer: Sequence {
struct Iterator: IteratorProtocol {
func next() -> Int? {
@davidbalbert
davidbalbert / notifier.go
Last active April 22, 2023 15:55
Broadcast notifier sketches
// Lightly modified from the slides for "Rethinking Classical Concurrency Patterns"
package sync
import "context"
type state struct {
seq int64
changed chan struct{} // closed upon notify
}
package main
import (
"bufio"
"bytes"
"io"
"strings"
"golang.org/x/term"
)
func commonPrefixLen(a, b string) int {
i := 0
for ; i < len(a) && i < len(b); i++ {
if a[i] != b[i] {
break
}
}
return i
}
struct ContentView: View {
@State var atTop = true
@Namespace var ns
var body: some View {
VStack {
VStack {
Text("Top")
.border(.green)
.opacity(atTop ? 1 : 0)
@davidbalbert
davidbalbert / TransformTest.swift
Created October 2, 2022 23:19
Default CGAffineTransforms in macOS
class NormalView: NSView {
override func draw(_ dirtyRect: NSRect) {
print("NormalView", NSGraphicsContext.current!.cgContext.ctm)
}
}
struct NormalViewRepresentable: NSViewRepresentable {
func makeNSView(context: Context) -> NormalView {
NormalView()
}
@davidbalbert
davidbalbert / AnyEquatable.swift
Created September 21, 2022 02:52
Three implementations of a type-erased Equatable
// Three implementations of a type-erased Equatable
struct AnyEquatable: Equatable {
class BoxBase {
func isEqual(_ other: BoxBase) -> Bool {
fatalError("not implemented")
}
}
class Box<E: Equatable>: BoxBase {
@davidbalbert
davidbalbert / main.m
Last active August 5, 2022 01:00
NSPasteboard UTF-16 nonsense
#import <Cocoa/Cocoa.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
@implementation NSData (NSArrayConversion)
- (NSArray<NSNumber*>*)byteArray {
NSMutableArray<NSNumber*> *a = [NSMutableArray arrayWithCapacity:[self length]];
const unsigned char *bytes = [self bytes];
for (int i = 0; i < [self length]; i++) {
a[i] = [[NSNumber alloc] initWithUnsignedChar:bytes[i]];