forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: consolidate once-only registration of extras (#85)
## Why this should be merged Consolidates duplicated logic. Similar rationale to #84. ## How this works New `register.AtMostOnce[T]` type is responsible for limiting calls to `Register()`. ## How this was tested Existing unit tests of `params`. Note that the equivalent functionality in `types` wasn't tested but now is.
- Loading branch information
Showing
6 changed files
with
111 additions
and
48 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,68 @@ | ||
// Copyright 2024 the libevm authors. | ||
// | ||
// The libevm additions to go-ethereum are free software: you can redistribute | ||
// them and/or modify them under the terms of the GNU Lesser General Public License | ||
// as published by the Free Software Foundation, either version 3 of the License, | ||
// or (at your option) any later version. | ||
// | ||
// The libevm additions are distributed in the hope that they will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see | ||
// <http://www.gnu.org/licenses/>. | ||
|
||
// Package register provides functionality for optional registration of types. | ||
package register | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/ava-labs/libevm/libevm/testonly" | ||
) | ||
|
||
// An AtMostOnce allows zero or one registration of a T. | ||
type AtMostOnce[T any] struct { | ||
v *T | ||
} | ||
|
||
// ErrReRegistration is returned on all but the first of calls to | ||
// [AtMostOnce.Register]. | ||
var ErrReRegistration = errors.New("re-registration") | ||
|
||
// Register registers `v` or returns [ErrReRegistration] if already called. | ||
func (o *AtMostOnce[T]) Register(v T) error { | ||
if o.Registered() { | ||
return ErrReRegistration | ||
} | ||
o.v = &v | ||
return nil | ||
} | ||
|
||
// MustRegister is equivalent to [AtMostOnce.Register], panicking on error. | ||
func (o *AtMostOnce[T]) MustRegister(v T) { | ||
if err := o.Register(v); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Registered reports whether [AtMostOnce.Register] has been called. | ||
func (o *AtMostOnce[T]) Registered() bool { | ||
return o.v != nil | ||
} | ||
|
||
// Get returns the registered value. It MUST NOT be called before | ||
// [AtMostOnce.Register]. | ||
func (o *AtMostOnce[T]) Get() T { | ||
return *o.v | ||
} | ||
|
||
// TestOnlyClear clears any previously registered value, returning `o` to its | ||
// default state. It panics if called from a non-testing call stack. | ||
func (o *AtMostOnce[T]) TestOnlyClear() { | ||
testonly.OrPanic(func() { | ||
o.v = nil | ||
}) | ||
} |
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
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