Skip to content

Instantly share code, notes, and snippets.

@therealbnut
therealbnut / cargo-mktmp-target
Created February 28, 2024 05:33
Make a temporary directory for cargo build products
#!/usr/bin/env bash
set -euxo pipefail
main() {
local tmpdir
tmpdir="$(mktemp -d /tmp/cargo-targets/target.XXXXXXXX)"
ln -s "${tmpdir}" target
}
func keys_equal<T, U:Equatable>(_ keypath: Keypath<T,U>) -> (T,T) -> Bool {
return { $0[keypath: keypath] == $1[keypath: keypath] }
}
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher {
    func removeDuplicates(matchingAll predicates: @escaping (Output, Output) -> Bool...)
        -> Publishers.RemoveDuplicates<Self>
    {
        return self.removeDuplicates { lhs, rhs in
public struct BouyantPointNumber: Hashable {
public typealias Storage = Double
private var storage: Storage
@_transparent
private static var scaleFactor: Storage {
return 10.0
}
@_transparent
private var scaleFactor: Storage {
struct S {
let x: Int
init(x: Int) { self.x = x }
// This is allowed
init(x2: Int) {
self.init(x: x2)
}
private class Example: NSObject {
@objc func example() {}
}
extension XCTestSuite {
public func addTests<Test: XCTestCase>(
_ tests: [(String, (Test) -> () -> Void)])
{
let method = class_getInstanceMethod(Example.self,
#!/bin/bash
set -eu
# You can bootstrap by running:
# wget --no-cache -N tinyurl.com/install-swift-linux && chmod +x ./install-swift-linux && ./install-swift-linux
# https://github.com/apple/swift/blob/master/README.md#getting-started
source /etc/os-release
@therealbnut
therealbnut / volumeUUIDForPath.swift
Created April 24, 2017 02:02 — forked from adgray/volumeUUIDForPath.swift
Get Volume UUID for Path
import DiskArbitration
extension URL {
var volumeUUID: String? {
guard self.isFileURL, let session = DASessionCreate(nil) else {
return nil
}
var fsStats = statfs()
guard statfs(self.path, &fsStats) == 0,
// Using XCode 7.3.1 (it's all I had at the time)
extension MutableCollectionType where Generator.Element == SubSequence.Generator.Element {
mutating func selectionSort(isOrderedBefore compare: (Generator.Element, Generator.Element) throws -> Bool) rethrows {
for currentIndex in self.startIndex ..< self.endIndex {
// (index,element) pairs after the current index
let pairs = zip(currentIndex ..< self.endIndex, self.suffixFrom(currentIndex)).lazy
// the pair with the minimum element
let pair = try pairs.minElement({ try compare($0.1, $1.1) })
// Using XCode 7.3.1 (it's all I had at the time)
extension MutableCollectionType where Generator.Element: Comparable {
mutating func selectionSortInPlace() {
for currentIndex in self.startIndex ..< self.endIndex {
guard let (minIndex, minElement) = zip((currentIndex ..< self.endIndex), self.suffixFrom(currentIndex))
.minElement({ lhs, rhs in (lhs.1 as! Generator.Element) < (rhs.1 as! Generator.Element) })
else {
continue
}
// Output:
// AnyPokemon<🔥>(Ponyta())
// AnyPokemon<🔥>(Charmander())
protocol Pokemon {
associatedtype Power
mutating func attack() -> Power
}
struct Pikachu: Pokemon {