Skip to content

Implement DerefMut for Once fixtures, to allow mutable access to non-Clone fixture resources#55

Merged
mgautierfr merged 2 commits into
mgautierfr:mainfrom
dantliff-sqc:impl-deref-mut-for-once-fixtures
Apr 29, 2026
Merged

Implement DerefMut for Once fixtures, to allow mutable access to non-Clone fixture resources#55
mgautierfr merged 2 commits into
mgautierfr:mainfrom
dantliff-sqc:impl-deref-mut-for-once-fixtures

Conversation

@dantliff-sqc
Copy link
Copy Markdown
Contributor

Typically, if a test needs mutable access to a fixture, the test can clone the fixture value and rebind it as mutable:

#[test]
fn test_something(my_fixture: MyFixture) {
    let mut my_fixture = (*my_fixture).clone();
    my_fixture.set_something(42);
    // ...

This is necessary when rustest is used in an embedded application, where access to hardware may require mutable access to a resource held by the fixture (e.g. an I2C bus).

Unfortunately, mostly due to HAL restrictions, sometimes a fixture isn't trivially Clone, so this is not possible without additional interior mutability wrappers around the resources. This is significant additional complexity just to provide test ability.

This PR introduces a simple implementation of DerefMut for Once fixtures, avoiding a .clone() and allowing this:

#[test]
fn test_something(mut my_fixture: MyFixture) {
    my_fixture.set_something(42);
    // ...

It might make some sense for shared or global fixtures to be DerefMut too, but the solution is more complex, and will involve locking. So only Once fixtures have this trait implemented by the #[fixture] macro in this PR.

… a fixture as 'mut' without having to clone.
Copy link
Copy Markdown
Owner

@mgautierfr mgautierfr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice. Thanks for the PR.

Maybe few improvement of the tests but else I agree with the feature.
Can you also update the documentation (in rustest/src/lib.rs and Readme) ?

Comment thread rustest/tests/test.rs Outdated
Comment thread rustest/tests/test.rs
@dantliff-sqc
Copy link
Copy Markdown
Contributor Author

Will update docs also

@dantliff-sqc dantliff-sqc force-pushed the impl-deref-mut-for-once-fixtures branch from 9c91fa7 to ec66c34 Compare March 4, 2026 22:05
@dantliff-sqc
Copy link
Copy Markdown
Contributor Author

Sorry, not sure what status report is awaited...

@mgautierfr mgautierfr self-requested a review April 29, 2026 10:22
@mgautierfr
Copy link
Copy Markdown
Owner

Sorry, not sure what status report is awaited...

Sorry, I have somehow missed that you update the PR and forget about it.
I will be more attentive next time.

@mgautierfr mgautierfr marked this pull request as draft April 29, 2026 10:35
@mgautierfr mgautierfr marked this pull request as ready for review April 29, 2026 10:35
@mgautierfr
Copy link
Copy Markdown
Owner

not sure what status report is awaited...

For some reason, github has not triggered a workflow run on your second commit and so never see the status reported.

The change of the second commit is small enough to merge without the CI passing.

Thanks for the PR.

@mgautierfr mgautierfr merged commit c73f83f into mgautierfr:main Apr 29, 2026
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.

2 participants