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: attach_to_cart_mut and try_attach_to_cart_mut #6130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ddystopia
Copy link

This PR adds attach_to_cart_mut and try_attach_to_cart_mut constructors, to allow passing mutable references into Yokable. It composes well with Yoke::with_mut.

Note that StableDeref requires 1 multiple calls to deref, and deref_mut if implemented, to return the same address.

Footnotes

  1. https://docs.rs/stable_deref_trait/latest/stable_deref_trait/trait.StableDeref.html

@CLAassistant
Copy link

CLAassistant commented Feb 15, 2025

CLA assistant check
All committers have signed the CLA.

@Manishearth
Copy link
Member

I can't see any obvious problems with this API but it does make me nervous, especially from the POV of subtler unsafety rules: what is the use case for this API?

We already may go down the route of restricting what is allowed within a cart to solve #2095, which would potentially make the use cases of this API even less workable.

@sffc sffc removed their request for review February 16, 2025 19:48
@Ddystopia
Copy link
Author

My use case involves the (costly) creation of a view on a buffer, followed by mutations to that data. The with_mut method is already available, allowing unrestricted access to &mut references to the cart. I believe this can be achieved if the Deref implementation returns a type with interior mutability, enabling attach_to_cart to extract &mut. However, this approach introduces runtime overhead or requires unsafe code. I attempted using ghost_cell, but it requires a brand lifetime in yokable, whereas only a single lifetime is permitted.

@Manishearth
Copy link
Member

allowing unrestricted access to &mut references to the cart

It allows reasonably restricted access to mutating the cart via the yokeable, which is a bit different. But that is also one of the APIs that is tricky around #2095 when paired with the cart getter.

Interior mutability on the cart is partly where #2095 gets trickier, too.

Generally we're happy to wait on fixing #2095 until the Rust unsafe guidelines around this are fully solid and they have utility APIs that help us; but I'm also wary of adding more APIs that may need to be removed in the future.


Could you share the concrete code pattern you hoping to use, vs the code pattern you have to use without this API?

(Also, this API needs an example anyway, if you can generalize the code pattern a bit)

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

Successfully merging this pull request may close these issues.

3 participants