Skip to content

Commit bd20449

Browse files
committed
feat: Use variadic generics
1 parent fe8eb86 commit bd20449

File tree

12 files changed

+855
-604
lines changed

12 files changed

+855
-604
lines changed

.github/workflows/Test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test_macos:
11+
if: |
12+
!contains(github.event.head_commit.message, '[ci skip]') &&
13+
!contains(github.event.head_commit.message, '[ci skip test]') &&
14+
!contains(github.event.head_commit.message, '[ci skip test_macos]')
15+
runs-on: macOS-13
16+
timeout-minutes: 30
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Select Xcode 15.0.0
20+
run: sudo xcode-select -s /Applications/Xcode_15.0.app
21+
- name: Run tests
22+
run: make test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
/Packages
44
/*.xcodeproj
55
xcuserdata/
6+
DerivedData
7+
Package.resolved

.spi.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: 1
2+
builder:
3+
configs:
4+
- documentation_targets: [Capture]
5+
swift_version: 5.9

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 CaptureContext
3+
Copyright (c) 2023 CaptureContext
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test:
2+
@swift test
3+
4+
preview_docs:
5+
@swift package --disable-sandbox preview-documentation --product Capture

Package.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.9
22

33
import PackageDescription
44

@@ -10,6 +10,12 @@ let package = Package(
1010
targets: ["Capture"]
1111
)
1212
],
13+
dependencies: [
14+
.package(
15+
url: "https://github.com/apple/swift-docc-plugin",
16+
from: "1.3.0"
17+
),
18+
],
1319
targets: [
1420
.target(name: "Capture"),
1521
.testTarget(

README.md

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# swift-capture
22

3-
[![SwiftPM 5.3](https://img.shields.io/badge/📦_swiftpm-5.3-ED523F.svg?style=flat)](https://swift.org/download/) [![@maximkrouk](https://img.shields.io/badge/contact-@capturecontext-1DA1F2.svg?style=flat&logo=twitter)](https://twitter.com/capture_context)
3+
[![Test](https://github.com/capturecontext/swift-capture/actions/workflows/Test.yml/badge.svg) [![SwiftPM 5.9](https://img.shields.io/badge/📦_swiftpm-5.9-ED523F.svg?style=flat)](https://github.com/CaptureContext/swift-declarative-configuration/actions/workflows/Test.yml) ![Platforms](https://img.shields.io/badge/platforms-iOS_|_macOS_|_tvOS_|_watchOS_|_Catalyst-ED523F.svg?style=flat) [![@capture_context](https://img.shields.io/badge/contact-@capture__context-1DA1F2.svg?style=flat&logo=twitter)](https://twitter.com/capture_context)
44

5-
A mechanism for safe capturing & weakifying objects in Swift.
5+
A mechanism for ergonomic and safe capturing & weakifying objects in Swift.
66

77
## Usage Examples
88

@@ -19,7 +19,7 @@ With Capture
1919
Default
2020
```swift
2121
{ [weak self] in
22-
guard let self = self else { return }
22+
guard let self else { return }
2323
/// ...
2424
}
2525
```
@@ -35,7 +35,7 @@ capture { _self in
3535
Multiple parameters
3636
```swift
3737
{ [weak self] a, b, c in
38-
guard let self = self else { return }
38+
guard let self else { return }
3939
/// ...
4040
}
4141
```
@@ -58,7 +58,7 @@ Methods
5858
```
5959

6060
```swift
61-
capture(Self.someMethod)
61+
capture(in: <#Type#>.someMethod)
6262
```
6363

6464
----
@@ -73,29 +73,14 @@ let object.dataSource = { [weak self] in
7373
```
7474

7575
```swift
76-
let object.dataSource = capture(or: [], in: \.data)
77-
```
78-
79-
----
80-
81-
Weak assign
82-
83-
```swift
84-
{ [weak self] value in
85-
self?.value = value
86-
}
87-
```
88-
89-
```swift
90-
captureAssign(to: \.value)
91-
captureAssign(to: \.value, removeDuplicates: ==)
76+
let object.dataSource = capture(orReturn: [], in: \.data)
9277
```
9378

9479
## Installation
9580

9681
### Basic
9782

98-
You can add `weak` to an Xcode project by adding it as a package dependency.
83+
You can add `swift-capture` to an Xcode project by adding it as a package dependency.
9984

10085
1. From the **File** menu, select **Swift Packages › Add Package Dependency…**
10186
2. Enter [`"https://github.com/capturecontext/swift-capture"`](https://github.com/capturecontext/swift-capture) into the package repository URL text field
@@ -107,17 +92,15 @@ If you use SwiftPM for your project, you can add `weak` to your package file. Al
10792

10893
```swift
10994
.package(
110-
name: "weak",
11195
url: "[email protected]:capturecontext/swift-capture.git",
112-
.upToNextMajor("2.0.0")
96+
.upToNextMajor("3.0.0")
11397
)
11498
```
11599

116100
Do not forget about target dependencies:
117101

118102
```swift
119103
.product(
120-
name: "swift-capture",
121104
name: "Capture",
122105
package: "swift-capture"
123106
)

Sources/Capture/Capture.swift

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import Foundation
2+
3+
// MARK: - Void result closures
4+
5+
/// Weakly captures an object in non-parametrized void result closure.
6+
@inlinable
7+
public func capture<Object: AnyObject>(
8+
_ object: Object,
9+
in closure: @escaping (Object) -> Void
10+
) -> () -> Void {
11+
return Weak(object).capture(in: closure)
12+
}
13+
14+
/// Weakly captures an object in non-parametrized lazy void result closure.
15+
@inlinable
16+
public func capture<Object: AnyObject>(
17+
_ object: Object,
18+
in closure: @escaping (Object) -> () -> Void
19+
) -> () -> Void {
20+
Weak(object).capture(in: closure)
21+
}
22+
23+
/// Weakly captures an object in parametrized void result closure.
24+
public func capture<Object: AnyObject, each Arg>(
25+
_ object: Object,
26+
in closure: @escaping (Object, repeat each Arg) -> Void
27+
) -> (repeat each Arg) -> Void {
28+
Weak(object).capture(in: closure)
29+
}
30+
31+
// MARK: - Non-void result closures
32+
33+
/// Weakly captures an object in non-parametrized non-void result closure.
34+
@inlinable
35+
public func capture<Object: AnyObject, Output>(
36+
_ object: Object,
37+
orReturn defaultValue: @escaping @autoclosure () -> Output,
38+
in closure: @escaping (Object) -> Output
39+
) -> () -> Output {
40+
Weak(object).capture(orReturn: defaultValue(), in: closure)
41+
}
42+
43+
/// Weakly captures an object in non-parametrized lazy non-void result closure.
44+
@inlinable
45+
public func capture<Object: AnyObject, Output>(
46+
_ object: Object,
47+
orReturn defaultValue: @escaping @autoclosure () -> Output,
48+
in closure: @escaping (Object) -> () -> Output
49+
) -> () -> Output {
50+
Weak(object).capture(orReturn: defaultValue(), in: closure)
51+
}
52+
53+
/// Weakly captures an object in parametrized non-void result closure.
54+
public func capture<Object: AnyObject, each Arg, Output>(
55+
_ object: Object,
56+
orReturn defaultValue: @escaping @autoclosure () -> Output,
57+
in closure: @escaping (Object, repeat each Arg) -> Output
58+
) -> (repeat each Arg) -> Output {
59+
Weak(object).capture(orReturn: defaultValue(), in: closure)
60+
}
61+
62+
// MARK: - Non-void optional result closures
63+
64+
/// Weakly captures an object in non-parametrized non-void optional result closure.
65+
@inlinable
66+
public func capture<Object: AnyObject, Output>(
67+
_ object: Object,
68+
in closure: @escaping (Object) -> Output?
69+
) -> () -> Output? {
70+
Weak(object).capture(in: closure)
71+
}
72+
73+
/// Weakly captures an object in non-parametrized lazy non-void optional result closure.
74+
@inlinable
75+
public func capture<Object: AnyObject, Output>(
76+
_ object: Object,
77+
in closure: @escaping (Object) -> () -> Output?
78+
) -> () -> Output? {
79+
Weak(object).capture(in: closure)
80+
}
81+
82+
/// Weakly captures an object in parametrized non-void optional result closure.
83+
public func capture<Object: AnyObject, each Arg, Output>(
84+
_ object: Object,
85+
in closure: @escaping (Object, repeat each Arg) -> Output?
86+
) -> (repeat each Arg) -> Output? {
87+
Weak(object).capture(in: closure)
88+
}

0 commit comments

Comments
 (0)