Commit 668418d1 authored by Robert Griesemer's avatar Robert Griesemer

go/token: remove dependency on encoding/gob

R=dsymonds
CC=golang-dev
https://golang.org/cl/5636053
parent 1f133e2b
......@@ -867,7 +867,10 @@ func (x *Index) Write(w io.Writer) error {
return err
}
if fulltext {
if err := x.fset.Write(w); err != nil {
encode := func(x interface{}) error {
return gob.NewEncoder(w).Encode(x)
}
if err := x.fset.Write(encode); err != nil {
return err
}
if err := x.suffixes.Write(w); err != nil {
......@@ -897,7 +900,10 @@ func (x *Index) Read(r io.Reader) error {
return err
}
x.suffixes = new(suffixarray.Index)
if err := x.suffixes.Read(r); err != nil {
decode := func(x interface{}) error {
return gob.NewDecoder(r).Decode(x)
}
if err := x.suffixes.Read(decode); err != nil {
return err
}
}
......
......@@ -4,11 +4,6 @@
package token
import (
"encoding/gob"
"io"
)
type serializedFile struct {
// fields correspond 1:1 to fields with same (lower-case) name in File
Name string
......@@ -23,19 +18,10 @@ type serializedFileSet struct {
Files []serializedFile
}
func (s *serializedFileSet) Read(r io.Reader) error {
return gob.NewDecoder(r).Decode(s)
}
func (s *serializedFileSet) Write(w io.Writer) error {
return gob.NewEncoder(w).Encode(s)
}
// Read reads the fileset from r into s; s must not be nil.
// If r does not also implement io.ByteReader, it will be wrapped in a bufio.Reader.
func (s *FileSet) Read(r io.Reader) error {
// Read calls decode to deserialize a file set into s; s must not be nil.
func (s *FileSet) Read(decode func(interface{}) error) error {
var ss serializedFileSet
if err := ss.Read(r); err != nil {
if err := decode(&ss); err != nil {
return err
}
......@@ -53,8 +39,8 @@ func (s *FileSet) Read(r io.Reader) error {
return nil
}
// Write writes the fileset s to w.
func (s *FileSet) Write(w io.Writer) error {
// Write calls encode to serialize the file set s.
func (s *FileSet) Write(encode func(interface{}) error) error {
var ss serializedFileSet
s.mutex.Lock()
......@@ -66,5 +52,5 @@ func (s *FileSet) Write(w io.Writer) error {
ss.Files = files
s.mutex.Unlock()
return ss.Write(w)
return encode(ss)
}
......@@ -6,6 +6,7 @@ package token
import (
"bytes"
"encoding/gob"
"fmt"
"testing"
)
......@@ -69,12 +70,18 @@ func equal(p, q *FileSet) error {
func checkSerialize(t *testing.T, p *FileSet) {
var buf bytes.Buffer
if err := p.Write(&buf); err != nil {
encode := func(x interface{}) error {
return gob.NewEncoder(&buf).Encode(x)
}
if err := p.Write(encode); err != nil {
t.Errorf("writing fileset failed: %s", err)
return
}
q := NewFileSet()
if err := q.Read(&buf); err != nil {
decode := func(x interface{}) error {
return gob.NewDecoder(&buf).Decode(x)
}
if err := q.Read(decode); err != nil {
t.Errorf("reading fileset failed: %s", err)
return
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment