Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e48ff5bb authored by Colin Cross's avatar Colin Cross
Browse files

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
parent 53d31263
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -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
// 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
	if v, ok := once.values.Load(key); ok {
		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
// key Get will panic.
func (once *OncePer) Get(key interface{}) interface{} {
func (once *OncePer) Get(key OnceKey) interface{} {
	v, ok := once.values.Load(key)
	if !ok {
		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
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)
}

// 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
	s := once.Once(key, func() interface{} {
		var s twoStringSlice