From 4fffbe81cd881e7999041425f2a435d82336d733 Mon Sep 17 00:00:00 2001 From: Brian Floersch Date: Mon, 15 Apr 2024 22:24:24 -0400 Subject: [PATCH] Rename select functions to be more swifty (#6) * swifty names * readme * readme --- Benchmarks/Sources/Benchmarks/main.swift | 12 ++-- GolangVsSwift.md | 14 ++--- README.md | 16 ++--- Sources/AsyncChannels/Select.swift | 24 ++++---- .../AsyncChannelTests.swift | 58 +++++++++---------- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/Benchmarks/Sources/Benchmarks/main.swift b/Benchmarks/Sources/Benchmarks/main.swift index af8ae60..7fd4bb9 100644 --- a/Benchmarks/Sources/Benchmarks/main.swift +++ b/Benchmarks/Sources/Benchmarks/main.swift @@ -242,12 +242,12 @@ func testMultiSelect(_ type: T.Type) async -> (String, String, while sum < 6 * 100_000 { await select { - rx(a) { sum += 1 } - rx(b) { sum += 1 } - rx(c) { sum += 1 } - rx(d) { sum += 1 } - rx(e) { sum += 1 } - rx(f) { sum += 1 } + receive(a) { sum += 1 } + receive(b) { sum += 1 } + receive(c) { sum += 1 } + receive(d) { sum += 1 } + receive(e) { sum += 1 } + receive(f) { sum += 1 } } } } diff --git a/GolangVsSwift.md b/GolangVsSwift.md index 55f1a4b..d9c280e 100644 --- a/GolangVsSwift.md +++ b/GolangVsSwift.md @@ -117,7 +117,7 @@ Swift has reserve words for `case` and `default` and the operator support is not -`rx(c)` +`receive(c)` `case <-c:` @@ -127,7 +127,7 @@ Swift has reserve words for `case` and `default` and the operator support is not -`rx(c) { v in ... }` +`receive(c) { v in ... }` `case v := <-c: ...` @@ -137,7 +137,7 @@ Swift has reserve words for `case` and `default` and the operator support is not -`tx(c, "foo")` +`send("foo", to: c)` `case c <- "foo":` @@ -147,7 +147,7 @@ Swift has reserve words for `case` and `default` and the operator support is not -`tx(c, "foo") { ... }` +`send("foo", to: c) { ... }` `case c <- "foo": ...` @@ -198,7 +198,7 @@ let a = Channel(capacity: 1) await a <- "foo" await select { - rx(a) { av in + receive(a) { av in print(av!) } none { @@ -235,7 +235,7 @@ default: let a = Channel(capacity: 1) await select { - tx(a, "foo") + send("foo", to: a) none { print("Not called") } @@ -271,7 +271,7 @@ fmt.Println(<-a) let a = Channel() await select { - rx(a) + receive(a) none { print("Default case!") } diff --git a/README.md b/README.md index 371c094..f78479b 100644 --- a/README.md +++ b/README.md @@ -124,13 +124,13 @@ The loop will break when the channel is closed. ### Operations -`rx(c)` receive a value, but do nothing with it. +`receive(c)` receive a value, but do nothing with it. -`rx(c) { v in ... }` receive a value and do something with it. +`receive(c) { v in ... }` receive a value and do something with it. -`tx(c, "foo")` send a value and do nothing. +`send("foo", to: c)` send a value and do nothing. -`tx(c, "foo") { ... }` run some code if a send is successful. +`send("foo", to: c) { ... }` run some code if a send is successful. `none { ... }` if none of the channel operations were ready, none will execute instead. @@ -148,8 +148,8 @@ Task { // Will print foo or bar await select { - rx(d) { print($0!) } - rx(c) { print($0!) } + receive(d) { print($0!) } + receive(c) { print($0!) } } ``` @@ -165,9 +165,9 @@ for _ in (0..<10) { for _ in (0..<20) { await select { // receive from a and print it - rx(a) { print($0!) } + receive(a) { print($0!) } // send "b" to b - tx(b, "b") + send("b", to: b) // if both a and b suspend, print "NONE" none { print("NONE") diff --git a/Sources/AsyncChannels/Select.swift b/Sources/AsyncChannels/Select.swift index fd4f5c6..71f5182 100644 --- a/Sources/AsyncChannels/Select.swift +++ b/Sources/AsyncChannels/Select.swift @@ -12,7 +12,7 @@ protocol SelectProtocol { func handle(_ sm: SelectSignal) async -> Bool } -struct RxHandler: SelectProtocol { +struct ReceiveHandler: SelectProtocol { private var chan: Channel private let outFunc: (T?) async -> () @@ -48,7 +48,7 @@ struct NoneHandler: SelectProtocol { } } -struct TxHandler: SelectProtocol { +struct SendHandler: SelectProtocol { private var chan: Channel private let val: T private let onSend: () async -> () @@ -102,24 +102,24 @@ public func select(@SelectCollector cases: () -> ([SelectHandler])) async { } } -public func rx(_ chan: Channel, _ outFunc: @escaping (T?) async -> ()) -> SelectHandler { - return SelectHandler(inner: RxHandler(chan: chan, outFunc: outFunc)) +public func receive(_ chan: Channel, _ outFunc: @escaping (T?) async -> ()) -> SelectHandler { + return SelectHandler(inner: ReceiveHandler(chan: chan, outFunc: outFunc)) } -public func rx(_ chan: Channel, _ outFunc: @escaping () async -> ()) -> SelectHandler { - return SelectHandler(inner: RxHandler(chan: chan, outFunc: { _ in await outFunc() })) +public func receive(_ chan: Channel, _ outFunc: @escaping () async -> ()) -> SelectHandler { + return SelectHandler(inner: ReceiveHandler(chan: chan, outFunc: { _ in await outFunc() })) } -public func rx(_ chan: Channel) -> SelectHandler { - return SelectHandler(inner: RxHandler(chan: chan, outFunc: { _ in })) +public func receive(_ chan: Channel) -> SelectHandler { + return SelectHandler(inner: ReceiveHandler(chan: chan, outFunc: { _ in })) } -public func tx(_ chan: Channel, _ val: T) -> SelectHandler { - return SelectHandler(inner: TxHandler(chan: chan, val: val, onSend: {})) +public func send(_ val: T, to chan: Channel) -> SelectHandler { + return SelectHandler(inner: SendHandler(chan: chan, val: val, onSend: {})) } -public func tx(_ chan: Channel, _ val: T, _ onSend: @escaping () async -> ()) -> SelectHandler { - return SelectHandler(inner: TxHandler(chan: chan, val: val, onSend: onSend)) +public func send(_ val: T, to chan: Channel, _ onSend: @escaping () async -> ()) -> SelectHandler { + return SelectHandler(inner: SendHandler(chan: chan, val: val, onSend: onSend)) } public func none(handler: @escaping () async -> ()) -> SelectHandler { diff --git a/Tests/AsyncChannelsTests/AsyncChannelTests.swift b/Tests/AsyncChannelsTests/AsyncChannelTests.swift index b0bc1a0..16bf40f 100644 --- a/Tests/AsyncChannelsTests/AsyncChannelTests.swift +++ b/Tests/AsyncChannelsTests/AsyncChannelTests.swift @@ -17,8 +17,8 @@ final class AsyncTest: XCTestCase { let stop = Channel() Task { await select { - rx(stop) - rx(sleep(for: duration)) { + receive(stop) + receive(sleep(for: duration)) { XCTFail("Test timed out") exit(1) } @@ -186,13 +186,13 @@ final class AsyncTest: XCTestCase { } await select { - rx(d) { await result <- $0! } - rx(c) { await result <- $0! } + receive(d) { await result <- $0! } + receive(c) { await result <- $0! } } await select { - rx(d) { await result <- $0! } - rx(c) { await result <- $0! } + receive(d) { await result <- $0! } + receive(c) { await result <- $0! } } result.close() @@ -214,8 +214,8 @@ final class AsyncTest: XCTestCase { for _ in (0..<6) { await select { - rx(d) { await result <- $0! } - rx(c) { await result <- $0! } + receive(d) { await result <- $0! } + receive(c) { await result <- $0! } } } result.close() @@ -241,10 +241,10 @@ final class AsyncTest: XCTestCase { let drain: () async -> Void = { await select { - rx(d) { + receive(d) { XCTFail() } - rx(c) { + receive(c) { cCall += 1 } none { @@ -268,12 +268,12 @@ final class AsyncTest: XCTestCase { Task { await select { - rx(c) { await result <- true } + receive(c) { await result <- true } none { XCTFail() } } await select { - rx(c) { XCTFail() } + receive(c) { XCTFail() } none { await result <- true } } } @@ -304,9 +304,9 @@ final class AsyncTest: XCTestCase { var done = false while !done { await select { - rx(a) { await c <- $0! } - rx(b) { await c <- $0! } - rx(done1) { done = true } + receive(a) { await c <- $0! } + receive(b) { await c <- $0! } + receive(done1) { done = true } } } } @@ -315,7 +315,7 @@ final class AsyncTest: XCTestCase { var count = 0 while !done { await select { - rx(c) { + receive(c) { count += 1 if count >= 2 * total { done = true @@ -345,10 +345,10 @@ final class AsyncTest: XCTestCase { var done = false while !done { await select { - rx(a) { count += 1 } - rx(b) { count += 1 } - rx(c) { count += 1 } - rx(d) { count += 1 } + receive(a) { count += 1 } + receive(b) { count += 1 } + receive(c) { count += 1 } + receive(d) { count += 1 } none { done = true } @@ -358,7 +358,7 @@ final class AsyncTest: XCTestCase { XCTAssertEqual(40, count) } - func testTx() async { + func testSend() async { let a = Channel(capacity: 10) let b = Channel(capacity: 10) @@ -368,8 +368,8 @@ final class AsyncTest: XCTestCase { for _ in (0..<20) { await select { - rx(a) - tx(b, "b") + receive(a) + send("b", to: b) none { XCTFail() } @@ -380,7 +380,7 @@ final class AsyncTest: XCTestCase { var done = false while !done { await select { - rx(b) { + receive(b) { countB += 1 XCTAssertEqual($0, "b") } @@ -393,12 +393,12 @@ final class AsyncTest: XCTestCase { XCTAssertEqual(10, countB) } - func testTxHandler() async { + func testSendHandler() async { let a = Channel(capacity: 1) let testChan = Channel(capacity: 1) await select { - tx(a, "b") { + send("b", to: a) { await testChan <- true } none { @@ -414,7 +414,7 @@ final class AsyncTest: XCTestCase { Task { await select { - rx(a) { val in + receive(a) { val in XCTAssertNil(val) } } @@ -487,7 +487,7 @@ final class AsyncTest: XCTestCase { var done = false while !done { await select { - tx(c, true) + send(true, to: c) none { done = true } @@ -525,7 +525,7 @@ final class AsyncTest: XCTestCase { var sum = 0 while !done { await select { - rx(c) { + receive(c) { sum += 1 } none {