1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: enable x = <-ch or {...} (#6195)

This commit is contained in:
Uwe Krüger
2020-08-23 02:12:05 +02:00
committed by GitHub
parent 7dfae2384b
commit 61df70fdf5
6 changed files with 126 additions and 14 deletions

View File

@ -15,13 +15,12 @@ mut:
// this function gets an array of channels for `St` references
fn do_rec_calc_send(chs []chan mut St) {
mut s := &St(0)
for {
if !(&sync.Channel(chs[0])).pop(&s) {
mut s := <-chs[0] or {
break
}
s.n++
(&sync.Channel(chs[1])).push(&s)
chs[1] <- s
}
}
@ -32,8 +31,8 @@ fn test_channel_array_mut() {
n: 100
}
for _ in 0 .. num_iterations {
(&sync.Channel(chs[0])).push(&t)
(&sync.Channel(chs[1])).pop(&t)
chs[0] <- t
t = <-chs[1]
}
(&sync.Channel(chs[0])).close()
assert t.n == 100 + num_iterations

View File

@ -0,0 +1,39 @@
import sync
const (
num_iterations = 10000
)
fn get_val_from_chan(ch chan i64) ?i64 {
r := <-ch?
return r
}
// this function gets an array of channels for `i64`
fn do_rec_calc_send(chs []chan i64, sem sync.Semaphore) {
mut msg := ''
for {
mut s := get_val_from_chan(chs[0]) or {
msg = err.str()
break
}
s++
chs[1] <- s
}
assert msg == 'channel closed'
sem.post()
}
fn test_channel_array_mut() {
mut chs := [chan i64{}, chan i64{cap: 10}]
sem := sync.new_semaphore()
go do_rec_calc_send(chs, sem)
mut t := i64(100)
for _ in 0 .. num_iterations {
chs[0] <- t
t = <-chs[1]
}
(&sync.Channel(chs[0])).close()
sem.wait()
assert t == 100 + num_iterations
}