This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows us to marshal/unmarshal/size protobufs without copying CID around.
- Loading branch information
Showing
5 changed files
with
137 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bitswap_message_pb | ||
|
||
import ( | ||
"github.com/ipfs/go-cid" | ||
) | ||
|
||
// NOTE: Don't "embed" the cid, wrap it like we're doing here. Otherwise, gogo | ||
// will try to use the Bytes() function. | ||
|
||
// Cid is a custom type for CIDs in protobufs, that allows us to avoid | ||
// reallocating. | ||
type Cid struct { | ||
Cid cid.Cid | ||
} | ||
|
||
func (c Cid) Marshal() ([]byte, error) { | ||
return c.Cid.Bytes(), nil | ||
} | ||
|
||
func (c *Cid) MarshalTo(data []byte) (int, error) { | ||
return copy(data[:c.Size()], c.Cid.Bytes()), nil | ||
} | ||
|
||
func (c *Cid) Unmarshal(data []byte) (err error) { | ||
c.Cid, err = cid.Cast(data) | ||
return err | ||
} | ||
|
||
func (c *Cid) Size() int { | ||
return len(c.Cid.KeyString()) | ||
} | ||
|
||
func (c Cid) MarshalJSON() ([]byte, error) { | ||
return c.Cid.MarshalJSON() | ||
} | ||
|
||
func (c *Cid) UnmarshalJSON(data []byte) error { | ||
return c.Cid.UnmarshalJSON(data) | ||
} | ||
|
||
func (c Cid) Equal(other Cid) bool { | ||
return c.Cid.Equals(c.Cid) | ||
} |
Oops, something went wrong.