1 // Phase 0c step 3: the declared ownership pair, `parent, child := edge[In, Out]{depth}`.
2 //
3 // The declaration allocates both directions' rings and FOUR fixed-direction
4 // handles in the root arena at the declaration (section 4: the storage cannot be
5 // a frame's), and returns two complementary Edge values whose roles never
6 // change: parent.Send writes parent -> child, child.Recv reads it, child.Send
7 // writes child -> parent, parent.Recv reads it. The compiler rewrites the form
8 // to the internal __edgealloc builtin and reads the two element types from the
9 // instantiated Edge type for the element sizes and frame budgets.
10 //
11 // spawn agreement and affineness are the next slice; this one pins the pair's
12 // existence and both directions, on both compilers.
13 //
14 //:wasmtest skip
15 package main
16 17 import "fmt"
18 19 type Msg struct {
20 Body []byte `cap:"1024"`
21 }
22 23 func main() {
24 select {
25 case <-shutdown:
26 return
27 default:
28 }
29 30 // A plain element type: two one-way rings, both directions. The parent is
31 // Edge[In, Out] (Recv In, Send Out); the child is the complement
32 // Edge[Out, In].
33 parent, child := edge[int32, int64]{2}
34 parent.Send <- 5
35 if (<-child.Recv) != 5 {
36 panic("parent -> child lost the value")
37 }
38 child.Send <- 9
39 if (<-parent.Recv) != 9 {
40 panic("child -> parent lost the value")
41 }
42 43 // A reference-carrying element: the request direction encodes through the
44 // codec, so the receiver gets its own copy (the same frame protocol a
45 // spawn channel uses).
46 // Request direction is the parent's Send (Out), response is its Recv (In).
47 req, resp := edge[int32, Msg]{4}
48 body := []byte{:100}
49 for i := int32(0); i < len(body); i++ {
50 body[i] = byte(i & 255)
51 }
52 req.Send <- Msg{Body: body}
53 got := <-resp.Recv
54 if len(got.Body) != 100 || got.Body[99] != 99 {
55 panic("codec element lost its payload")
56 }
57 resp.Send <- 42
58 if (<-req.Recv) != 42 {
59 panic("response direction lost the value")
60 }
61 62 // Closing a sender releases that direction's ring once the peer end is
63 // done with it; the receiver handles cannot be closed by the language
64 // (a Receiver is receive-only), so their resources go with the domain.
65 close(parent.Send)
66 close(child.Send)
67 close(req.Send)
68 close(resp.Send)
69 fmt.Println("edge decl ok")
70 }
71