Have Encode return []byte instead of ([]byte, error).
Encoding can never fail, and returning an error is inconsistent with the
standard library's encoding/{ascii85,hex,pem} packages.
Fixes #8.
diff --git a/encode.go b/encode.go
index dda3724..2116951 100644
--- a/encode.go
+++ b/encode.go
@@ -79,7 +79,7 @@
// slice of dst if dst was large enough to hold the entire encoded block.
// Otherwise, a newly allocated slice will be returned.
// It is valid to pass a nil dst.
-func Encode(dst, src []byte) ([]byte, error) {
+func Encode(dst, src []byte) []byte {
if n := MaxEncodedLen(len(src)); len(dst) < n {
dst = make([]byte, n)
}
@@ -92,7 +92,7 @@
if len(src) != 0 {
d += emitLiteral(dst[d:], src)
}
- return dst[:d], nil
+ return dst[:d]
}
// Initialize the hash table. Its size ranges from 1<<8 to 1<<14 inclusive.
@@ -145,7 +145,7 @@
if lit != len(src) {
d += emitLiteral(dst[d:], src[lit:])
}
- return dst[:d], nil
+ return dst[:d]
}
// MaxEncodedLen returns the maximum length of a snappy block, given its
@@ -226,11 +226,7 @@
// Compress the buffer, discarding the result if the improvement
// isn't at least 12.5%.
chunkType := uint8(chunkTypeCompressedData)
- chunkBody, err := Encode(w.enc, uncompressed)
- if err != nil {
- w.err = err
- return n, err
- }
+ chunkBody := Encode(w.enc, uncompressed)
if len(chunkBody) >= len(uncompressed)-len(uncompressed)/8 {
chunkType, chunkBody = chunkTypeUncompressedData, uncompressed
}
@@ -244,11 +240,11 @@
w.buf[5] = uint8(checksum >> 8)
w.buf[6] = uint8(checksum >> 16)
w.buf[7] = uint8(checksum >> 24)
- if _, err = w.w.Write(w.buf[:]); err != nil {
+ if _, err := w.w.Write(w.buf[:]); err != nil {
w.err = err
return n, err
}
- if _, err = w.w.Write(chunkBody); err != nil {
+ if _, err := w.w.Write(chunkBody); err != nil {
w.err = err
return n, err
}
diff --git a/snappy_test.go b/snappy_test.go
index 7ec2373..7f1294f 100644
--- a/snappy_test.go
+++ b/snappy_test.go
@@ -24,11 +24,7 @@
)
func roundtrip(b, ebuf, dbuf []byte) error {
- e, err := Encode(ebuf, b)
- if err != nil {
- return fmt.Errorf("encoding error: %v", err)
- }
- d, err := Decode(dbuf, e)
+ d, err := Decode(dbuf, Encode(ebuf, b))
if err != nil {
return fmt.Errorf("decoding error: %v", err)
}
@@ -197,10 +193,7 @@
}
func benchDecode(b *testing.B, src []byte) {
- encoded, err := Encode(nil, src)
- if err != nil {
- b.Fatal(err)
- }
+ encoded := Encode(nil, src)
// Bandwidth is in amount of uncompressed data.
b.SetBytes(int64(len(src)))
b.ResetTimer()