codec: Add Encodable, Decodable interfaces

This commit is contained in:
Sasha Koshka 2025-06-04 12:20:04 -04:00
parent 1f62f6d973
commit ec965caa27
2 changed files with 15 additions and 0 deletions

View File

@ -2,6 +2,14 @@ package codec
import "io"
// Decodable is any type that can decode itself from a decoder.
type Decodable interface {
// Decode reads data from decoder, replacing the data of the object. It
// returns the amount of bytes written, and an error if the write
// stopped early.
Decode(decoder *Decoder) (n int, err error)
}
// Decoder wraps an [io.Reader] and decodes data from it.
type Decoder struct {
io.Reader

View File

@ -2,6 +2,13 @@ package codec
import "io"
// Encodable is any type that can write itself to an encoder.
type Encodable interface {
// Encode sends data to encoder. It returns the amount of bytes written,
// and an error if the write stopped early.
Encode(encoder *Encoder) (n int, err error)
}
// Encoder wraps an [io.Writer] and encodes data to it.
type Encoder struct {
io.Writer