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

Client: hide pointer indirection. #260

Merged
merged 2 commits into from
Jul 16, 2022

Conversation

zenhack
Copy link
Contributor

@zenhack zenhack commented Jul 12, 2022

This changes the APIs so that everything operates on Client instead of
*Client. This will smooth over a couple things I've run into, mainly
around being able to implement interfaces where a method needs to
re-assign the receiver.

I'm open to debate as to whether this is a good change. Copying my message from matrix for posterity:

@lthibault: How do you feel about changing the API to use Client instead of *Client everywhere (and probably hide the pointer indirection inside the struct)? I think this would make a lot of stuff esp. with generics cleaner.

E.g. on my generics branch right now I have:

package capnp

// The TypeParam interface must be satisified by a type to be used as a canpproto
// type parameter. This is satisified by all capnproto pointer types. T should
// be instantiated by the type itself; in type parameter lists you will typically
// see parameter/constraints like T TypeParam[T].
type TypeParam[T any] interface {
	// Convert the receiver to a Ptr, storing it in seg if it is not
	// already associated with some message (only true for Clients and
	// wrappers around them.
	EncodeAsPtr(seg *Segment) Ptr

  // Decode the pointer as the type of the receiver. Generally,
  // the reciever will be the zero value for the type.
  DecodeFromPtr(p Ptr) T
}

...but I'd like to drop the T parameter so it would just be:

type TypeParam interface {
   EncodeAsPtr(*Segment) Ptr
   DecodeFromPtr(Ptr)
}

...but I can't do the latter because I would have to implement DecodeFromPtr on **Client, since it needs to modify the pointer. But you can't define methods on double-indirect pointers like that.
Returning the T is kindof an awful hack so we can just define it on *Client instead.
But if we hide the pointer indirection inside the struct, then we can define the method on *Client, and it can modify it in place.

Despite the size of the diff, I don't think this will be horribly disruptive to downstream code; for ocap-md I just needed to re-run the schema compiler and it just worked. Changes should be pretty mechanical in the cases where you are working with raw Clients.

This changes the APIs so that everything operates on Client instead of
*Client. This will smooth over a couple things I've run into, mainly
around being able to implement interfaces where a method needs to
re-assign the receiver.
@zenhack zenhack requested a review from lthibault July 15, 2022 22:07
@zenhack
Copy link
Contributor Author

zenhack commented Jul 15, 2022

Looks like the tests are passing again here too.

capability_test.go Show resolved Hide resolved
Copy link
Collaborator

@lthibault lthibault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lthibault lthibault merged commit 8ad5f39 into capnproto:main Jul 16, 2022
@zenhack zenhack deleted the hide-client-pointer branch July 18, 2022 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants