Skip to content

Commit

Permalink
Rename select functions to be more swifty (#6)
Browse files Browse the repository at this point in the history
* swifty names

* readme

* readme
  • Loading branch information
gh123man authored Apr 16, 2024
1 parent 4dd5591 commit 4fffbe8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
12 changes: 6 additions & 6 deletions Benchmarks/Sources/Benchmarks/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ func testMultiSelect<T: Initializable>(_ 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 }
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions GolangVsSwift.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Swift has reserve words for `case` and `default` and the operator support is not
<tr style="vertical-align: top;">
<td>

`rx(c)`
`receive(c)`
</td><td>

`case <-c:`
Expand All @@ -127,7 +127,7 @@ Swift has reserve words for `case` and `default` and the operator support is not
<tr>
<td>

`rx(c) { v in ... }`
`receive(c) { v in ... }`
</td><td>

`case v := <-c: ...`
Expand All @@ -137,7 +137,7 @@ Swift has reserve words for `case` and `default` and the operator support is not
<tr>
<td>

`tx(c, "foo")`
`send("foo", to: c)`
</td><td>

`case c <- "foo":`
Expand All @@ -147,7 +147,7 @@ Swift has reserve words for `case` and `default` and the operator support is not
<tr>
<td>

`tx(c, "foo") { ... }`
`send("foo", to: c) { ... }`
</td><td>

`case c <- "foo": ...`
Expand Down Expand Up @@ -198,7 +198,7 @@ let a = Channel<String>(capacity: 1)
await a <- "foo"

await select {
rx(a) { av in
receive(a) { av in
print(av!)
}
none {
Expand Down Expand Up @@ -235,7 +235,7 @@ default:
let a = Channel<String>(capacity: 1)

await select {
tx(a, "foo")
send("foo", to: a)
none {
print("Not called")
}
Expand Down Expand Up @@ -271,7 +271,7 @@ fmt.Println(<-a)
let a = Channel<Bool>()

await select {
rx(a)
receive(a)
none {
print("Default case!")
}
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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!) }
}
```

Expand All @@ -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")
Expand Down
24 changes: 12 additions & 12 deletions Sources/AsyncChannels/Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol SelectProtocol {
func handle(_ sm: SelectSignal) async -> Bool
}

struct RxHandler<T>: SelectProtocol {
struct ReceiveHandler<T>: SelectProtocol {

private var chan: Channel<T>
private let outFunc: (T?) async -> ()
Expand Down Expand Up @@ -48,7 +48,7 @@ struct NoneHandler: SelectProtocol {
}
}

struct TxHandler<T>: SelectProtocol {
struct SendHandler<T>: SelectProtocol {
private var chan: Channel<T>
private let val: T
private let onSend: () async -> ()
Expand Down Expand Up @@ -102,24 +102,24 @@ public func select(@SelectCollector cases: () -> ([SelectHandler])) async {
}
}

public func rx<T>(_ chan: Channel<T>, _ outFunc: @escaping (T?) async -> ()) -> SelectHandler {
return SelectHandler(inner: RxHandler(chan: chan, outFunc: outFunc))
public func receive<T>(_ chan: Channel<T>, _ outFunc: @escaping (T?) async -> ()) -> SelectHandler {
return SelectHandler(inner: ReceiveHandler(chan: chan, outFunc: outFunc))
}

public func rx<T>(_ chan: Channel<T>, _ outFunc: @escaping () async -> ()) -> SelectHandler {
return SelectHandler(inner: RxHandler(chan: chan, outFunc: { _ in await outFunc() }))
public func receive<T>(_ chan: Channel<T>, _ outFunc: @escaping () async -> ()) -> SelectHandler {
return SelectHandler(inner: ReceiveHandler(chan: chan, outFunc: { _ in await outFunc() }))
}

public func rx<T>(_ chan: Channel<T>) -> SelectHandler {
return SelectHandler(inner: RxHandler(chan: chan, outFunc: { _ in }))
public func receive<T>(_ chan: Channel<T>) -> SelectHandler {
return SelectHandler(inner: ReceiveHandler(chan: chan, outFunc: { _ in }))
}

public func tx<T>(_ chan: Channel<T>, _ val: T) -> SelectHandler {
return SelectHandler(inner: TxHandler(chan: chan, val: val, onSend: {}))
public func send<T>(_ val: T, to chan: Channel<T>) -> SelectHandler {
return SelectHandler(inner: SendHandler(chan: chan, val: val, onSend: {}))
}

public func tx<T>(_ chan: Channel<T>, _ val: T, _ onSend: @escaping () async -> ()) -> SelectHandler {
return SelectHandler(inner: TxHandler(chan: chan, val: val, onSend: onSend))
public func send<T>(_ val: T, to chan: Channel<T>, _ onSend: @escaping () async -> ()) -> SelectHandler {
return SelectHandler(inner: SendHandler(chan: chan, val: val, onSend: onSend))
}

public func none(handler: @escaping () async -> ()) -> SelectHandler {
Expand Down
58 changes: 29 additions & 29 deletions Tests/AsyncChannelsTests/AsyncChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class AsyncTest: XCTestCase {
let stop = Channel<Bool>()
Task {
await select {
rx(stop)
rx(sleep(for: duration)) {
receive(stop)
receive(sleep(for: duration)) {
XCTFail("Test timed out")
exit(1)
}
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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 }
}
}
Expand Down Expand Up @@ -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 }
}
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -358,7 +358,7 @@ final class AsyncTest: XCTestCase {
XCTAssertEqual(40, count)
}

func testTx() async {
func testSend() async {
let a = Channel<String>(capacity: 10)
let b = Channel<String>(capacity: 10)

Expand All @@ -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()
}
Expand All @@ -380,7 +380,7 @@ final class AsyncTest: XCTestCase {
var done = false
while !done {
await select {
rx(b) {
receive(b) {
countB += 1
XCTAssertEqual($0, "b")
}
Expand All @@ -393,12 +393,12 @@ final class AsyncTest: XCTestCase {
XCTAssertEqual(10, countB)
}

func testTxHandler() async {
func testSendHandler() async {
let a = Channel<String>(capacity: 1)
let testChan = Channel<Bool>(capacity: 1)

await select {
tx(a, "b") {
send("b", to: a) {
await testChan <- true
}
none {
Expand All @@ -414,7 +414,7 @@ final class AsyncTest: XCTestCase {

Task {
await select {
rx(a) { val in
receive(a) { val in
XCTAssertNil(val)
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -525,7 +525,7 @@ final class AsyncTest: XCTestCase {
var sum = 0
while !done {
await select {
rx(c) {
receive(c) {
sum += 1
}
none {
Expand Down

0 comments on commit 4fffbe8

Please sign in to comment.