Skip to content

Commit

Permalink
feat: Use variadic generics
Browse files Browse the repository at this point in the history
  • Loading branch information
maximkrouk committed Oct 29, 2023
1 parent fe8eb86 commit bd20449
Show file tree
Hide file tree
Showing 12 changed files with 855 additions and 604 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/Test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test_macos:
if: |
!contains(github.event.head_commit.message, '[ci skip]') &&
!contains(github.event.head_commit.message, '[ci skip test]') &&
!contains(github.event.head_commit.message, '[ci skip test_macos]')
runs-on: macOS-13
timeout-minutes: 30
steps:
- uses: actions/checkout@v3
- name: Select Xcode 15.0.0
run: sudo xcode-select -s /Applications/Xcode_15.0.app
- name: Run tests
run: make test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/Packages
/*.xcodeproj
xcuserdata/
DerivedData
Package.resolved
5 changes: 5 additions & 0 deletions .spi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 1
builder:
configs:
- documentation_targets: [Capture]
swift_version: 5.9
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 CaptureContext
Copyright (c) 2023 CaptureContext

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
@swift test

preview_docs:
@swift package --disable-sandbox preview-documentation --product Capture
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.9

import PackageDescription

Expand All @@ -10,6 +10,12 @@ let package = Package(
targets: ["Capture"]
)
],
dependencies: [
.package(
url: "https://github.com/apple/swift-docc-plugin",
from: "1.3.0"
),
],
targets: [
.target(name: "Capture"),
.testTarget(
Expand Down
33 changes: 8 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# swift-capture

[![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)
[![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)

A mechanism for safe capturing & weakifying objects in Swift.
A mechanism for ergonomic and safe capturing & weakifying objects in Swift.

## Usage Examples

Expand All @@ -19,7 +19,7 @@ With Capture
Default
```swift
{ [weak self] in
guard let self = self else { return }
guard let self else { return }
/// ...
}
```
Expand All @@ -35,7 +35,7 @@ capture { _self in
Multiple parameters
```swift
{ [weak self] a, b, c in
guard let self = self else { return }
guard let self else { return }
/// ...
}
```
Expand All @@ -58,7 +58,7 @@ Methods
```

```swift
capture(Self.someMethod)
capture(in: <#Type#>.someMethod)
```

----
Expand All @@ -73,29 +73,14 @@ let object.dataSource = { [weak self] in
```

```swift
let object.dataSource = capture(or: [], in: \.data)
```

----

Weak assign

```swift
{ [weak self] value in
self?.value = value
}
```

```swift
captureAssign(to: \.value)
captureAssign(to: \.value, removeDuplicates: ==)
let object.dataSource = capture(orReturn: [], in: \.data)
```

## Installation

### Basic

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

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

```swift
.package(
name: "weak",
url: "[email protected]:capturecontext/swift-capture.git",
.upToNextMajor("2.0.0")
.upToNextMajor("3.0.0")
)
```

Do not forget about target dependencies:

```swift
.product(
name: "swift-capture",
name: "Capture",
package: "swift-capture"
)
Expand Down
88 changes: 88 additions & 0 deletions Sources/Capture/Capture.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Foundation

// MARK: - Void result closures

/// Weakly captures an object in non-parametrized void result closure.
@inlinable
public func capture<Object: AnyObject>(
_ object: Object,
in closure: @escaping (Object) -> Void
) -> () -> Void {
return Weak(object).capture(in: closure)
}

/// Weakly captures an object in non-parametrized lazy void result closure.
@inlinable
public func capture<Object: AnyObject>(
_ object: Object,
in closure: @escaping (Object) -> () -> Void
) -> () -> Void {
Weak(object).capture(in: closure)
}

/// Weakly captures an object in parametrized void result closure.
public func capture<Object: AnyObject, each Arg>(
_ object: Object,
in closure: @escaping (Object, repeat each Arg) -> Void
) -> (repeat each Arg) -> Void {
Weak(object).capture(in: closure)
}

// MARK: - Non-void result closures

/// Weakly captures an object in non-parametrized non-void result closure.
@inlinable
public func capture<Object: AnyObject, Output>(
_ object: Object,
orReturn defaultValue: @escaping @autoclosure () -> Output,
in closure: @escaping (Object) -> Output
) -> () -> Output {
Weak(object).capture(orReturn: defaultValue(), in: closure)
}

/// Weakly captures an object in non-parametrized lazy non-void result closure.
@inlinable
public func capture<Object: AnyObject, Output>(
_ object: Object,
orReturn defaultValue: @escaping @autoclosure () -> Output,
in closure: @escaping (Object) -> () -> Output
) -> () -> Output {
Weak(object).capture(orReturn: defaultValue(), in: closure)
}

/// Weakly captures an object in parametrized non-void result closure.
public func capture<Object: AnyObject, each Arg, Output>(
_ object: Object,
orReturn defaultValue: @escaping @autoclosure () -> Output,
in closure: @escaping (Object, repeat each Arg) -> Output
) -> (repeat each Arg) -> Output {
Weak(object).capture(orReturn: defaultValue(), in: closure)
}

// MARK: - Non-void optional result closures

/// Weakly captures an object in non-parametrized non-void optional result closure.
@inlinable
public func capture<Object: AnyObject, Output>(
_ object: Object,
in closure: @escaping (Object) -> Output?
) -> () -> Output? {
Weak(object).capture(in: closure)
}

/// Weakly captures an object in non-parametrized lazy non-void optional result closure.
@inlinable
public func capture<Object: AnyObject, Output>(
_ object: Object,
in closure: @escaping (Object) -> () -> Output?
) -> () -> Output? {
Weak(object).capture(in: closure)
}

/// Weakly captures an object in parametrized non-void optional result closure.
public func capture<Object: AnyObject, each Arg, Output>(
_ object: Object,
in closure: @escaping (Object, repeat each Arg) -> Output?
) -> (repeat each Arg) -> Output? {
Weak(object).capture(in: closure)
}
Loading

0 comments on commit bd20449

Please sign in to comment.