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:
@ -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
|
||||
|
39
vlib/sync/channel_opt_propagate_test.v
Normal file
39
vlib/sync/channel_opt_propagate_test.v
Normal 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
|
||||
}
|
Reference in New Issue
Block a user