puzpuzpuz/xsync

Nesting multiple xsync.MapOf maps not supported, or...?

grongor opened this issue · 2 comments

Hello! I've been trying out the xsync.MapOf on one of my projects and I ran into a strange issue when nesting the maps. Am I doing something wrong, or did I just discover a bug? Here is a runnable example (using latest xsync version):

package main

import (
	"github.com/puzpuzpuz/xsync"
)

type tmp struct {
	Hello string
}

func main() {
	const stringKey = "test"
	const intKey = 123

	outerStringMap := xsync.NewMapOf[*xsync.MapOf[uint32, *tmp]]()

	innerIntMap, loaded := outerStringMap.LoadOrCompute(stringKey, func() *xsync.MapOf[uint32, *tmp] {
		return xsync.NewIntegerMapOf[uint32, *tmp]()
	})
	if loaded {
		panic("expected nothing")
	}

	innerIntMap.Store(intKey, &tmp{Hello: "world"})

	innerIntMap, loaded = outerStringMap.Load(stringKey)
	if !loaded {
		panic("expected existing map")
	}

	hello, loaded := innerIntMap.Load(intKey)
	if !loaded {
		panic("expected existing value")
	}

	if hello.Hello != "world" {
		panic("unexpected value")
	}
}
grongor@grongor-nb:~/xsync-test$ go run .
panic: expected existing value

goroutine 1 [running]:
main.main()
        /home/grongor/xsync-test/main.go:33 +0x112
exit status 2

image
image

Hi @grongor

You should import github.com/puzpuzpuz/xsync/v2, not github.com/puzpuzpuz/xsync. Other than that, I've tried your snippet on go 1.19.3 and saw no panic. What's your go version?

OMG I hate this versioning system so much :D Yeah, that was it...thank you very much for the quick response! Sorry for wasting your time :)

Great lib btw!