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

Arbitrary Return Types for Register Modification Closures #874

Merged
merged 7 commits into from
Oct 24, 2024

Conversation

AdinAck
Copy link
Contributor

@AdinAck AdinAck commented Oct 22, 2024

Discussed in #859.

Added

  • write_and
  • write_with_zero_and
  • modify_and

These functions return a T. The writer cannot be returned outside of the closure because the lifetime is constrained to remain within the function body.

Motivation

My reasons for this change are to allow strong type-state validity:

let state = rb.reg().write_and(|w| State::set(w.field()));

Thanks @burrbull!

@AdinAck AdinAck requested a review from a team as a code owner October 22, 2024 15:46
@burrbull
Copy link
Member

These functions return a T

Return a generic T which corresponds to type returned by passed closure.

Just a note.

@burrbull
Copy link
Member

Instead of suffix it can be prefix.

What about external_write? ext_write

@AdinAck
Copy link
Contributor Author

AdinAck commented Oct 22, 2024

Hey great minds think alike, I was just thinking the same thing.

I was thinking from_write (but that's a bit close to From) and with_write.

@burrbull
Copy link
Member

from_write sounds good

@AdinAck
Copy link
Contributor Author

AdinAck commented Oct 22, 2024

Ok well I guess we can tentatively go with from_* then. Thanks!

@AdinAck
Copy link
Contributor Author

AdinAck commented Oct 22, 2024

On a side note, to reduce duplicate code, I think the original methods could invoke the new ones with the generic T filled in. Will look into it later.

@burrbull
Copy link
Member

cc @Emilgardis any comments?

@burrbull burrbull added this pull request to the merge queue Oct 24, 2024
Merged via the queue into rust-embedded:master with commit 89af369 Oct 24, 2024
46 checks passed
@Emilgardis
Copy link
Member

Emilgardis commented Oct 24, 2024

Would it make sense to put this behind a setting? I think not, but somethibg to maybe consider

@AdinAck
Copy link
Contributor Author

AdinAck commented Oct 24, 2024

Would it make sense to put this behind a setting? I think not, but somethibg to maybe consider

We discussed putting it behind a feature gate in the last meeting, but general consensus was against that idea.

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