1 // Phase 0c step 3b: a declared pair crosses a spawn boundary.
2 //
3 // `parent, child := edge[In, Out]{d}` makes the parent an Edge[In, Out] (Recv
4 // In, Send Out) and the child its complement Edge[Out, In], so the child's
5 // Receive end is typed for what the parent sends. `spawn(worker, child)` passes
6 // that value; the compiler checks it against the target's parameter by NAME
7 // (a same-shaped struct would not pass) and binds the child's breaker on all
8 // four handles through the pair record, so a parent parked on either direction
9 // wakes when the child ends.
10 //
11 //:wasmtest skip
12 package main
13 14 import "fmt"
15 16 type Req struct {
17 Body []byte `cap:"1024"`
18 }
19 20 func worker(ep Edge[Req, int32]) (closed chan struct{}) {
21 for {
22 select {
23 case <-closed:
24 return closed
25 case r := <-ep.Recv:
26 if len(r.Body) == 0 {
27 return closed
28 }
29 ep.Send <- int32(len(r.Body))
30 }
31 }
32 }
33 34 func main() {
35 select {
36 case <-shutdown:
37 return
38 default:
39 }
40 parent, child := edge[int32, Req]{2}
41 done := spawn(worker, child)
42 for _, n := range []int32{10, 300, 1000} {
43 parent.Send <- Req{Body: []byte{:n}}
44 if got := <-parent.Recv; got != n {
45 panic("edge response lost")
46 }
47 }
48 close(parent.Send)
49 <-done
50 fmt.Println("edge spawn ok")
51 }
52