Skip to content

Commit 01c8417

Browse files
authored
Fix array.Cap() (#138)
The `array` can be built by calling `arrayFromSlice()`. We cannot be sure that the `cap` of the underlying byte slice is the same as its `len`.
1 parent 87b8a9a commit 01c8417

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

array.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func arrayFromSlice(x interface{}) array {
4747

4848
func (a *array) Len() int { return a.Header.TypedLen(a.t.Type) }
4949

50-
func (a *array) Cap() int { return a.Header.TypedLen(a.t.Type) }
50+
func (a *array) Cap() int { return a.Header.TypedCap(a.t.Type) }
5151

5252
// fromSlice populates the value from a slice
5353
func (a *array) fromSlice(x interface{}) {
@@ -91,10 +91,8 @@ func (a *array) sliceInto(i, j int, res *array) {
9191

9292
s := i * int(a.t.Size())
9393
e := j * int(a.t.Size())
94-
c = c - i
9594

9695
res.Raw = a.Raw[s:e]
97-
9896
}
9997

10098
// slice slices an array

internal/storage/header.go

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func (h *Header) TypedLen(t reflect.Type) int {
1717
return len(h.Raw) / int(t.Size())
1818
}
1919

20+
func (h *Header) TypedCap(t reflect.Type) int {
21+
return cap(h.Raw) / int(t.Size())
22+
}
23+
2024
func Copy(t reflect.Type, dst, src *Header) int {
2125
copied := copy(dst.Raw, src.Raw)
2226
return copied / int(t.Size())

0 commit comments

Comments
 (0)