all: use Go 1.20 APIs

Closes #2746
This commit is contained in:
Hajime Hoshi 2024-09-12 00:20:49 +09:00
parent c346c1d75b
commit a36f6210c0
5 changed files with 11 additions and 21 deletions

View File

@ -188,14 +188,7 @@ func (c *Context) addPlayingPlayer(p *playerImpl) {
defer c.m.Unlock() defer c.m.Unlock()
c.playingPlayers[p] = struct{}{} c.playingPlayers[p] = struct{}{}
// (reflect.Type).Comparable() is enough here, as reflect.TypeOf should always return a dynamic (non-interface) type. if !reflect.ValueOf(p.sourceIdent()).Comparable() {
// If reflect.TypeOf returned an interface type, this check would be meaningless.
// See these for more details:
// * https://pkg.go.dev/reflect#TypeOf
// * https://pkg.go.dev/reflect#Type.Comparable
//
// (*reflect.Value).Comparable() is more intuitive but this was introduced in Go 1.20.
if !reflect.TypeOf(p.sourceIdent()).Comparable() {
return return
} }

View File

@ -69,15 +69,14 @@ func TestFloat32(t *testing.T) {
name = "seek" name = "seek"
} }
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
// Note that unsafe.SliceData is available as of Go 1.20.
var in, out []byte var in, out []byte
if len(c.In) > 0 { if len(c.In) > 0 {
outF32 := make([]float32, len(c.In)) outF32 := make([]float32, len(c.In))
for i := range c.In { for i := range c.In {
outF32[i] = float32(c.In[i]) / (1 << 15) outF32[i] = float32(c.In[i]) / (1 << 15)
} }
in = unsafe.Slice((*byte)(unsafe.Pointer(&c.In[0])), len(c.In)*2) in = unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(c.In))), len(c.In)*2)
out = unsafe.Slice((*byte)(unsafe.Pointer(&outF32[0])), len(outF32)*4) out = unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(outF32))), len(outF32)*4)
} }
r := convert.NewFloat32BytesReaderFromInt16BytesReader(bytes.NewReader(in)).(io.ReadSeeker) r := convert.NewFloat32BytesReaderFromInt16BytesReader(bytes.NewReader(in)).(io.ReadSeeker)
var got []byte var got []byte

View File

@ -616,6 +616,5 @@ func bytePtrToString(p *byte) string {
ptr = unsafe.Add(ptr, 1) ptr = unsafe.Add(ptr, 1)
} }
// unsafe.String(p, n) is available as of Go 1.20. return unsafe.String(p, n)
return string(unsafe.Slice(p, n))
} }

View File

@ -38,8 +38,7 @@ func (c *defaultContext) init() error {
return nil return nil
} }
// TODO: Use multiple %w-s as of Go 1.20 return fmt.Errorf("gl: failed to load: OpenGL.framework: %w, OpenGLES.framework: %w", errGL, errGLES)
return fmt.Errorf("gl: failed to load: OpenGL.framework: %v, OpenGLES.framework: %v", errGL, errGLES)
} }
func (c *defaultContext) getProcAddress(name string) (uintptr, error) { func (c *defaultContext) getProcAddress(name string) (uintptr, error) {

View File

@ -17,8 +17,8 @@
package gl package gl
import ( import (
"errors"
"fmt" "fmt"
"strings"
"github.com/ebitengine/purego" "github.com/ebitengine/purego"
) )
@ -29,8 +29,7 @@ var (
) )
func (c *defaultContext) init() error { func (c *defaultContext) init() error {
// TODO: Use multiple %w-s as of Go 1.20. var errs []error
var errors []string
// Try OpenGL ES first. Some machines like Android and Raspberry Pi might work only with OpenGL ES. // Try OpenGL ES first. Some machines like Android and Raspberry Pi might work only with OpenGL ES.
for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} { for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} {
@ -40,7 +39,7 @@ func (c *defaultContext) init() error {
c.isES = true c.isES = true
return nil return nil
} }
errors = append(errors, fmt.Sprintf("%s: %v", name, err)) errs = append(errs, fmt.Errorf("gl: Dlopen failed: name: %s: %w", name, err))
} }
// Try OpenGL next. // Try OpenGL next.
@ -54,10 +53,11 @@ func (c *defaultContext) init() error {
libGL = lib libGL = lib
return nil return nil
} }
errors = append(errors, fmt.Sprintf("%s: %v", name, err)) errs = append(errs, fmt.Errorf("gl: Dlopen failed: name: %s: %w", name, err))
} }
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: %s", strings.Join(errors, ", ")) errs = append([]error{fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: ")}, errs...)
return errors.Join(errs...)
} }
func (c *defaultContext) getProcAddress(name string) (uintptr, error) { func (c *defaultContext) getProcAddress(name string) (uintptr, error) {