<commit_before>package capnp

import (
	"errors"
	"strconv"

	"golang.org/x/net/context"
)

// An Interface is a reference to a client in a message's capability table.
type Interface struct {
	seg *Segment
	cap CapabilityID
}

// NewInterface creates a new interface pointer.  No allocation is
// performed; s is only used for Segment()'s return value.
func NewInterface(s *Segment, cap CapabilityID) Interface {
	return Interface{
		seg: s,
		cap: cap,
	}
}

// ToInterface attempts to convert p into an interface.  If p is not a
// valid interface, then ToInterface returns an invalid Interface.
func ToInterface(p Pointer) Interface {
	if !IsValid(p) {
		return Interface{}
	}
	i, ok := p.underlying().(Interface)
	if !ok {
		return Interface{}
	}
	return i
}

// Segment returns the segment this pointer came from.
func (i Interface) Segment() *Segment {
	return i.seg
}

// HasData is always true.
func (i Interface) HasData() bool {
	return true
}

// Capability returns the capability ID of the interface.
func (i Interface) Capability() CapabilityID {
	return i.cap
}

// value returns a raw interface pointer with the capability ID.
func (i Interface) value(paddr Address) rawPointer {
	return rawInterfacePointer(i.cap)
}

func (i Interface) underlying() Pointer {
	return i
}

// Client returns the client stored in the message's capability table
// or nil if the pointer is invalid.
func (i Interface) Client() Client {
	if i.seg == nil {
		return nil
	}
	tab := i.seg.msg.CapTable
	if int64(i.cap) >= int64(len(tab)) {
		return nil
	}
	return tab[i.cap]
}

// ErrNullClient is returned from a call made on a null client pointer.
var ErrNullClient = errors.New("capnp: call on null client")

// A CapabilityID is an index into a message's capability table.
type CapabilityID uint32

// A Client represents an Cap'n Proto interface type.
//
// Generally, only RPC protocol implementers should provide types that
// implement Client: call ordering guarantees, promises, and
// synchronization are tricky to get right.  Prefer creating a server
// that wraps another interface than trying to implement Client.<commit_msg>capnp: clarify that clients are safe to use from multiple goroutines<commit_after>package capnp

import (
	"errors"
	"strconv"

	"golang.org/x/net/context"
)

// An Interface is a reference to a client in a message's capability table.
type Interface struct {
	seg *Segment
	cap CapabilityID
}

// NewInterface creates a new interface pointer.  No allocation is
// performed; s is only used for Segment()'s return value.
func NewInterface(s *Segment, cap CapabilityID) Interface {
	return Interface{
		seg: s,
		cap: cap,
	}
}

// ToInterface attempts to convert p into an interface.  If p is not a
// valid interface, then ToInterface returns an invalid Interface.
func ToInterface(p Pointer) Interface {
	if !IsValid(p) {
		return Interface{}
	}
	i, ok := p.underlying().(Interface)
	if !ok {
		return Interface{}
	}
	return i
}

// Segment returns the segment this pointer came from.
func (i Interface) Segment() *Segment {
	return i.seg
}

// HasData is always true.
func (i Interface) HasData() bool {
	return true
}

// Capability returns the capability ID of the interface.
func (i Interface) Capability() CapabilityID {
	return i.cap
}

// value returns a raw interface pointer with the capability ID.
func (i Interface) value(paddr Address) rawPointer {
	return rawInterfacePointer(i.cap)
}

func (i Interface) underlying() Pointer {
	return i
}

// Client returns the client stored in the message's capability table
// or nil if the pointer is invalid.
func (i Interface) Client() Client {
	if i.seg == nil {
		return nil
	}
	tab := i.seg.msg.CapTable
	if int64(i.cap) >= int64(len(tab)) {
		return nil
	}
	return tab[i.cap]
}

// ErrNullClient is returned from a call made on a null client pointer.
var ErrNullClient = errors.New("capnp: call on null client")

// A CapabilityID is an index into a message's capability table.
type CapabilityID uint32

// A Client represents an Cap'n Proto interface type.  It is safe to use
// from multiple goroutines.
//
// Generally, only RPC protocol implementers should provide types that
// implement Client: call ordering guarantees, promises, and
// synchronization are tricky to get right.  Prefer creating a server
// that wraps another interface than trying to implement Client.
{'fraction_non_alphanumeric': 0.08984643593857437, 'fraction_numerical': 0.0027504011001604402, 'mean_word_length': 3.774617067833698, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2521238', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1271, 'n_words': 559}