Skip to content

Instantly share code, notes, and snippets.

View T1T4N's full-sized avatar
👽

Robert Armenski T1T4N

👽
View GitHub Profile
@T1T4N
T1T4N / UnsafeSendableBox.swift
Created August 13, 2024 08:10
A generic box type for adding unchecked Sendable conformance to a type
@dynamicMemberLookup
final class UnsafeSendableBox<T>: @unchecked Sendable {
let value: T
init(_ value: T) {
self.value = value
}
subscript<Value>(dynamicMember keyPath: KeyPath<T, Value>) -> Value {
value[keyPath: keyPath]
}
@T1T4N
T1T4N / DispatchMemoryPressure6.swift
Created August 12, 2024 10:19
Swift 6 concurrency-friendly way of monitoring memory pressure
@discardableResult
func logMemoryPressure() -> DispatchSourceProtocol {
// Wrapper class to make DispatchSourceMemoryPressure sendable
final class SendableDispatchMemoryPressureSource: @unchecked Sendable {
let source: any DispatchSourceMemoryPressure
init(_ source: any DispatchSourceMemoryPressure) {
self.source = source
}
func activate() {
@T1T4N
T1T4N / DispatchMemoryPressure.swift
Last active August 12, 2024 10:18
Preconcurrency (Swift 6) way of monitoring memory pressure in Swift
@discardableResult
@preconcurrency func logMemoryPressure() -> DispatchSourceProtocol {
let source = DispatchSource.makeMemoryPressureSource(eventMask: .all, queue: nil)
let queue = DispatchQueue(
label: "com.example.memoryPressure")
queue.async {
source.setEventHandler {
guard !source.isCancelled else { return }
let event = source.data
@T1T4N
T1T4N / UInt+Bits.swift
Last active August 9, 2024 09:52
Add support for manipulating specific bits within an integer by treating it like an array of Bool values
// Source: https://www.douggregor.net/posts/swift-for-cxx-practitioners-extensions/
extension UInt32 {
subscript(index: Int) -> Bool {
get {
(self & UInt32(1) << index) != 0
}
set {
let mask = UInt32(1) << index
self = (self & ~mask) | (newValue ? mask : 0)
@T1T4N
T1T4N / gist:77ccc29445d1bf8e8da87509ddfe9af4
Created July 16, 2024 10:23
Open Microsoft Edge on macOS with a specified Workspace
# Source: https://www.reddit.com/r/MicrosoftEdge/comments/16u0nuf/i_know_how_to_launch_edge_into_a_specific_profile/
cat "$HOME/Library/Application Support/Microsoft Edge/Default/Workspaces/WorkspacesCache"
open -a '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge' --args --profile-directory="ProfileName" --launch-workspace=<workspace-id>
@T1T4N
T1T4N / vscode-local-workspace.json
Created June 22, 2024 21:45
An example VSCode configuration for color-coding workspace windows
{
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#dc0000",
"activityBar.activeBorder": "#03A9F4",
"activityBar.background": "#860009",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#03A9F4",
"activityBarBadge.foreground": "#e7e7e7",
"statusBar.background": "#03A9F4",
@T1T4N
T1T4N / Publisher+WeakAssign.swift
Created June 22, 2024 10:14
Combine extensions for weak assignment and sink
extension Publisher where Failure == Never {
func weakAssign<T: AnyObject>(
to keyPath: ReferenceWritableKeyPath<T, Output>, on object: T
) -> AnyCancellable {
sink { [weak object] value in
object?[keyPath: keyPath] = value
}
}
func weakSink<T: AnyObject>(
@T1T4N
T1T4N / Publisher+Async.swift
Last active June 21, 2024 12:17
A Swift Extension for converting a Combine Publisher to async/await
extension Publisher {
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func asyncFirst() async throws -> Output? {
return try await self
.first()
.values
.first { _ in true }
}
// https://medium.com/geekculture/from-combine-to-async-await-c08bf1d15b77
@T1T4N
T1T4N / Memoize.swift
Created June 21, 2024 07:48
Swift memoization helpers
// Source: https://www.hackingwithswift.com/plus/high-performance-apps/using-memoization-to-speed-up-slow-functions
// Source: https://medium.com/@mvxlr/swift-memoize-walk-through-c5224a558194
typealias MemFn<Input, Output> = (Input) -> Output
// work with any sort of input and output as long as the input is hashable
// accept a function that takes Input and returns Output, and return a function with the same signature
func memoize<Input: Hashable, Output>(
_ work: @escaping MemFn<Input, Output>
) -> MemFn<Input, Output> {
@T1T4N
T1T4N / gist:f66a2ebbeb17b73d6643a178ede14639
Created June 18, 2024 14:54
Disable Xcode 16's Predictive Code Completion LLM Model
defaults write com.apple.dt.Xcode IDEModelAccessHasUserConsentForOnDeviceInteractions -bool FALSE