Skip to content

Commit

Permalink
Add TypeParam interface
Browse files Browse the repository at this point in the history
...and implement it for basic pointer types, and generated types where
appropriate.

This is the first step on generics support.
  • Loading branch information
zenhack committed Jul 30, 2022
1 parent a609dc7 commit b7a45ef
Show file tree
Hide file tree
Showing 18 changed files with 637 additions and 0 deletions.
21 changes: 21 additions & 0 deletions capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ type Interface struct {
cap CapabilityID
}

// i.EncodeAsPtr is equivalent to i.ToPtr(); for implementing TypeParam.
// The segment argument is ignored.
func (i Interface) EncodeAsPtr(*Segment) Ptr { return i.ToPtr() }

// DecodeFromPtr(p) is equivalent to p.Interface(); for implementing TypeParam.
func (Interface) DecodeFromPtr(p Ptr) Interface { return p.Interface() }

var _ TypeParam[Interface] = Interface{}

// NewInterface creates a new interface pointer.
//
// No allocation is performed in the given segment: it is used purely
Expand Down Expand Up @@ -572,6 +581,18 @@ func (c Client) Release() {
h.Shutdown()
}

func (c Client) EncodeAsPtr(seg *Segment) Ptr {
capId := seg.Message().AddCap(c)
return NewInterface(seg, capId).ToPtr()
}

func (Client) DecodeFromPtr(p Ptr) Client {
return p.Interface().Client()
}

var _ TypeParam[Client] = Client{}


// isResolve reports whether ch has been resolved.
// The caller must be holding onto ch.mu.
func (ch *clientHook) isResolved() bool {
Expand Down
3 changes: 3 additions & 0 deletions capnpc-go/templates/baseStructFuncs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ func (s {{.Node.Name}}) String() string {
}
{{end}}

func ({{.Node.Name}}) DecodeFromPtr(p {{.G.Capnp}}.Ptr) {{.Node.Name}} {
return {{.Node.Name}}{ Struct: {{.G.Capnp}}.Struct{}.DecodeFromPtr(p) }
}
4 changes: 4 additions & 0 deletions capnpc-go/templates/interfaceClient
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func (c {{$.Node.Name}}) AddRef() {{$.Node.Name}} {
Client: c.Client.AddRef(),
}
}

func ({{$.Node.Name}}) DecodeFromPtr(p {{.G.Capnp}}.Ptr) {{$.Node.Name}} {
return {{$.Node.Name}}{ Client: {{.G.Capnp}}.Client{}.DecodeFromPtr(p) }
}
Loading

0 comments on commit b7a45ef

Please sign in to comment.