WriteTo writes out a serialized representation of g to w. Implements the io.WriterTo interface.
(w io.Writer)
| 79 | // |
| 80 | // Implements the io.WriterTo interface. |
| 81 | func (g *Graph) WriteTo(w io.Writer) (int64, error) { |
| 82 | buf := C.TF_NewBuffer() |
| 83 | defer C.TF_DeleteBuffer(buf) |
| 84 | status := newStatus() |
| 85 | C.TF_GraphToGraphDef(g.c, buf, status.c) |
| 86 | if err := status.Err(); err != nil { |
| 87 | return 0, err |
| 88 | } |
| 89 | if buf.length > (1 << 30) { |
| 90 | // For very large graphs, the writes can be chunked. |
| 91 | // Punt on that for now. |
| 92 | return 0, fmt.Errorf("Graph is too large to write out, Graph.WriteTo needs to be updated") |
| 93 | } |
| 94 | // A []byte slice backed by C memory. |
| 95 | // See: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices |
| 96 | length := int(buf.length) |
| 97 | slice := (*[1 << 30]byte)(unsafe.Pointer(buf.data))[:length:length] |
| 98 | n, err := w.Write(slice) |
| 99 | return int64(n), err |
| 100 | } |
| 101 | |
| 102 | // ImportWithOptions imports the nodes and edges from a serialized representation of |
| 103 | // another Graph into g. |