Skip to content

Commit

Permalink
unixfs: Add support for byte slices backed CIDs for zero allocations
Browse files Browse the repository at this point in the history
```
$ benchstat /mnt/ramdisk/{old,new}
name   old time/op    new time/op    delta
PB-12     730ns ± 5%     255ns ± 2%   -65.04%  (p=0.000 n=9+10)

name   old alloc/op   new alloc/op   delta
PB-12      224B ± 0%        0B       -100.00%  (p=0.000 n=10+10)

name   old allocs/op  new allocs/op  delta
PB-12      4.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
```

Depends on ipfs/go-cid#159.
  • Loading branch information
Jorropo committed Jun 12, 2023
1 parent d47c419 commit 234debc
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 72 deletions.
6 changes: 3 additions & 3 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/ipfs/boxo v0.7.1-0.20230323075409-f4a8dd6614df
github.com/ipfs/go-block-format v0.1.2
github.com/ipfs/go-cid v0.4.0
github.com/ipfs/go-cid v0.4.2-0.20230612091241-80d1e915f662
github.com/ipfs/go-datastore v0.6.0
github.com/ipld/go-ipld-prime v0.20.0
github.com/libp2p/go-libp2p v0.26.3
Expand Down Expand Up @@ -102,8 +102,8 @@ require (
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.1.1 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/ginkgo/v2 v2.5.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ github.com/ipfs/go-blockservice v0.5.0 h1:B2mwhhhVQl2ntW2EIpaWPwSCxSuqr5fFA93Ms4
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
github.com/ipfs/go-cid v0.4.0 h1:a4pdZq0sx6ZSxbCizebnKiMCx/xI/aBBFlB73IgH4rA=
github.com/ipfs/go-cid v0.4.0/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
github.com/ipfs/go-cid v0.4.2-0.20230612091241-80d1e915f662 h1:jWQ5yOEmR1Fvv6Rj8mEye4QTeGQoKKECMieju9z5kgA=
github.com/ipfs/go-cid v0.4.2-0.20230612091241-80d1e915f662/go.mod h1:4rtyA9XdBeZBapaRNJuTY9H+/6bG4URx/cVwjAzK6fw=
github.com/ipfs/go-datastore v0.6.0 h1:JKyz+Gvz1QEZw0LsX1IBn+JFCJQH4SJVFtM4uWU0Myk=
github.com/ipfs/go-datastore v0.6.0/go.mod h1:rt5M3nNbSO/8q1t4LNkLyUwRs8HupMeN/8O4Vn9YAT8=
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=
Expand Down Expand Up @@ -469,16 +469,16 @@ github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/e
github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo=
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
github.com/multiformats/go-multibase v0.1.1 h1:3ASCDsuLX8+j4kx58qnJ4YFq/JWTJpCyDW27ztsVTOI=
github.com/multiformats/go-multibase v0.1.1/go.mod h1:ZEjHE+IsUrgp5mhlEAYjMtZwK1k4haNkcaPg9aoe1a8=
github.com/multiformats/go-multibase v0.2.0 h1:isdYCVLvksgWlMW9OZRYJEa9pZETFivncJHmHnnd87g=
github.com/multiformats/go-multibase v0.2.0/go.mod h1:bFBZX4lKCA/2lyOFSAoKH5SS6oPyjtnzK/XTFDPkNuk=
github.com/multiformats/go-multicodec v0.8.1 h1:ycepHwavHafh3grIbR1jIXnKCsFm0fqsfEOsJ8NtKE8=
github.com/multiformats/go-multicodec v0.8.1/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
github.com/multiformats/go-multihash v0.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108=
github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc=
github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U=
github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM=
github.com/multiformats/go-multistream v0.4.1 h1:rFy0Iiyn3YT0asivDUIR05leAdwZq3de4741sbiSdfo=
github.com/multiformats/go-multistream v0.4.1/go.mod h1:Mz5eykRVAjJWckE2U78c6xqdtyNUEhKSM0Lwar2p77Q=
github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/ipfs/bbloom v0.0.4
github.com/ipfs/go-bitfield v1.1.0
github.com/ipfs/go-block-format v0.1.2
github.com/ipfs/go-cid v0.4.0
github.com/ipfs/go-cid v0.4.2-0.20230612091241-80d1e915f662
github.com/ipfs/go-cidutil v0.1.0
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-detect-race v0.0.1
Expand Down Expand Up @@ -48,9 +48,9 @@ require (
github.com/multiformats/go-base32 v0.1.0
github.com/multiformats/go-multiaddr v0.8.0
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multibase v0.1.1
github.com/multiformats/go-multibase v0.2.0
github.com/multiformats/go-multicodec v0.8.1
github.com/multiformats/go-multihash v0.2.1
github.com/multiformats/go-multihash v0.2.3
github.com/multiformats/go-multistream v0.4.1
github.com/multiformats/go-varint v0.0.7
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUP
github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog=
github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
github.com/ipfs/go-cid v0.4.0 h1:a4pdZq0sx6ZSxbCizebnKiMCx/xI/aBBFlB73IgH4rA=
github.com/ipfs/go-cid v0.4.0/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
github.com/ipfs/go-cid v0.4.2-0.20230612091241-80d1e915f662 h1:jWQ5yOEmR1Fvv6Rj8mEye4QTeGQoKKECMieju9z5kgA=
github.com/ipfs/go-cid v0.4.2-0.20230612091241-80d1e915f662/go.mod h1:4rtyA9XdBeZBapaRNJuTY9H+/6bG4URx/cVwjAzK6fw=
github.com/ipfs/go-cidutil v0.1.0 h1:RW5hO7Vcf16dplUU60Hs0AKDkQAVPVplr7lk97CFL+Q=
github.com/ipfs/go-cidutil v0.1.0/go.mod h1:e7OEVBMIv9JaOxt9zaGEmAoSlXW9jdFZ5lP/0PwcfpA=
github.com/ipfs/go-datastore v0.5.0/go.mod h1:9zhEApYMTl17C8YDp7JmU7sQZi2/wqiYh73hakZ90Bk=
Expand Down Expand Up @@ -492,16 +492,16 @@ github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/e
github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo=
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
github.com/multiformats/go-multibase v0.1.1 h1:3ASCDsuLX8+j4kx58qnJ4YFq/JWTJpCyDW27ztsVTOI=
github.com/multiformats/go-multibase v0.1.1/go.mod h1:ZEjHE+IsUrgp5mhlEAYjMtZwK1k4haNkcaPg9aoe1a8=
github.com/multiformats/go-multibase v0.2.0 h1:isdYCVLvksgWlMW9OZRYJEa9pZETFivncJHmHnnd87g=
github.com/multiformats/go-multibase v0.2.0/go.mod h1:bFBZX4lKCA/2lyOFSAoKH5SS6oPyjtnzK/XTFDPkNuk=
github.com/multiformats/go-multicodec v0.8.1 h1:ycepHwavHafh3grIbR1jIXnKCsFm0fqsfEOsJ8NtKE8=
github.com/multiformats/go-multicodec v0.8.1/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
github.com/multiformats/go-multihash v0.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108=
github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc=
github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U=
github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM=
github.com/multiformats/go-multistream v0.4.1 h1:rFy0Iiyn3YT0asivDUIR05leAdwZq3de4741sbiSdfo=
github.com/multiformats/go-multistream v0.4.1/go.mod h1:Mz5eykRVAjJWckE2U78c6xqdtyNUEhKSM0Lwar2p77Q=
github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
Expand Down
28 changes: 16 additions & 12 deletions unixfs/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ const (
// optional Data Data = 1;
// }

func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inCid cid.Cid, origData []byte) (typ Type, file File, dir Directory, sym Symlink, err error) {
func parsePB[Self, Children cid.Storage](
fileChildrens []FileEntry[Children],
directoryChildrens []DirectoryEntry[Children],
inCid cid.GenericCid[Self], origData []byte,
) (typ Type, file File[Self, Children], dir Directory[Self, Children], sym Symlink[Self], err error) {
var dataType uint64
var fileLinks, blocksizes uint
var content []byte
Expand All @@ -81,8 +85,8 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC
// FIXME: is an empty name a valid file name in a directory ?
directoryChildrens = slices.Grow(directoryChildrens, len(fileChildrens)+extra)
for _, v := range fileChildrens {
directoryChildrens = append(directoryChildrens, DirectoryEntry{
Entry: Entry{Cid: v.Cid, tSize: v.tSize},
directoryChildrens = append(directoryChildrens, DirectoryEntry[Children]{
Entry: Entry[Children]{Cid: v.Cid, tSize: v.tSize},
Name: AliasableString{},
})
}
Expand Down Expand Up @@ -126,7 +130,7 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC
}

// FIXME: add support aliased CIDs in github.com/ipfs/go-cid
var c cid.Cid
var c cid.GenericCid[Children]
var name []byte
var tSize uint64 // will be offset by +1, zero means not found

Expand Down Expand Up @@ -182,7 +186,7 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC
fileChildrens[blocksizes].FileSize = blocksize
} else {
// we have discovered more blocksizes than links at this point, add new entries
fileChildrens = append(fileChildrens, FileEntry{FileSize: blocksize})
fileChildrens = append(fileChildrens, FileEntry[Children]{FileSize: blocksize})
}
blocksizes++
return nil
Expand Down Expand Up @@ -244,7 +248,7 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC
}
mData = mData[l:]

c, err = cid.Cast(cBytes)
c, err = cid.CastGeneric[Children](cBytes)
if err != nil {
err = fmt.Errorf("failed to decode cid: %w", err)
return
Expand Down Expand Up @@ -289,7 +293,7 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC

if outerNumber == 2 {
// repeated PBLink Links = 2;
if c == cid.Undef {
if !c.Defined() {
err = errors.New("link is missing CID")
}

Expand All @@ -306,8 +310,8 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC
moveZeroNamedDirectoryEntriesToDirectoryChildrens(1)
}

directoryChildrens = append(directoryChildrens, DirectoryEntry{
Entry: Entry{Cid: c, tSize: tSize},
directoryChildrens = append(directoryChildrens, DirectoryEntry[Children]{
Entry: Entry[Children]{Cid: c, tSize: tSize},
Name: AliasableString(name),
})
} else {
Expand All @@ -318,7 +322,7 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC
fileChildrens[fileLinks].tSize = tSize
} else {
// we have discovered more links than blocksizes at this point, add new entries
fileChildrens = append(fileChildrens, FileEntry{Entry: Entry{Cid: c, tSize: tSize}})
fileChildrens = append(fileChildrens, FileEntry[Children]{Entry: Entry[Children]{Cid: c, tSize: tSize}})
}
fileLinks++
}
Expand Down Expand Up @@ -350,8 +354,8 @@ func parsePB(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inC
}

typ = TFile
file = File{
Entry: Entry{Cid: inCid, tSize: selfTSize + uint64(len(origData))},
file = File[Self, Children]{
Entry: Entry[Self]{Cid: inCid, tSize: selfTSize + uint64(len(origData))},
Data: content,
Childrens: fileChildrens,
}
Expand Down
7 changes: 4 additions & 3 deletions unixfs/pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func BenchmarkPB(b *testing.B) {
if err != nil {
b.Fatal()
}
c := cid.NewCidV0(mh)
c := cid.NewCidV0Generic[[]byte](mh)

b.ResetTimer()
var out []FileEntry
var out []FileEntry[[]byte]
for i := b.N; i != 0; i-- {
_, f, _, _, _ := parsePB(out[:0], nil, c, data)
out = f.Childrens
Expand All @@ -40,6 +40,7 @@ func FuzzPB(f *testing.F) {
// Assume a block limit is inplace.
return
}
parsePB(nil, nil, cid.Undef, b)
var zero cid.GenericCid[[]byte]
parsePB[[]byte, []byte](nil, nil, zero, b)
})
}
62 changes: 33 additions & 29 deletions unixfs/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,62 @@ import (
)

// Entry is a basic unit block.
type Entry struct {
Cid cid.Cid
type Entry[S cid.Storage] struct {
Cid cid.GenericCid[S]
// tSize encode the comulative size of the DAG.
// the zero value indicates tsize is missing.
tSize uint64
}

func (e Entry) TSize() (tsize uint64, ok bool) {
func (e Entry[S]) TSize() (tsize uint64, ok bool) {
if e.tSize == 0 {
return 0, false
}

return e.tSize - 1, true
}

func (e Entry) Untyped() Entry {
func (e Entry[S]) Untyped() Entry[S] {
return e
}

var _ Node = File{}
var _ Node[string] = File[string, string]{}

type File struct {
type File[Self, Children cid.Storage] struct {
badge
Entry
Entry[Self]
Data []byte
Childrens []FileEntry
Childrens []FileEntry[Children]
}

func FileEntryWithTSize(c cid.Cid, fileSize, tSize uint64) FileEntry {
return FileEntry{Entry: Entry{Cid: c, tSize: tSize + 1}, FileSize: fileSize}
func FileEntryWithTSize[S cid.Storage](c cid.GenericCid[S], fileSize, tSize uint64) FileEntry[S] {
return FileEntry[S]{Entry: Entry[S]{Cid: c, tSize: tSize + 1}, FileSize: fileSize}
}

type FileEntry struct {
Entry
type FileEntry[S cid.Storage] struct {
Entry[S]
// FileSize is the logical size of the file at this location once decoded.
FileSize uint64
}

var _ Node = Directory{}
var _ Node[string] = Directory[string, string]{}

type Directory struct {
type Directory[Self, Children cid.Storage] struct {
badge
Entry
Childrens []DirectoryEntry
Entry[Self]
Childrens []DirectoryEntry[Children]
}

type DirectoryEntry struct {
Entry
type DirectoryEntry[S cid.Storage] struct {
Entry[S]
Name AliasableString
}

var _ Node = Symlink{}
var _ Node[string] = Symlink[string]{}

type Symlink struct {
type Symlink[S cid.Storage] struct {
badge
Entry
Entry[S]
Value []byte
}

Expand All @@ -86,9 +86,9 @@ func (badge) nodeBadge() {

// Node is an interface that can exclusively be a [File], [Directory] or [Symlink]. We might add more in the future.
// You MUST NOT embed this interface, it's only purpose is to provide type safe enums.
type Node interface {
type Node[S cid.Storage] interface {
// Untyped returns the untyped [Entry] for that value stripped of all type related information.
Untyped() Entry
Untyped() Entry[S]
// nodeBadge must never be called it's just here to trick the type checker.
nodeBadge()
}
Expand All @@ -97,8 +97,8 @@ type Node interface {
// [File.Data], [DirectoryEntry.Name] and [Symlink.Value] values are aliased to b.RawData().
// The data argument MUST hash to cid, this wont check the validaty of the hash.
// It assumes the size of the block is limited and reasonable.
func Parse(b blocks.Block) (Node, error) {
switch t, f, d, s, err := ParseAppend(nil, nil, b.Cid(), b.RawData()); t {
func Parse[Children cid.Storage](b blocks.Block) (Node[string], error) {
switch t, f, d, s, err := ParseAppend[string, Children](nil, nil, b.Cid(), b.RawData()); t {
case TError:
return nil, err
case TFile:
Expand All @@ -121,7 +121,11 @@ func Parse(b blocks.Block) (Node, error) {
// It only ever clobber extra capacity within the slices, it may do so in the case of an error.
// The data argument MUST hash to cid, this wont check the validaty of the hash.
// It assumes the size of the block is limited and reasonable.
func ParseAppend(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry, inCid cid.Cid, data []byte) (t Type, f File, d Directory, s Symlink, err error) {
func ParseAppend[Self, Children cid.Storage](
fileChildrens []FileEntry[Children],
directoryChildrens []DirectoryEntry[Children],
inCid cid.GenericCid[Self], data []byte,
) (t Type, f File[Self, Children], d Directory[Self, Children], s Symlink[Self], err error) {
// Avoid clobbering the used part of the slice.
fileChildrens = fileChildrens[len(fileChildrens):]
directoryChildrens = directoryChildrens[len(directoryChildrens):]
Expand All @@ -130,8 +134,8 @@ func ParseAppend(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry,
switch c := multicodec.Code(pref.Codec); c {
case multicodec.Raw:
t = TFile
f = File{
Entry: Entry{
f = File[Self, Children]{
Entry: Entry[Self]{
Cid: inCid,
tSize: uint64(len(data)) + 1,
},
Expand All @@ -140,7 +144,7 @@ func ParseAppend(fileChildrens []FileEntry, directoryChildrens []DirectoryEntry,
}
return
case multicodec.DagPb:
return parsePB(fileChildrens, directoryChildrens, inCid, data)
return parsePB[Self, Children](fileChildrens, directoryChildrens, inCid, data)
default:
err = errors.New("unsupported codec: " + c.String())
return
Expand Down
Loading

0 comments on commit 234debc

Please sign in to comment.