-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85bf692
commit 11e6e23
Showing
5 changed files
with
95 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package proto | ||
|
||
import ( | ||
"encoding/xml" | ||
"io" | ||
) | ||
|
||
type CodecXML[T any, K comparable] struct{} | ||
|
||
func (k CodecXML[T, K]) Marshal(w io.Writer, val Proto[T, K]) error { | ||
return xml.NewEncoder(w).Encode(val.Value()) | ||
} | ||
|
||
func (k CodecXML[T, K]) Unmarshal(r io.Reader, ptr Proto[T, K]) error { | ||
return xml.NewDecoder(r).Decode(ptr.Self()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package proto | ||
|
||
import "sync" | ||
|
||
type InstancePool[T any, K comparable] struct { | ||
pool sync.Pool | ||
} | ||
|
||
func (k *InstancePool[T, K]) Alloc() Proto[T, K] { | ||
return k.pool.Get().(Proto[T, K]) | ||
} | ||
|
||
func (k *InstancePool[T, K]) Free(proto Proto[T, K]) { | ||
// if Proto impl Resetter interface | ||
if free, ok := proto.(Resetter); ok { | ||
free.Reset() | ||
} | ||
k.pool.Put(proto) | ||
} | ||
|
||
func NewInstancePool[T any, K comparable](newProto func() Proto[T, K]) *InstancePool[T, K] { | ||
return &InstancePool[T, K]{ | ||
pool: sync.Pool{ | ||
New: func() any { return newProto() }, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters