Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First steps towards generics #274

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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