Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT REQ] Add Utility Method to Convert xsync.MapOf to a Regular Go Map #149

Open
KiddoV opened this issue Nov 7, 2024 · 2 comments
Open
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@KiddoV
Copy link

KiddoV commented Nov 7, 2024

The xsync.MapOf is a concurrent-safe map, but for use cases where a regular map is needed (e.g., serialization or interactions with non-concurrent code), it would be very convenient to have a built-in method for this conversion.
This would improve usability and reduce boilerplate code in user applications.

Currently, to convert an xsync.MapOf[K, V] to a regular Go map (map[K]V), I would have to manually iterate over the xsync.MapOf using the Range method, like so:

func xSyncMapToMap[K comparable, V any](x *xsync.MapOf[K, V]) map[K]V {
	goMap := make(map[K]V)
	//
	x.Range(func(key K, value V) bool {
		goMap[key] = value
		return true //Continue iteration
	})
	return goMap
}

It would be helpful if the library could provide a utility method that simplifies this process, such as:

func (m *MapOf[K, V]) ToMap() map[K]V

Is this safe to do? What do you think?

@puzpuzpuz puzpuzpuz added the enhancement New feature or request label Nov 7, 2024
@puzpuzpuz
Copy link
Owner

Thanks for bringing up the idea. It makes sense to me an enhancement.

@puzpuzpuz puzpuzpuz added the good first issue Good for newcomers label Nov 7, 2024
@pippellia-btc
Copy link

+1 on this. It would also help me in my unit tests, where I want to compare a result MapOf with an expected MapOf. If I can convert to regular maps, I can then use reflect.DeepCompare, without the need of a specific method .Equal()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants