Skip to content

Instantly share code, notes, and snippets.

View macguru's full-sized avatar

Max Seelemann macguru

View GitHub Profile
@macguru
macguru / UncertainFuture.swift
Created February 16, 2026 22:55
Helper for implementing observations using cancellable async/await functions
import Foundation
import Synchronization
/// Synchronization mechanism to support cooperative and cancellable waiting on a value that _might_ become available _at some point in the future_.
public final class UncertainFuture<Value: Sendable>: Sendable {
/// Initializes a new uncertain future.
public init() {}
/// Return the passed value to all currently suspended callers waiting for ``value``.
public func complete(_ value: Value) {
@macguru
macguru / Thing.swift
Created February 16, 2026 22:21
AsyncStream based observation sample
import Foundation
import Synchronization
class Thing {
var value: Int = 0
var observers: [UUID: AsyncStream<Int>.Continuation] = [:]
func nextValue() async throws -> Int {
let id = UUID()
@macguru
macguru / TaskCancellationHandler.swift
Last active November 9, 2025 09:01
Combines withTaskCancellationHandler with a CheckedContinuation.
import Synchronization
func untilCancelled() async {
print("1")
let mutex: Mutex<CheckedContinuation<Void, Never>?> = .init(nil)
await withTaskCancellationHandler {
print("2")
await withCheckedContinuation { continuation in
print("3")
@macguru
macguru / ScopedTask.swift
Last active January 19, 2026 12:03
ScopedTask: A wrapper around Task that auto-cancels when going out of scope.
/// A task that is cancelled if goes out of scope, i.e. the last reference on it has been discarded.
public final class ScopedTask<Success: Sendable, Failure: Error>: Sendable {
/// The underlying task
let wrapped: Task<Success, Failure>
/// Wraps a task into a scoped task.
init(wrapping task: Task<Success, Failure>) {
wrapped = task
}
@macguru
macguru / UnsafeMutablePointer+EmptyCheck.swift
Last active September 27, 2024 14:41
Using SIMD instructions inside a buffer iteration
extension UnsafeMutablePointer<UInt8> {
/// Returns the index of the first element in the buffer that is not `0`
func firstIndexNotZero(size bufferSize: Int) -> Int? {
let stride = MemoryLayout<SIMD16<UInt8>>.stride
let simdSize = bufferSize / stride
// Iterate buffer using 16-component SIMD vectors
let simdIndex: Int? = withMemoryRebound(to: SIMD16<UInt8>.self, capacity: simdSize) { buffer in
let zero = SIMD16<UInt8>(repeating: 0)
return (0..<simdSize).first { buffer[$0] != zero }
@macguru
macguru / ContentView.swift
Created March 8, 2024 08:09
SwiftUI view to try out SF Pro with various widths and weights
import SwiftUI
struct ContentView: View {
@State var width: Float = 0
@State var weight: Float = 0
var body: some View {
VStack {
HStack{
Text("Width")
@macguru
macguru / SwiftUIView.swift
Last active November 9, 2022 17:00
Adapter view to use a (wrapping) SwiftUI view with auto layout, where a certain width is pre-defined
import SwiftUI
public final class SwiftUIView<Content: View>: UIView {
private let hostingController: UIHostingController<Content>
public init(rootView: Content) {
hostingController = UIHostingController(rootView: rootView)
super.init(frame: .zero)
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"languages: %@", [NSLocale.preferredLanguages componentsJoinedByString: @", "]);
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString: @"暗a"];
@macguru
macguru / UIViewController+Rotation.m
Created April 11, 2018 15:30
Forcing a rotation on a UIViewController.
//
// UIViewController+Rotation.m
// Ulysses
//
// Created by Götz Fabian on 12.10.17.
// Copyright © 2017 Ulysses GmbH & Co. KG. All rights reserved.
//
#import "UIViewController+Rotation.h"
@macguru
macguru / SwizzlingTextView.m
Last active January 6, 2018 15:55
Swizzling behaviour into TISmartPunctuationController
@implementation SwizzlingTextView /* Subclass of UITextView */
{
BOOL _swizzled;
}
/* working solution */
- (void)setInputDelegate:(id<UITextInputDelegate>)inputDelegate
{
[super setInputDelegate: inputDelegate];