Change type of Once keys to OnceKey
Require that the key for Once calls be created with NewOnceKey or NewCustomOnceKey. Test: m checkbuild Change-Id: I44b8ea6034338b45f558f862b21d5732364cbf0f
This commit is contained in:
@@ -26,7 +26,7 @@ type OncePer struct {
|
|||||||
|
|
||||||
// Once computes a value the first time it is called with a given key per OncePer, and returns the
|
// Once computes a value the first time it is called with a given key per OncePer, and returns the
|
||||||
// value without recomputing when called with the same key. key must be hashable.
|
// value without recomputing when called with the same key. key must be hashable.
|
||||||
func (once *OncePer) Once(key interface{}, value func() interface{}) interface{} {
|
func (once *OncePer) Once(key OnceKey, value func() interface{}) interface{} {
|
||||||
// Fast path: check if the key is already in the map
|
// Fast path: check if the key is already in the map
|
||||||
if v, ok := once.values.Load(key); ok {
|
if v, ok := once.values.Load(key); ok {
|
||||||
return v
|
return v
|
||||||
@@ -50,7 +50,7 @@ func (once *OncePer) Once(key interface{}, value func() interface{}) interface{}
|
|||||||
|
|
||||||
// Get returns the value previously computed with Once for a given key. If Once has not been called for the given
|
// Get returns the value previously computed with Once for a given key. If Once has not been called for the given
|
||||||
// key Get will panic.
|
// key Get will panic.
|
||||||
func (once *OncePer) Get(key interface{}) interface{} {
|
func (once *OncePer) Get(key OnceKey) interface{} {
|
||||||
v, ok := once.values.Load(key)
|
v, ok := once.values.Load(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Errorf("Get() called before Once()"))
|
panic(fmt.Errorf("Get() called before Once()"))
|
||||||
@@ -60,12 +60,12 @@ func (once *OncePer) Get(key interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OnceStringSlice is the same as Once, but returns the value cast to a []string
|
// OnceStringSlice is the same as Once, but returns the value cast to a []string
|
||||||
func (once *OncePer) OnceStringSlice(key interface{}, value func() []string) []string {
|
func (once *OncePer) OnceStringSlice(key OnceKey, value func() []string) []string {
|
||||||
return once.Once(key, func() interface{} { return value() }).([]string)
|
return once.Once(key, func() interface{} { return value() }).([]string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnceStringSlice is the same as Once, but returns two values cast to []string
|
// OnceStringSlice is the same as Once, but returns two values cast to []string
|
||||||
func (once *OncePer) Once2StringSlice(key interface{}, value func() ([]string, []string)) ([]string, []string) {
|
func (once *OncePer) Once2StringSlice(key OnceKey, value func() ([]string, []string)) ([]string, []string) {
|
||||||
type twoStringSlice [2][]string
|
type twoStringSlice [2][]string
|
||||||
s := once.Once(key, func() interface{} {
|
s := once.Once(key, func() interface{} {
|
||||||
var s twoStringSlice
|
var s twoStringSlice
|
||||||
|
Reference in New Issue
Block a user