Skip to content

Commit

Permalink
Turn ClientSnapshot.IsPromise into a method.
Browse files Browse the repository at this point in the history
Single source of truth and all that.
  • Loading branch information
zenhack committed May 26, 2023
1 parent 125f4e7 commit b57e496
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
22 changes: 11 additions & 11 deletions capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,8 @@ func (c Client) WeakRef() WeakClient {
// Snapshot reads the current state of the client. It returns the zero
// ClientSnapshot if c is nil, has resolved to null, or has been released.
func (c Client) Snapshot() ClientSnapshot {
h, resolved, _ := c.startCall()
if h == nil {
return ClientSnapshot{}
}
return ClientSnapshot{
hook: h,
IsPromise: !resolved,
}
h, _, _ := c.startCall()
return ClientSnapshot{hook: h}
}

// A Brand is an opaque value used to identify a capability.
Expand All @@ -585,15 +579,21 @@ type Brand struct {
// redirect to point at the resolution.
type ClientSnapshot struct {
hook *rc.Ref[clientHook]
// IsPromise is true if the client was an unresolved promise when
// this snapshot was taken.
IsPromise bool
}

func (cs ClientSnapshot) IsValid() bool {
return cs.hook.IsValid()
}

// IsPromise returns true if the snapshot is a promise.
func (cs ClientSnapshot) IsPromise() bool {
if cs.hook == nil {
return false
}
_, ret := cs.hook.Value().resolution.Get()
return ret
}

// Send implements ClientHook.Send
func (cs ClientSnapshot) Send(ctx context.Context, s Send) (*Answer, ReleaseFunc) {
return cs.hook.Value().Send(ctx, s)
Expand Down
10 changes: 5 additions & 5 deletions capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestClient(t *testing.T) {
t.Error("new client is not valid")
}
state := c.Snapshot()
if state.IsPromise {
if state.IsPromise() {
t.Error("c.State().IsPromise = true; want false")
}
if state.Brand().Value != int(42) {
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestReleasedClient(t *testing.T) {
if state.Brand().Value != nil {
t.Errorf("c.Snapshot().Brand().Value = %#v; want <nil>", state.Brand().Value)
}
if state.IsPromise {
if state.IsPromise() {
t.Error("c.Snapshot().IsPromise = true; want false")
}
state.Release()
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestNullClient(t *testing.T) {
if state.Brand().Value != nil {
t.Errorf("c.Snapshot().Brand() = %#v; want <nil>", state.Brand())
}
if state.IsPromise {
if state.IsPromise() {
t.Error("c.Snapshot().IsPromise = true; want false")
}
state.Release()
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestPromisedClient(t *testing.T) {
if state.Brand().Value != int(111) {
t.Errorf("before resolution, ca.Snapshot().Brand().Value = %#v; want 111", state.Brand().Value)
}
if !state.IsPromise {
if !state.IsPromise() {
t.Error("before resolution, ca.Snapshot().IsPromise = false; want true")
}
state.Release()
Expand All @@ -259,7 +259,7 @@ func TestPromisedClient(t *testing.T) {
if state.Brand().Value != int(222) {
t.Errorf("after resolution, ca.Snapshot().Brand().Value = %#v; want 222", state.Brand().Value)
}
if state.IsPromise {
if state.IsPromise() {
t.Error("after resolution, ca.Snapshot().IsPromise = true; want false")
}
state.Release()
Expand Down
2 changes: 1 addition & 1 deletion rpc/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (c *lockedConn) sendCap(d rpccp.CapDescriptor, client capnp.Client) (_ expo
ee = &expent{
client: client.AddRef(),
wireRefs: 1,
isPromise: state.IsPromise,
isPromise: state.IsPromise(),
cancel: func() {},
}
id = exportID(c.lk.exportID.next())
Expand Down

0 comments on commit b57e496

Please sign in to comment.