Skip to content

Commit

Permalink
v4 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Nov 30, 2023
1 parent 68191d8 commit dd061dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
36 changes: 22 additions & 14 deletions Sources/Wrap/Wrap.swift
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@

postfix operator &>

public postfix func &> <T>(argument: T) -> Wrap<T> {
.init(argument)
public postfix func &> <T>(argument: consuming T) -> Wrap<T> {
.init(consume argument)
}

public struct Wrap<Value> {
public struct Wrap<Value>: ~Copyable {

public let value: Value

public init(_ value: Value) {
self.value = value
public init(_ value: consuming Value) {
self.value = consume value
}

}

public func modify<Value>(_ value: inout Value, _ modifier: (inout Value) -> Void) {
modifier(&value)
public func modify<Value>(_ value: inout Value, _ modifier: (inout Value) throws -> Void) rethrows {
try modifier(&value)
}

extension Wrap {

public func map<U>(_ transform: (Value) throws -> U) rethrows -> U {
public borrowing func map<U>(_ transform: (consuming Value) throws -> U) rethrows -> U {
try transform(value)
}

@discardableResult
public func `do`(_ applier: (Value) throws -> Void) rethrows -> Value where Value : AnyObject {
public borrowing func `do`(_ applier: (borrowing Value) throws -> Void) rethrows -> Value {
try applier(value)
return value
}

public func modify(_ modifier: (inout Value) throws -> Void) rethrows -> Value {

@discardableResult
@_disfavoredOverload
public borrowing func `do`(_ applier: (borrowing Value) throws -> Void) rethrows -> Value? {
try applier(value)
return value
}

public borrowing func modify(_ modifier: (inout Value) throws -> Void) rethrows -> Value {
var v = value
try modifier(&v)
return v
}

public func filter(_ filter: (Value) -> Bool) -> Value? {
public borrowing func filter(_ filter: (borrowing Value) -> Bool) -> Value? {
guard filter(value) else {
return nil
}
return value
}

}

23 changes: 0 additions & 23 deletions WrapLib.podspec

This file was deleted.

0 comments on commit dd061dc

Please sign in to comment.