Skip to content

Instantly share code, notes, and snippets.

View sanekgusev's full-sized avatar

Aleksandr Gusev sanekgusev

View GitHub Profile
@sanekgusev
sanekgusev / TargetActionClosureAdapter.swift
Created September 2, 2016 10:17
TargetActionClosureAdapter
import UIKit
import ObjectiveC.runtime
private class TargetActionClosureAdapter<T : AnyObject> {
typealias Closure = (sender: T!) -> Void
private let closure: Closure
private init() {
@sanekgusev
sanekgusev / arm64_super_forwarding_trampoline.asm
Last active September 8, 2016 21:25
arm64 super-forwarding IMP
.section __TEXT,__cstring,cstring_literals
L_.super_ivar_name:
.ascii "_super"
.byte 0
.section __TEXT,__text,regular,pure_instructions
.globl _SGVObjcMsgSendSuperTrampolineNew
.align 2
; This function will be used as an IMP for a runtime-added method in the super-forwarding proxy.
; The goal is to keep all arguments intact and only modify the first argument (in x0)
; to point to an 'objc_super' struct instead of proxy instance, and then simply tail-call to objc_msgSendSuper
@sanekgusev
sanekgusev / EnsureGlobalQueuesDrained.swift
Last active September 16, 2016 13:38
Ensure that global GCD queues are drained (useful for avoiding undesired side-effects in unit tests)
// this method ensures that blocks enqueued onto global GCD queues by the previous test method
// have finished executing and can have no side effects on the following test methods
// NOTE: This does not affect any private queues: any queued, but unfinished blocks may still be running off those
// unless properly cancelled by the code managing those queues
func ensureGlobalQueuesDrained() {
// all of the below only makes sense if we're running on the main thread
assert(NSThread.isMainThread())
// construct an array of all concurrent global queues
@sanekgusev
sanekgusev / Robots.swift
Created January 6, 2017 03:52
Robotized API
// MARK: Interface
public protocol ConnectionState: RawRepresentable {
var rawValue: String { get }
init?(rawValue: String)
}
public struct SensorReading<U: Unit> {
let name: String
let measurement: Measurement<U>
@sanekgusev
sanekgusev / NSLayoutAnchor+MultiplierConstraints.swift
Created September 25, 2017 14:29
NSLayoutAnchor+MultiplierConstraints
import UIKit
extension NSLayoutXAxisAnchor {
func constraint(equalTo anchor: NSLayoutXAxisAnchor,
multiplier m: CGFloat,
constant c: CGFloat = 0.0) -> NSLayoutConstraint {
let constraint = self.constraint(equalTo: anchor, constant: c)
return constraint.constraint(multiplier: m)
}
@sanekgusev
sanekgusev / keybase.md
Created October 25, 2017 02:43
keybase.md

Keybase proof

I hereby claim:

  • I am sanekgusev on github.
  • I am sanekgusev (https://keybase.io/sanekgusev) on keybase.
  • I have a public key ASA6pUUO04wDkVzDocxv9k0dlBlgt-CKiFaQ3DD_-GbImwo

To claim this, I am signing this object:

@sanekgusev
sanekgusev / runloops.swift
Created December 5, 2017 14:55
Nested Runloops
import Foundation
func asynchronousFunction(with completion: @escaping () -> Void) {
DispatchQueue.global().async {
Thread.sleep(forTimeInterval: 1)
DispatchQueue.main.async {
print("running callback on main thread")
completion()
}
}