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

Mutable reference on owner in builder #59

Closed
gwenn opened this issue Aug 18, 2024 · 9 comments · Fixed by #60
Closed

Mutable reference on owner in builder #59

gwenn opened this issue Aug 18, 2024 · 9 comments · Fixed by #60

Comments

@gwenn
Copy link

gwenn commented Aug 18, 2024

Would it be possible to have &'a mut $Owner:

fn try_new<Err>(
    owner: $Owner,
    dependent_builder: impl for<'a> ::core::ops::FnOnce(&'a mut $Owner) -> Result<$Dependent<'a>, Err>
) -> Result<Self, Err>

instead of

fn try_new<Err>(
    owner: $Owner,
    dependent_builder: impl for<'a> ::core::ops::FnOnce(&'a $Owner) -> Result<$Dependent<'a>, Err>
) -> Result<Self, Err>

(use case: https://github.com/rusqlite/rusqlite/pull/1462/files#diff-c595ea0bc5c6bee5e8dfc649ad92279225e2e9a354b8f4d355fbaf4012ebe1e8R19)

@Voultapher
Copy link
Owner

Hi, this has been discussed here a couple of times, for example here #36. It's one of the main differences to ouroboros API wise, allowing it easily leads to UB, the available API would need to be more variable than it is now. Although now that I'm revisiting the topic I have a new idea how it could be tackled without making the API variable.

Voultapher added a commit that referenced this issue Sep 6, 2024
This has feature that has been requested multiple times by users,
#36 and
#59.
Voultapher added a commit that referenced this issue Sep 15, 2024
* Add support for mut ref dependents

This has feature that has been requested multiple times by users,
#36 and
#59.

* Fix accidental std usage.

* Model unlocked -> locked via unlocked by construction

This is less code, less run-time work and
conceptually harder to miss-use. Win-win.

* Remove unnecessary lock code and switch to AtomicBool
@Voultapher
Copy link
Owner

Voultapher commented Sep 15, 2024

@gwenn could you please try the main branch and let me know if the added MutBorrow works for your use-cases. This example should explain how to use it.

@Voultapher Voultapher reopened this Sep 15, 2024
@gwenn
Copy link
Author

gwenn commented Sep 29, 2024

Tested successfully:
rusqlite/rusqlite@93787c6
Thanks.

@Voultapher
Copy link
Owner

Cool!

I plan on doing another feature, after which I plan on releasing a new version that contains MutBorrow. Will close this issue once the new version is released.

@hniksic
Copy link

hniksic commented Dec 5, 2024

Thanks for implementing this feature - please consider uploading a release that contains it. It'd be nice to replace uses of ouroboros with self-cell, as self-cell generates much less code. But the switch is blocked by cases where the builder requires a mutable owner (and it doesn't make sense to use two self-referential crates in the same workspace).

@Voultapher
Copy link
Owner

Thanks for the reminder, I have to admit to myself that the idea will just not be ready soon so while it would have been nice to have a bigger 1.1 release, let's just release this.

@Voultapher
Copy link
Owner

Done https://github.com/Voultapher/self_cell/releases/tag/v1.1.0.

@Voultapher
Copy link
Owner

@gwenn I just released v1.1.0 with that version you should be able to port the rusqlite use cases.

@gwenn
Copy link
Author

gwenn commented Dec 5, 2024

@Voultapher
Wonderful !

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 a pull request may close this issue.

3 participants