Skip to content

Essential tools that extend the capabilities of Swift Standard Library

License

Notifications You must be signed in to change notification settings

NSFatalError/Principle

Repository files navigation

Principle

Swift Codecov

Essential tools that extend the capabilities of Swift Standard Library.

Contents

PrincipleConcurrency

PrincipleConcurrency introduces SingleUseTransfer - an important utility that allows to safely capture sending values in closures where the compiler would otherwise prohibit it.

Since Swift currently lacks a built-in annotation to indicate that a closure is guaranteed to be invoked at most once, the compiler may reject code that programmers can prove to be safe. SingleUseTransfer shifts the responsibility of ensuring single invocation to the developer while preserving all the benefits of strict concurrency checking — without resorting to tempting workarounds like @unchecked or nonisolated(unsafe):

let mutex = Mutex(NonSendable())
let instance = NonSendable()
var transfer = SingleUseTransfer(instance)

mutex.withLock { protected in
    protected = transfer.finalize()
}

Additionally, PrincipleConcurrency contains multiple tools to impose time-based constraints for async operations:

Task {
    do {
        try await withTimeout(.seconds(1)) {
            try await crunchNumbers()
        }
    } catch is TimeoutError {
        print("Operation took too long to finish and got cancelled.")
    }
}

PrincipleCollections

PrincipleCollections contains extensions to expressively sort collections of types which can't naturally conform to Comparable protocol:

struct Person: Equatable {
    let age: Int
    let name: String
}

var people: [Person] = [...]
people.sort(on: \.age)

Installation

.package(
    url: "https://github.com/NSFatalError/Principle",
    from: "1.0.0"
)