Skip to content

Instantly share code, notes, and snippets.

@available(iOS 16.0, macOS 13.0, watchOS 9.0, *)
@propertyWrapper
public struct AsyncPublished<Value> {
@available(*, unavailable, message: "@AsyncPublished is only available on properties of classes")
public var wrappedValue: Value {
get { fatalError() }
set { fatalError() } // swiftlint:disable:this unused_setter_value
}
public let projectedValue: AsyncStream<Value>
public init(initialValue: Value) {
import Foundation
/*
Somewhere in Foundation
public protocol Foo {
...
public init<T>?(foo value: T) where T: Foo
...
}
protocol Example {
associatedtype Value
func value() -> Value
}
struct AnyExample<Value> {
private class Container<Value> {
func value() -> Value {
fatalError()
}
@propertyWrapper
struct ErasedToAnyPublisher<Upstream: Publisher> {
var wrappedValue: AnyPublisher<Upstream.Output, Upstream.Failure> {
upstream.eraseToAnyPublisher()
}
let upstream: Upstream
init(upstream: Upstream) {
self.upstream = upstream
}
}
public extension Publisher {
/// Assigns each element from a Publisher to a property on an object.
///
/// - Parameters:
/// - keyPath: The key path of the property to assign.
/// - object: The object on which to assign the value.
/// - Returns: A cancellable instance; used when you end assignment of the received value. Deallocation of the result will tear down the subscription stream.
func assignResult<Root>(to keyPath: ReferenceWritableKeyPath<Root, Result<Self.Output, Self.Failure>>,
on object: Root) -> AnyCancellable {
sink(receiveCompletion: { completion in
class Foo {
var body: some View {
...
}
}
class Bar: Foo {
override var body: some View {
...
}
//
// BoundedQueue.swift
//
// Copyright © 2017 Doug Russell. All rights reserved.
//
import Foundation
public class BoundedQueue {
private let group = DispatchGroup()
@rustle
rustle / gist:6618451
Created September 19, 2013 02:32
A __attribute__ decorator that causes method calls to become implicit dispatches could be interesting: __attribute__((dispatch, async, priority))
#import <Foundation/Foundation.h>
#include <objc/message.h>
typedef void (*DISPATCH_IMP_NOARGS_NORETURN)(id, SEL);
typedef id (*DISPATCH_IMP_NOARGS)(id, SEL);
@interface Foo : NSObject
@end
@rustle
rustle / gist:6406932
Created September 1, 2013 19:58
Example of the kind of thorough checks you can do in asserts that would be too expensive to leave in production code
#if !defined(NS_BLOCK_ASSERTIONS)
#define RSTLSetOfStringsAssert(set) [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { NSParameterAssert([obj isKindOfClass:[NSString class]]); }];
#else
#define RSTLSetOfStringsAssert(set)
#endif
#pragma mark - Memory Cache Subscripting
// Allow self[key] for looking up and writing to memory cache
// This is mostly a novelty
- (id)objectForKeyedSubscript:(id)key
{
return [self.memoryCache objectForKey:key];
}