Swiftpack.co - Package - GalenRhodes/Rubicon


Rubicon is a set of classes, structures, enums, extensions, tools, and utilities to make Swift development easier, cleaner, and less verbose.

API Documentation

Documentation of the API can be found here: Rubion API

Swifty Stuff

Here's just a few of the places where I've taken advantage of Swift's features.

NSRecursiveLock -> RecursiveLock

Even though they've included a version of NSRecursiveLock in the open source version of Swift I still get nervous that one day all the NS* classes will suddenly disappear. So I created a wrapper around NSRecursiveLock called simply "RecursiveLock". That way if I suddenly have to recreate it at least I won't have to rename it.

Also as part of RecursiveLock I've create a method called:

func withLock<T>(_ lambda: () throws -> T) rethrows -> T

So that rather than the standard design pattern of:

do {
    defer { lock.unlock() }
    /* Do something here. */

We can now just do this:

lock.withLock { /* Do something here. */ }

It will even allow returned values and throws.

let val = try lock.withLock { try iReturnAValueOrThrowAnError() }

NSCondition -> Conditional

For the same reasons as above I created a wrapper around NSCondition called simply "Conditional".

Also, along with the "withLock(_:)" method above, I've also included a new method called:

public func withLockWait<T>(cond: () -> Bool, _ lambda: () throws -> T) rethrows -> T

This method takes two enclosures. One to test the condition and the other to execute once the condition is met.

So instead of saying:

do {
    defer {
    while !someCondition() {
    /* Do something here! */

You can now simply say this:

lock.withLockWait(cond: { someCondition() }) { /* Do something here! */ }

So much more clear!

Also, the version of "withLock(_:)" in Conditional calls "signal()" right before it calls "unlock()".


Stars: 0


Used By

Total: 0


v0.1.9-alpha - 2020-09-14 14:37:17

v0.1.4-alpha - 2020-09-10 12:36:33

v0.1.3-alpha - 2020-09-10 12:22:00

First SPM Release - 2020-09-09 20:28:31