Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

Replace bare inits on Future and Task with .never #247

Merged
merged 1 commit into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Sources/Deferred/ExistentialFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Deferred
//
// Created by Zachary Waldowski on 8/29/15.
// Copyright © 2014-2016 Big Nerd Ranch. Licensed under MIT.
// Copyright © 2014-2018 Big Nerd Ranch. Licensed under MIT.
//

import Dispatch
Expand Down Expand Up @@ -134,11 +134,15 @@ public struct Future<Value>: FutureProtocol {
self.box = Always(value: value)
}

/// Create a future that will never get fulfilled.
public init() {
private init(never: ()) {
self.box = Never()
}

/// Create a future that will never get fulfilled.
public static var never: Future<Value> {
return Future(never: ())
}

/// Create a future having the same underlying future as `other`.
public init(_ other: Future<Value>) {
self.box = other.box
Expand Down
15 changes: 9 additions & 6 deletions Sources/Task/ExistentialTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public final class Task<SuccessValue>: NSObject {
self.progress = .taskRoot(for: progress)
}

/// Create a task that will never complete.
public override init() {
self.future = Future()
private init(never: ()) {
self.future = .never
self.progress = .indefinite()
}

Expand Down Expand Up @@ -81,12 +80,16 @@ public final class Task<SuccessValue>: NSObject {
self.cancellation = cancellation ?? {}
}

/// Create a task that will never complete.
public override init() {
self.future = Future()
private init(never: ()) {
self.future = .never
self.cancellation = {}
}
#endif

/// Create a task that will never complete.
public static var never: Task<SuccessValue> {
return Task(never: ())
}
}

extension Task: FutureProtocol {
Expand Down
6 changes: 3 additions & 3 deletions Tests/DeferredTests/ExistentialFutureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ExistentialFutureTests: XCTestCase {
}

func testPeekWhenUnfilled() {
anyFuture = Future<Int>()
anyFuture = Future<Int>.never
let peek = anyFuture.peek()
XCTAssertNil(peek)
}
Expand Down Expand Up @@ -103,7 +103,7 @@ class ExistentialFutureTests: XCTestCase {
}

func testDebugDescriptionUnfilled() {
let future = Future<Int>()
let future = Future<Int>.never
XCTAssertEqual("\(future)", "Future(not filled)")
}

Expand All @@ -118,7 +118,7 @@ class ExistentialFutureTests: XCTestCase {
}

func testReflectionUnfilled() {
let future = Future<Int>()
let future = Future<Int>.never

let magicMirror = Mirror(reflecting: future)
XCTAssertEqual(magicMirror.displayStyle, .optional)
Expand Down
4 changes: 2 additions & 2 deletions Tests/TaskTests/TaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class TaskTests: CustomExecutorTestCase {
func testThatAndThenForwardsCancellationToSubsequentTask() {
let expect = expectation(description: "flatMapped task is cancelled")
let task = makeAnyFinishedTask().andThen(upon: executor) { _ -> Task<String> in
Task(future: Future()) { expect.fulfill() }
Task(future: .never) { expect.fulfill() }
}

task.cancel()
Expand Down Expand Up @@ -227,7 +227,7 @@ class TaskTests: CustomExecutorTestCase {
}

func testThatTaskCreatedUnfilledIsIndeterminate() {
let task = Task<Int>()
let task = Task<Int>.never

XCTAssert(task.progress.isIndeterminate)
}
Expand Down