Skip to content

Commit

Permalink
Add ContainerStorable.HasPointer()
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Oct 18, 2023
1 parent 800a44a commit eecb91a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error {
return NewEncodingError(err)
}

if a.hasPointer() {
if a.HasPointer() {
h.setHasPointers()
}

Expand Down Expand Up @@ -1002,7 +1002,7 @@ func (a *ArrayDataSlab) Uninline(storage SlabStorage) error {
return nil
}

func (a *ArrayDataSlab) hasPointer() bool {
func (a *ArrayDataSlab) HasPointer() bool {
for _, e := range a.elements {
if hasPointer(e) {
return true
Expand Down
4 changes: 2 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2705,7 +2705,7 @@ func (m *MapDataSlab) Encode(enc *Encoder) error {
return NewEncodingError(err)
}

if m.hasPointer() {
if m.HasPointer() {
h.setHasPointers()
}

Expand Down Expand Up @@ -3031,7 +3031,7 @@ func (m *MapDataSlab) canBeEncodedAsCompactMap() ([]Digest, []ComparableStorable
return elements.hkeys, keys, values, true
}

func (m *MapDataSlab) hasPointer() bool {
func (m *MapDataSlab) HasPointer() bool {
return m.elements.hasPointer()
}

Expand Down
19 changes: 9 additions & 10 deletions storable.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,12 @@ type ContainerStorable interface {
Storable

EncodeAsElement(*Encoder, InlinedExtraData) error
}

type containerStorable interface {
Storable
hasPointer() bool
HasPointer() bool
}

func hasPointer(storable Storable) bool {
if cs, ok := storable.(containerStorable); ok {
return cs.hasPointer()
if cs, ok := storable.(ContainerStorable); ok {
return cs.HasPointer()
}
return false
}
Expand All @@ -95,10 +91,9 @@ const (

type SlabIDStorable SlabID

var _ Storable = SlabIDStorable{}
var _ containerStorable = SlabIDStorable{}
var _ ContainerStorable = SlabIDStorable{}

func (v SlabIDStorable) hasPointer() bool {
func (v SlabIDStorable) HasPointer() bool {
return true
}

Expand Down Expand Up @@ -155,6 +150,10 @@ func (v SlabIDStorable) Encode(enc *Encoder) error {
return nil
}

func (v SlabIDStorable) EncodeAsElement(enc *Encoder, _ InlinedExtraData) error {
return v.Encode(enc)
}

func (v SlabIDStorable) ByteSize() uint32 {
// tag number (2 bytes) + byte string header (1 byte) + slab id (16 bytes)
return 2 + 1 + slabIDSize
Expand Down
19 changes: 15 additions & 4 deletions storable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,11 @@ type SomeStorable struct {
Storable Storable
}

var _ Storable = SomeStorable{}
var _ ContainerStorable = SomeStorable{}

func (v SomeStorable) hasPointer() bool {
if ms, ok := v.Storable.(containerStorable); ok {
return ms.hasPointer()
func (v SomeStorable) HasPointer() bool {
if ms, ok := v.Storable.(ContainerStorable); ok {
return ms.HasPointer()
}
return false
}
Expand All @@ -723,6 +723,17 @@ func (v SomeStorable) Encode(enc *Encoder) error {
return v.Storable.Encode(enc)
}

func (v SomeStorable) EncodeAsElement(enc *Encoder, inlinedExtraData InlinedExtraData) error {
err := enc.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, cborTagSomeValue,
})
if err != nil {
return err
}
return EncodeStorableAsElement(enc, v.Storable, inlinedExtraData)
}

func (v SomeStorable) ChildStorables() []Storable {
return []Storable{v.Storable}
}
Expand Down

0 comments on commit eecb91a

Please sign in to comment.