diff options
author | Yorhel <git@yorhel.nl> | 2012-03-30 12:47:29 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2012-03-30 12:47:29 +0200 |
commit | 1e36c6d184fdebedbd897bb06593e7f733a33a28 (patch) | |
tree | 4887d71908e22d7dd53583ca3be0f0d1fea3e280 | |
parent | 163fb3d7fbdb17a209518e7499a9538c6e7a917f (diff) |
Store and pass around tuples as value rather than reference
This makes Tuples more convenient to use because the index expression
(tup[n]) doesn't work on pointers to slices.
There's one exception for Registration.send(), which will use the
pointer to detect duplicates. (Pointers can be compared, slices can't).
-rw-r--r-- | msg.go | 4 | ||||
-rw-r--r-- | node.go | 7 | ||||
-rw-r--r-- | reg.go | 4 | ||||
-rw-r--r-- | ses.go | 4 | ||||
-rw-r--r-- | tuples.go | 6 |
5 files changed, 13 insertions, 12 deletions
@@ -1,7 +1,7 @@ package tanja type Message struct { - Tup *Tuple + Tup Tuple ret *ReturnPath reg *Registration } @@ -17,7 +17,7 @@ func (m *Message) Dispatch() { } } -func (*Message) Reply(t *Tuple) { +func (*Message) Reply(t Tuple) { // TODO } @@ -44,14 +44,15 @@ func (n *Node) reg(r *Registration) { // TODO: notify links } -func (n *Node) send(t *Tuple, path *ReturnPath) { +func (n *Node) send(t Tuple, path *ReturnPath) { // TODO: Some way of preventing that a message is routed back to the link // it came from. n.lock.Lock() defer n.lock.Unlock() for _, r := range n.regs { - if t.Match(*r.pat) { - r.send(t, path) + if t.Match(r.pat) { + // Pass a reference to the Tuple to allow duplicate detection. + r.send(&t, path) } } } @@ -1,7 +1,7 @@ package tanja type Registration struct { - pat *Tuple + pat Tuple id int32 active bool willReply bool @@ -64,7 +64,7 @@ func (r *Registration) unreg() { // Called while the node lock is held. func (r *Registration) send(t *Tuple, path *ReturnPath) { if s, ok := r.recv.(*Session); ok { - s.disp <- &Message{t, path, r} + s.disp <- &Message{*t, path, r} } // TODO: links } @@ -77,7 +77,7 @@ func (n *Node) Session() *Session { return s } -func (s *Session) Register(p *Tuple, willReply bool) *Registration { +func (s *Session) Register(p Tuple, willReply bool) *Registration { r := &Registration{} r.recv = s r.pat = p @@ -134,7 +134,7 @@ func (s *Session) Run() { } } -func (s *Session) Send(t *Tuple, wantReply bool) *ReturnPath { +func (s *Session) Send(t Tuple, wantReply bool) *ReturnPath { // TODO: fix a return path var r *ReturnPath s.node.send(t, r) @@ -120,11 +120,11 @@ func (t Tuple) Match(p Tuple) bool { return true } -// Same as &Tuple{El(a), El(b), ..} -func Tup(t ...interface{}) *Tuple { +// Same as Tuple{El(a), El(b), ..} +func Tup(t ...interface{}) Tuple { r := make(Tuple, len(t)) for i, v := range t { r[i] = El(v) } - return &r + return r } |