Browse Source

codec: SelfExt: move documentation to proper location

Ugorji Nwoke 6 years ago
parent
commit
fbe40e3ea7
1 changed files with 22 additions and 19 deletions
  1. 22 19
      codec/selfext.go

+ 22 - 19
codec/selfext.go

@@ -5,6 +5,22 @@ package codec
 
 
 import "sync"
 import "sync"
 
 
+// This extension expects that types registered with it implement SelfExt interface.
+//
+// This is used by libraries that support BytesExt e.g. cbor, json.
+var GlobalSelfInterfaceExt InterfaceExt = selfInterfaceExt{}
+
+// var selfExtEncPool = sync.Pool{
+// 	New: func() interface{} { return new(Encoder) },
+// }
+// var selfExtDecPool = sync.Pool{
+// 	New: func() interface{} { return new(Decoder) },
+// }
+
+// SelfExt is the interface that users implement so that their types
+// can, with minimal effort, be able to be an extension while allowing the
+// library handling the encoding/decoding needs easily.
+//
 // We now support the ability for an extension to define a tag,
 // We now support the ability for an extension to define a tag,
 // but allow itself to be encoded in a default way.
 // but allow itself to be encoded in a default way.
 //
 //
@@ -31,53 +47,40 @@ import "sync"
 //        // ... all t fields
 //        // ... all t fields
 //    }
 //    }
 //
 //
-// To use:
+// Usage model:
 //
 //
 //    cborHandle.SetInterfaceExt(reflect.TypeOf(T), 122, codec.GlobalSelfInterfaceExt)
 //    cborHandle.SetInterfaceExt(reflect.TypeOf(T), 122, codec.GlobalSelfInterfaceExt)
 //
 //
 //    msgpackHandle.SetBytesExt(reflect.TypeOf(T), 122, codec.NewSelfBytesExt(msgpackHandle, 1024))
 //    msgpackHandle.SetBytesExt(reflect.TypeOf(T), 122, codec.NewSelfBytesExt(msgpackHandle, 1024))
 //
 //
-// This extension expects that types registered with it implement SelfExt interface.
-var GlobalSelfInterfaceExt InterfaceExt = selfInterfaceExt{}
-
-// var selfExtEncPool = sync.Pool{
-// 	New: func() interface{} { return new(Encoder) },
-// }
-// var selfExtDecPool = sync.Pool{
-// 	New: func() interface{} { return new(Decoder) },
-// }
-
-// SelfExt is the interface that users implement so that their types
-// can, with minimal effort, be able to be an extension while allowing the
-// library handling the encoding/decoding needs easily.
 type SelfExt interface {
 type SelfExt interface {
 	CodecConvertExt() interface{}
 	CodecConvertExt() interface{}
 	CodecUpdateExt(interface{})
 	CodecUpdateExt(interface{})
 }
 }
 
 
 type selfBytesExt struct {
 type selfBytesExt struct {
+	// For performance and memory utilization, use sync.Pools
+	// They all have to be local to the Ext, as the Ext is bound to a Handle.
 	p sync.Pool // pool of byte buffers
 	p sync.Pool // pool of byte buffers
 	e sync.Pool
 	e sync.Pool
 	d sync.Pool
 	d sync.Pool
 	h Handle
 	h Handle
 	// bufcap int     // cap for each byte buffer created
 	// bufcap int     // cap for each byte buffer created
-	// inited uint32
-	// mu sync.Mutex
 }
 }
 
 
 type selfInterfaceExt struct{}
 type selfInterfaceExt struct{}
 
 
 // NewSelfBytesExt will return a BytesExt implementation,
 // NewSelfBytesExt will return a BytesExt implementation,
 // that will call an encoder to encode the value to a stream
 // that will call an encoder to encode the value to a stream
-// so it can be placed into the encoder stream.
+// so it can be placed into the encoder stream, and use a decoder
+// to do the same on the other end.
 //
 //
 // Users can specify a buffer size, and we will initialize that
 // Users can specify a buffer size, and we will initialize that
 // buffer for encoding the type. This allows users manage
 // buffer for encoding the type. This allows users manage
 // how big the buffer is based on their knowledge of the type being
 // how big the buffer is based on their knowledge of the type being
 // registered.
 // registered.
 //
 //
-// This extension expects that types registered with it implement
-// SelfExt interface.
+// This extension expects that types registered with it implement SelfExt interface.
 //
 //
 // This is used by libraries that support BytesExt e.g. msgpack, binc.
 // This is used by libraries that support BytesExt e.g. msgpack, binc.
 func NewSelfBytesExt(h Handle, bufcap int) *selfBytesExt {
 func NewSelfBytesExt(h Handle, bufcap int) *selfBytesExt {