Swiftpack.co -  alexdrone/Utility as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
alexdrone/Utility
Assign/Partial/ReadOnly/Proxy/Locks in Swift
.package(url: "https://github.com/alexdrone/Utility.git", from: "0.0.1")

Utility Swift

Assign

This function is used to copy the values of all enumerable own properties from one or more source struct to a target struct. If the argument is a reference type the same refence is returned.

public func assign<T>(_ value: T, changes: (inout T) -> Void) -> T

Partial

Constructs a type with all properties of T set to optional. This utility will return a type that represents all subsets of a given type.

The wrapped type can be then constructed at referred time by calling the build() method. The instance can be later on changed with the merge(inout _:) method.

struct Todo { var title: String; var description: String } 
var partial = Partial { .success(Todo(
 title: $0.get(\Todo.title, default: "Untitled"),   
 description: $0.get(\Todo.description, default: "No description"))) 
} 
partial.title = "A Title" 
partial.description = "A Description" 
var todo = try! partial.build().get() 
partial.description = "Another Descrition" 
todo = partial.merge(&todo) 

ReadOnly

Constructs a type with all properties of T set to readonly, meaning the properties of the constructed type cannot be reassigned.

Note A read-only object can propagate change events if the wrapped type ia an ObservableObject by calling propagateObservableObject() at construction time.

struct Todo { var title: String; var description: String }
let todo = Todo(title: "A Title", description: "A Description")
let readOnlyTodo = ReadOnly(todo)
readOnlyTodo.title // "A title"

ObservableProxy

Creates an observable Proxy for the object passed as argument (with granularity at the property level).

struct Todo { var title: String; var description: String }
let todo = Todo(title: "A Title", description: "A Description")
let proxy = Proxy(todo)
proxy.propertyDidChange.sink {
  if $0.match(keyPath: \.title) {
    ...
  }
}
proxy.title = "New Title"

Concurrency

This package offer a variety of different lock implementations:

  • Mutex: enforces limits on access to a resource when there are many threads of execution.
  • UnfairLock: low-level lock that allows waiters to block efficiently on contention.
  • ReadersWriterLock: readers-writer lock provided by the platform implementation of the POSIX Threads standard.

Property wrappers to work with any of the locks above or any NSLocking compliant lock:

  • @LockAtomic<L: Locking>
  • @SyncDispatchQueueAtomic
  • @ReadersWriterAtomic

The package also includes LockfreeAtomic: fine-grained atomic operations allowing for Lockfree concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object.

GitHub

link
Stars: 31
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

Proxy 0.0.1
1 year ago

Initial release

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API