interface{} -> any

Because I’m on Go 1.18 now.
This commit is contained in:
Josh Deprez 2022-03-17 03:42:08 +00:00
parent ca23e0e06b
commit b4f7753a00
13 changed files with 70 additions and 70 deletions

View file

@ -63,7 +63,7 @@ func (a *Actor) CollidesAt(p geom.Int3) bool {
log.Printf("collision domain %q not found", a.CollisionDomain)
return false
}
return errCollision == a.game.Query(cd, ColliderType, nil, func(c interface{}) error {
return errCollision == a.game.Query(cd, ColliderType, nil, func(c any) error {
if cl, ok := c.(Collider); ok && cl.CollidesWith(bounds) {
return errCollision
}

View file

@ -30,7 +30,7 @@ type assetKey struct {
}
// LoadGobz gunzips and gob-decodes a component from a file from a FS.
func LoadGobz(dst interface{}, assets fs.FS, path string) error {
func LoadGobz(dst any, assets fs.FS, path string) error {
f, err := assets.Open(path)
if err != nil {
return err
@ -45,7 +45,7 @@ func LoadGobz(dst interface{}, assets fs.FS, path string) error {
// SaveGobz takes an object, gob-encodes it, gzips it, and writes to disk.
// This requires running on something with a disk to write to (not JS)
func SaveGobz(src interface{}, name string) error {
func SaveGobz(src any, name string) error {
f, err := os.CreateTemp(".", filepath.Base(name))
if err != nil {
return err

View file

@ -39,7 +39,7 @@ func init() {
// Camera models a camera that is viewing something.
type Camera struct {
ID
Child interface{}
Child any
Disables
Hides

View file

@ -33,15 +33,15 @@ func init() {
}
// Container is a component that contains many other components, in order.
// It can be used as both a component in its own right, or as a ordered set.
// It can be used as both a component in its own right, or as an ordered set.
// A nil *Container contains no items and modifications will panic (like a map).
type Container struct {
items []interface{}
reverse map[interface{}]int
items []any
reverse map[any]int
}
// MakeContainer puts the items into a new Container.
func MakeContainer(items ...interface{}) *Container {
func MakeContainer(items ...any) *Container {
c := &Container{items: items}
c.rebuildReverse()
return c
@ -51,7 +51,7 @@ func (c *Container) rebuildReverse() {
if c == nil {
return
}
c.reverse = make(map[interface{}]int, len(c.items))
c.reverse = make(map[any]int, len(c.items))
for i, x := range c.items {
c.reverse[x] = i
}
@ -100,7 +100,7 @@ func (c *Container) Scan(visit VisitFunc) error {
// Add adds an item to the end of the container, if not already present.
// Adding nil, or a component already present in the container, does nothing.
// Add is _not_ safe to call on a nil *Container.
func (c *Container) Add(component interface{}) {
func (c *Container) Add(component any) {
if component == nil || c.Contains(component) {
return
}
@ -112,7 +112,7 @@ func (c *Container) Add(component interface{}) {
// half the slice, the slice is compacted (indexes of items will change).
// Removing an item not in the Container does nothing.
// Remove is safe to call on a nil *Container.
func (c *Container) Remove(component interface{}) {
func (c *Container) Remove(component any) {
if c == nil {
return
}
@ -129,7 +129,7 @@ func (c *Container) Remove(component interface{}) {
// Contains reports if an item exists in the container.
// Contains is safe to call on a nil *Container.
func (c *Container) Contains(component interface{}) bool {
func (c *Container) Contains(component any) bool {
if c == nil {
return false
}
@ -140,7 +140,7 @@ func (c *Container) Contains(component interface{}) bool {
// IndexOf reports if an item exists in the container and returns the index if
// present.
// IndexOf is safe to call on a nil *Container.
func (c *Container) IndexOf(component interface{}) (int, bool) {
func (c *Container) IndexOf(component any) (int, bool) {
if c == nil {
return 0, false
}
@ -159,7 +159,7 @@ func (c *Container) ItemCount() int {
// Element returns the item at index i, or nil for a free slot.
// Element is _not_ safe to call on a nil *Container.
func (c *Container) Element(i int) interface{} { return c.items[i] }
func (c *Container) Element(i int) any { return c.items[i] }
// Len returns the number of items plus the number of nil slots in the container.
// Len is safe to call on a nil *Container.

View file

@ -24,10 +24,10 @@ import (
func TestMakeContainer(t *testing.T) {
c := MakeContainer(69, 420)
if want := []interface{}{69, 420}; !cmp.Equal(c.items, want) {
if want := []any{69, 420}; !cmp.Equal(c.items, want) {
t.Errorf("c.items = %v, want %v", c.items, want)
}
if want := map[interface{}]int{69: 0, 420: 1}; !cmp.Equal(c.reverse, want) {
if want := map[any]int{69: 0, 420: 1}; !cmp.Equal(c.reverse, want) {
t.Errorf("c.reverse = %v, want %v", c.reverse, want)
}
}

View file

@ -48,7 +48,7 @@ func init() {
// the number of tests between components.
type DrawDAG struct {
ChunkSize int
Child interface{}
Child any
Disables
Hides
@ -71,7 +71,7 @@ func (d *DrawDAG) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
hidden bool
opts ebiten.DrawImageOptions
}
cache := map[interface{}]state{
cache := map[any]state{
d: {
hidden: false,
opts: *opts,
@ -86,7 +86,7 @@ func (d *DrawDAG) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
}
// Walk up game tree to find the nearest state in cache.
var st state
stack := []interface{}{x}
stack := []any{x}
for p := d.game.Parent(x); p != nil; p = d.game.Parent(p) {
if s, found := cache[p]; found {
st = s
@ -168,8 +168,8 @@ func (d *DrawDAG) Update() error {
// Register recursively registers compponent and all descendants that are
// DrawBoxers into internal data structures (the DAG, etc) unless they are
// descendants of a different DrawManager.
func (d *DrawDAG) Register(component, _ interface{}) error {
return d.game.Query(component, DrawBoxerType, func(c interface{}) error {
func (d *DrawDAG) Register(component, _ any) error {
return d.game.Query(component, DrawBoxerType, func(c any) error {
if db, ok := c.(DrawBoxer); ok {
d.registerOne(db)
}
@ -235,8 +235,8 @@ func (d *DrawDAG) registerOne(x DrawBoxer) {
}
// Unregister unregisters the component and all subcomponents.
func (d *DrawDAG) Unregister(component interface{}) {
d.game.Query(component, DrawBoxerType, func(c interface{}) error {
func (d *DrawDAG) Unregister(component any) {
d.game.Query(component, DrawBoxerType, func(c any) error {
if db, ok := c.(DrawBoxer); ok {
d.unregisterOne(db)
}

View file

@ -37,7 +37,7 @@ func init() {
// through the game tree using Query, without any extra sorting based on Z
// values or consideration for DrawOrderer.
type DrawDFS struct {
Child interface{}
Child any
Hides
game *Game
@ -49,7 +49,7 @@ func (d *DrawDFS) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
stack := []ebiten.DrawImageOptions{*opts}
d.game.Query(d, DrawerType,
// visitPre
func(x interface{}) error {
func(x any) error {
if h, ok := x.(Hider); ok && h.Hidden() {
return Skip
}
@ -70,7 +70,7 @@ func (d *DrawDFS) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
return nil
},
// visitPost
func(x interface{}) error {
func(x any) error {
if _, ok := x.(Transformer); ok {
stack = stack[:len(stack)-1]
}

View file

@ -60,10 +60,10 @@ type Game struct {
VoxelScale geom.Float3
dbmu sync.RWMutex
byID map[string]Identifier // Named components by ID
byAB map[abKey]*Container // paths matching interface
parent map[interface{}]interface{} // parent[x] is parent of x
children map[interface{}]*Container // children[x] are chilren of x
byID map[string]Identifier // Named components by ID
byAB map[abKey]*Container // paths matching interface
parent map[any]any // parent[x] is parent of x
children map[any]*Container // children[x] are children of x
}
// Draw draws everything.
@ -84,14 +84,14 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
// not updated.
func (g *Game) Update() error {
return g.Query(g.Root, UpdaterType,
func(c interface{}) error {
func(c any) error {
if d, ok := c.(Disabler); ok && d.Disabled() {
// Do not update this component or descendants.
return Skip
}
return nil
},
func(c interface{}) error {
func(c any) error {
if u, ok := c.(Updater); ok {
return u.Update()
}
@ -114,7 +114,7 @@ func (g *Game) Component(id string) Identifier {
// Parent returns the parent of a given component, or nil if there is none.
// This only returns sensible values for registered components.
func (g *Game) Parent(c interface{}) interface{} {
func (g *Game) Parent(c any) any {
g.dbmu.RLock()
defer g.dbmu.RUnlock()
return g.parent[c]
@ -122,7 +122,7 @@ func (g *Game) Parent(c interface{}) interface{} {
// Children returns the direct subcomponents of the given component, or nil if
// there are none. This only returns sensible values for registered components.
func (g *Game) Children(c interface{}) *Container {
func (g *Game) Children(c any) *Container[any] {
g.dbmu.RLock()
defer g.dbmu.RUnlock()
return g.children[c]
@ -130,7 +130,7 @@ func (g *Game) Children(c interface{}) *Container {
// PathRegister calls Register on every Registrar in the path between g and
// parent (top-to-bottom, i.e. game first, component last).
func (g *Game) PathRegister(component, parent interface{}) error {
func (g *Game) PathRegister(component, parent any) error {
for _, p := range g.Path(parent) {
if r, ok := p.(Registrar); ok {
if err := r.Register(component, parent); err != nil {
@ -143,7 +143,7 @@ func (g *Game) PathRegister(component, parent interface{}) error {
// PathUnregister calls Unregister on every Registrar in the path between g and
// parent (bottom-to-top, i.e. component first, game last).
func (g *Game) PathUnregister(component interface{}) {
func (g *Game) PathUnregister(component any) {
for _, p := range g.ReversePath(component) {
if r, ok := p.(Registrar); ok {
r.Unregister(component)
@ -153,7 +153,7 @@ func (g *Game) PathUnregister(component interface{}) {
// Path returns a slice with the path of components to reach component from g
// (including g and component).
func (g *Game) Path(component interface{}) []interface{} {
func (g *Game) Path(component any) []any {
stack := g.ReversePath(component)
for i, j := 0, len(stack)-1; i < j; i, j = i+1, j-1 {
stack[i], stack[j] = stack[j], stack[i]
@ -163,8 +163,8 @@ func (g *Game) Path(component interface{}) []interface{} {
// ReversePath returns the same slice as Path, but reversed. (ReversePath is
// faster than Path).
func (g *Game) ReversePath(component interface{}) []interface{} {
var stack []interface{}
func (g *Game) ReversePath(component any) []any {
var stack []any
g.dbmu.RLock()
for p := component; p != nil; p = g.parent[p] {
stack = append(stack, p)
@ -194,7 +194,7 @@ func (g *Game) ReversePath(component interface{}) []interface{} {
// Skip when it is returned from a recursive call. Returning Skip from visitPre
// will cause visitPost and the descendants of the component to be skipped (see
// the implementation of Update for an example of how to use this).
func (g *Game) Query(ancestor interface{}, behaviour reflect.Type, visitPre, visitPost VisitFunc) error {
func (g *Game) Query(ancestor any, behaviour reflect.Type, visitPre, visitPost VisitFunc) error {
if visitPre != nil {
if err := visitPre(ancestor); err != nil {
return err
@ -208,7 +208,7 @@ func (g *Game) Query(ancestor interface{}, behaviour reflect.Type, visitPre, vis
g.dbmu.RLock()
q := g.byAB[abKey{ancestor, behaviour}]
g.dbmu.RUnlock()
if err := q.Scan(func(x interface{}) error {
if err := q.Scan(func(x any) error {
if err := g.Query(x, behaviour, visitPre, visitPost); err != nil {
if errors.Is(err, Skip) {
return nil
@ -232,7 +232,7 @@ func (g *Game) Scan(visit VisitFunc) error {
// Load loads a component and all subcomponents recursively.
// Note that this method does not implement Loader itself.
func (g *Game) Load(component interface{}, assets fs.FS) error {
func (g *Game) Load(component any, assets fs.FS) error {
// Query cannot be used for this method because Load might cause
// subcomponents to spring into existence.
if l, ok := component.(Loader); ok {
@ -241,7 +241,7 @@ func (g *Game) Load(component interface{}, assets fs.FS) error {
}
}
if sc, ok := component.(Scanner); ok {
return sc.Scan(func(x interface{}) error {
return sc.Scan(func(x any) error {
return g.Load(x, assets)
})
}
@ -250,10 +250,10 @@ func (g *Game) Load(component interface{}, assets fs.FS) error {
// Prepare prepares a component and all subcomponents recursively.
// Note that this method does not implement Prepper itself.
func (g *Game) Prepare(component interface{}) error {
func (g *Game) Prepare(component any) error {
// Postorder traversal, in case ancestors depend on descendants being
// ready to answer queries.
return g.Query(component, PrepperType, nil, func(c interface{}) error {
return g.Query(component, PrepperType, nil, func(c any) error {
if p, ok := c.(Prepper); ok {
return p.Prepare(g)
}
@ -300,8 +300,8 @@ func (g *Game) build() error {
defer g.dbmu.Unlock()
g.byID = make(map[string]Identifier)
g.byAB = make(map[abKey]*Container)
g.parent = make(map[interface{}]interface{})
g.children = make(map[interface{}]*Container)
g.parent = make(map[any]any)
g.children = make(map[any]*Container)
return g.registerRecursive(g, nil)
}
@ -310,7 +310,7 @@ func (g *Game) build() error {
// Registering multiple components with the same ID is also an error.
// Registering a component will recursively register all children found via
// Scan.
func (g *Game) Register(component, parent interface{}) error {
func (g *Game) Register(component, parent any) error {
if component == nil {
return errNilComponent
}
@ -322,19 +322,19 @@ func (g *Game) Register(component, parent interface{}) error {
return g.registerRecursive(component, parent)
}
func (g *Game) registerRecursive(component, parent interface{}) error {
func (g *Game) registerRecursive(component, parent any) error {
if err := g.registerOne(component, parent); err != nil {
return err
}
if sc, ok := component.(Scanner); ok {
return sc.Scan(func(x interface{}) error {
return sc.Scan(func(x any) error {
return g.registerRecursive(x, component)
})
}
return nil
}
func (g *Game) registerOne(component, parent interface{}) error {
func (g *Game) registerOne(component, parent any) error {
// register in g.byID if needed
if i, ok := component.(Identifier); ok {
if id := i.Ident(); id != "" {
@ -377,7 +377,7 @@ func (g *Game) registerOne(component, parent interface{}) error {
// Unregister removes the component from the component database.
// Passing a nil component has no effect. Unregistering a component will
// recursively unregister child components found via Scan.
func (g *Game) Unregister(component interface{}) {
func (g *Game) Unregister(component any) {
if component == nil {
return
}
@ -386,15 +386,15 @@ func (g *Game) Unregister(component interface{}) {
g.dbmu.Unlock()
}
func (g *Game) unregisterRecursive(component interface{}) {
g.children[component].Scan(func(x interface{}) error {
func (g *Game) unregisterRecursive(component any) {
g.children[component].Scan(func(x any) error {
g.unregisterRecursive(x)
return nil
})
g.unregisterOne(component)
}
func (g *Game) unregisterOne(component interface{}) {
func (g *Game) unregisterOne(component any) {
parent := g.parent[component]
// unregister from g.byAB
@ -428,7 +428,7 @@ func (g *Game) String() string { return "Game" }
// abKey is the key type for game.byAB.
type abKey struct {
parent interface{}
parent any
behaviour reflect.Type
}
@ -453,10 +453,10 @@ func concatOpts(a, b ebiten.DrawImageOptions) ebiten.DrawImageOptions {
// For example, Query takes two VisitFuncs that are called for each result, and
// Scan is given a VisitFunc that should be called with each component. For
// recursive operations, return Skip for components that should be skipped.
type VisitFunc func(interface{}) error
type VisitFunc func(any) error
// Many calls a VisitFunc for multiple args, and returns on first non-nil error.
func (v VisitFunc) Many(x ...interface{}) error {
func (v VisitFunc) Many(x ...any) error {
for _, c := range x {
if err := v(c); err != nil {
return err

View file

@ -150,8 +150,8 @@ type Prepper interface {
// into internal data structures). Registrars are expected to automatically
// register/unregister subcomponents of components (usually recursively).
type Registrar interface {
Register(component, parent interface{}) error
Unregister(component interface{})
Register(component, parent any) error
Unregister(component any)
}
// Saver components can be saved to disk.

View file

@ -39,7 +39,7 @@ func init() {
type Parallax struct {
CameraID string
Factor float64 // how much to translate in response to the camera
Child interface{}
Child any
camera *Camera
}

View file

@ -95,7 +95,7 @@ func (g *Game) cmdTree(dst io.Writer, argv []string) {
fmt.Println(dst, "Usage: tree [ID]")
return
}
c := interface{}(g)
c := any(g)
if len(argv) == 2 { // subtree
id := argv[1]
c = g.Component(id)
@ -107,7 +107,7 @@ func (g *Game) cmdTree(dst io.Writer, argv []string) {
g.printTreeRecursive(dst, 0, c)
}
func (g *Game) printTreeRecursive(dst io.Writer, depth int, c interface{}) {
func (g *Game) printTreeRecursive(dst io.Writer, depth int, c any) {
indent := ""
if depth > 0 {
indent = strings.Repeat(" ", depth-1) + "↳ "
@ -118,7 +118,7 @@ func (g *Game) printTreeRecursive(dst io.Writer, depth int, c interface{}) {
} else {
fmt.Fprintf(dst, "%s%v\n", indent, c)
}
g.Children(c).Scan(func(x interface{}) error {
g.Children(c).Scan(func(x any) error {
g.printTreeRecursive(dst, depth+1, x)
return nil
})
@ -145,7 +145,7 @@ func (g *Game) cmdQuery(dst io.Writer, argv []string) {
return
}
var ancestor interface{} = g
var ancestor any = g
if len(argv) == 3 {
c := g.Component(argv[2])
if c == nil {
@ -156,7 +156,7 @@ func (g *Game) cmdQuery(dst io.Writer, argv []string) {
}
noResults := true
g.Query(ancestor, behaviour, func(c interface{}) error {
g.Query(ancestor, behaviour, func(c any) error {
if !reflect.TypeOf(c).Implements(behaviour) {
return nil
}
@ -174,7 +174,7 @@ func (g *Game) cmdQuery(dst io.Writer, argv []string) {
}
}
func (g *Game) cmdutilComponentArg1(dst io.Writer, argv []string) interface{} {
func (g *Game) cmdutilComponentArg1(dst io.Writer, argv []string) any {
if len(argv) != 2 {
fmt.Fprintln(dst, "Usage: hide ID")
return nil

View file

@ -49,7 +49,7 @@ func init() {
type Scene struct {
ID
Bounds // world coordinates
Child interface{}
Child any
Disables
Hides
}
@ -66,7 +66,7 @@ func (s *Scene) String() string { return "Scene" }
// This is mostly useful for scenes that refer to other scenes, e.g.
//
// sc := &Scene{
// Components: []interface{}{
// Components: []any{
// &SceneRef{Path: "assets/foo.gob.gz"} // inflated at Load time
// },
// }

View file

@ -64,7 +64,7 @@ func main() {
ebiten.SetWindowTitle("TODO")
// Change to true to rewrite level1.gobz
lev1 := interface{}(&engine.SceneRef{Path: "assets/level1.gobz"})
lev1 := any(&engine.SceneRef{Path: "assets/level1.gobz"})
if hardcodedLevel1 {
lev1 = example.Level1()
if rewriteLevel1 && runtime.GOOS != "js" {