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

Ref Wrapper API #6

Open
ibraheemdev opened this issue Jul 7, 2024 · 2 comments
Open

Ref Wrapper API #6

ibraheemdev opened this issue Jul 7, 2024 · 2 comments
Labels
feature New feature or request

Comments

@ibraheemdev
Copy link
Owner

It is sometimes useful to wrap a Guard and a &V into a single type instead of requiring a Guard as input. We could add a Ref wrapper type to support this. See the advanced lifetimes guide for context.

I'm unsure how the API should look as I don't want this to be the default return type for get. We could potentially add a Ref::create method that takes a guard and a closure that creates a value tied to the guard and extends the lifetime. However, we have to be careful about not creating self references. Though this is unlikely, it would be possible to create a HashMap<K, &Guard> or similar through Ref::create, so we might have to unfortunately restrict it to 'static types.

@ibraheemdev ibraheemdev added the feature New feature or request label Jul 7, 2024
@XAMPPRocky
Copy link

FWIW after having used papaya a little bit and encountering this need, I think the intuitive solution would be to add a HashMap::pin_ref method which returns a wrapper around HashMapRef which implements the same methods but returns the Ref type.

This has benefit of only adding one "new" method, and allowing the rest of the API surface to look the same e.g.

map.pin_ref().get("foo")
map.pin().get_ref("foo")
let pin = map.pin();
Ref::create(pin.get("foo"), pin)

@ibraheemdev
Copy link
Owner Author

Yeah that could work. I was envisioning an API something like map.create_ref(|m| m.get("foo")) to avoid duplicating the entire API on a wrapper type.

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

No branches or pull requests

2 participants