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.
## Why this should be merged There are 3 places at which we perform the same, sensitive logic to access registered payloads and as we modify more types this is likely to expand. (e.g. `types.Header`). ## How this works Introduces `pseudo.Accessor` to abstract the reusable code. ## How this was tested Existing unit tests. Note that the `types.StateAccount` tests needed a minor refactor to provide the assertions with access to the `ExtraPayloads[T]` without introducing generic types anywhere.
- Loading branch information
Showing
10 changed files
with
135 additions
and
135 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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 pseudo | ||
|
||
// An Accessor provides access to T values held in other types. | ||
type Accessor[Container any, T any] struct { | ||
get func(Container) *Type | ||
set func(Container, *Type) | ||
} | ||
|
||
// NewAccessor constructs a new [Accessor]. The `get` function MUST return a | ||
// [Type] holding a T. | ||
func NewAccessor[C any, T any](get func(C) *Type, set func(C, *Type)) Accessor[C, T] { | ||
return Accessor[C, T]{get, set} | ||
} | ||
|
||
// Get returns the T held by the Container. | ||
func (a Accessor[C, T]) Get(from C) T { | ||
return MustNewValue[T](a.get(from)).Get() | ||
} | ||
|
||
// Get returns a pointer to the T held by the Container, which is guaranteed to | ||
// be non-nil. However, if T is itself a pointer, no guarantees are provided. | ||
// | ||
// Note that copying a Container might result in a shallow copy and that the *T | ||
// returned here will therefore be shared by all copies. If this is not the | ||
// desired behaviour, use [Accessor.Set]. | ||
func (a Accessor[C, T]) GetPointer(from C) *T { | ||
return MustPointerTo[T](a.get(from)).Value.Get() | ||
} | ||
|
||
// Set sets the T carried by the Container. | ||
func (a Accessor[C, T]) Set(on C, val T) { | ||
a.set(on, From(val).Type) | ||
} |
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
Oops, something went wrong.