Skip to content

make 'fn ptr()' APIs to be 'const fn ptr()' #235

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

Merged
merged 6 commits into from
Jul 28, 2020
Merged

make 'fn ptr()' APIs to be 'const fn ptr()' #235

merged 6 commits into from
Jul 28, 2020

Conversation

JOE1994
Copy link
Contributor

@JOE1994 JOE1994 commented Jul 5, 2020

This PR changes functions like ITM::ptr(), DWT::ptr() to become const fns.

  • functions like ITM::ptr(), DWT::ptr() return pointers that are cast from constants,
    but currently these functions can't be used to define a constant. This PR will allow below code to compile.
use cortex_m::peripheral::ITM;
use cortex_m::peripheral::itm::RegisterBlock;
// Below line currently won't compile, since `ITM::ptr()` is not a `const fn`
const ITM_PTR: *mut RegisterBlock = ITM::ptr();

I couldn't think of disadvantages that might accompany this change, but please correct me if I'm wrong.
Thank you for reviewing this PR 👍

@JOE1994 JOE1994 requested a review from a team as a code owner July 5, 2020 03:30
@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @therealprof (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cortex-m labels Jul 5, 2020
@jonas-schievink
Copy link
Contributor

What's the use case for something like this?

If we do this, we probably also want to change svd2rust accordingly, since it generates similar APIs.

(aside: is there any reason why ptr is not an associated constant instead?)

@JOE1994
Copy link
Contributor Author

JOE1994 commented Jul 5, 2020

What's the use case for something like this?

To be honest, I don't have a solid use case for this. My idea was just "Why not make these const fns?".
Now I realized that I rather should've opened an issue first, rather than a PR. I apologize for this spammish PR 😢

If we do this, we probably also want to change svd2rust accordingly, since it generates similar APIs.

(aside: is there any reason why ptr is not an associated constant instead?)

Making ptr to be associated constants seems to be a much better option than making these functions to be const fn 😃
(although it would be a breaking change).
It would make the code more neat (in my personal opinion), and slightly reduce compile time.

This PR is rather a minor refactoring attempt that doesn't solve practical issues.
I could close this PR and open an issue for discussion instead.

@therealprof
Copy link
Contributor

I think we should generally try to be more idiomatic and expressive even without particular usecase or problem to address. Declaring constant functions as const makes a lot of sense from an expressive point of view, enables new uses and could even today lead to potentially better code generation in dev mode. So I'm all for such changes, especially if they don't require a MSRV bump.

@jonas-schievink
Copy link
Contributor

could even today lead to potentially better code generation in dev mode

Not sure why that would be the case here, unless we fully move to an associated constant.

@therealprof
Copy link
Contributor

Not sure why that would be the case here, unless we fully move to an associated constant.

There're plenty of cases where rustc decides to use a function call instead of simply inlining the code. My understanding is that const fn are/will be evaluated during compile time in more cases and are thus less likely to end up as function calls. Seems like non-breaking low hanging fruit to me, independent of associated consts.

@Disasm
Copy link
Member

Disasm commented Jul 5, 2020

Yet another use case (for svd2rust generated code, though):
https://github.com/stm32-rs/stm32f4xx-hal/blob/44f2f01e711981e89d031b4331b541e07643094d/src/otg_fs.rs#L29

@jonas-schievink
Copy link
Contributor

My understanding is that const fn are/will be evaluated during compile time in more cases and are thus less likely to end up as function calls.

No, not at the moment. They're only evaluated at compile time if you initialize a const or static with them or use them in an array's length. rustc will not inline anything that isn't #[inline(always)] in debug mode, if people want inlining they should enable optimizations.

@Disasm's use case looks interesting. I think it makes sense to move svd2rust over to associated constants as well as this crate (perhaps with a migration period in which we provide both).

@therealprof therealprof added the nominated Issue nominated as discussion topic for the Embedded WG meeting label Jul 20, 2020
@therealprof
Copy link
Contributor

therealprof commented Jul 21, 2020

As discussed in the meeting having const fn ptr() is a nice thing to have and we'll be happy to take it for now. Longer term we're going to move to associated consts and will deprecate the free ptr functions.

@adamgreig
Copy link
Member

I think the idea was to add the associated constants now too, since they can be a non-breaking addition and are more generally useful, but then move to deprecating the ptr methods in the next breaking release (0.7).

@therealprof
Copy link
Contributor

@adamgreig I shouldn't have edited the second sentence in so sloppily. 😅 Of course I meant to say we're adding in the associated const right away.

@JOE1994
Copy link
Contributor Author

JOE1994 commented Jul 23, 2020

Should I add another commit to add in associated constants also??

@therealprof
Copy link
Contributor

@JOE1994 Yes, please.

@JOE1994
Copy link
Contributor Author

JOE1994 commented Jul 27, 2020

I think this PR is ready to be reviewed 😺

@Disasm
Copy link
Member

Disasm commented Jul 27, 2020

I'd instead call these constants with the same name for consistency (PTR might work). However, no strong feelings about it.

@therealprof
Copy link
Contributor

Yes, those should all be called PTR so they have a common name, analogous to the ptr() function.

@therealprof
Copy link
Contributor

Now couldn't the ptr() functions just use the fine new PTR consts? ;)

@therealprof
Copy link
Contributor

One tiny little ask and then I'm happy: Would you please add an entry to the CHANGELOG?

JOE1994 added 5 commits July 27, 2020 19:01
This commit introduces new associated constants to Core Peripherals.
(pointers to the register block)
This commit also adds a notice that 'ptr()' APIs will be deprecated in
v0.7.
@JOE1994
Copy link
Contributor Author

JOE1994 commented Jul 27, 2020

I did a rebase to update the CHANGELOG.

CHANGELOG.md Outdated
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- New `InterruptNumber` trait is now required on interrupt arguments to the
various NVIC functions, replacing the previous use of `Nr` from bare-metal.
- Associated const `PTR` is introduced to Core Peripherals to
replace the existing `ptr()` API. `ptr()` API is to be removed in the
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the plan was to mark as deprecated in 0.7 and remove it for good in 1.0? Anyway I'd just leave it out for now, we can always tweak the deprecation/removal plan in a separate PR.

Co-authored-by: Daniel Egger <[email protected]>
Copy link
Contributor

@therealprof therealprof left a comment

Choose a reason for hiding this comment

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

Excellent, thanks a lot!

bors r+

@bors
Copy link
Contributor

bors bot commented Jul 28, 2020

Build succeeded:

@bors bors bot merged commit f0d1ed4 into rust-embedded:master Jul 28, 2020
@JOE1994 JOE1994 deleted the const_fn branch July 28, 2020 00:28
therealprof added a commit to rust-embedded/svd2rust that referenced this pull request Jul 28, 2020
bors bot added a commit to rust-embedded/svd2rust that referenced this pull request Jul 28, 2020
457: Add associated const ptr `PTR` to each peripheral RegisterBlock r=adamgreig a=therealprof

cc rust-embedded/cortex-m#235

Signed-off-by: Daniel Egger <[email protected]>

Co-authored-by: Daniel Egger <[email protected]>
@therealprof therealprof removed the nominated Issue nominated as discussion topic for the Embedded WG meeting label Jul 28, 2020
TDHolmes added a commit to TDHolmes/cortex-m that referenced this pull request Jan 6, 2022
Per rust-embedded#370/rust-embedded#235, the const fn ptr() was supposed to be deprecated and
subsequently removed. This PR removes it for the master branch tracking
the 0.8 release
TDHolmes added a commit to TDHolmes/cortex-m that referenced this pull request Jan 6, 2022
Per rust-embedded#370/rust-embedded#235, the const fn ptr() was supposed to be deprecated and
subsequently removed. This PR removes it for the master branch tracking
the 0.8 release
bors bot added a commit that referenced this pull request Jan 10, 2022
385: remove the ptr() function in favor of the PTR constant r=adamgreig a=TDHolmes

Per #370/#235, the const fn ptr() was supposed to be deprecated and subsequently removed. This PR removes it for the master branch tracking the 0.8 release

Co-authored-by: Tyler Holmes <[email protected]>
adamgreig pushed a commit that referenced this pull request Jan 12, 2022
235: Document a possible reason for the 'interrupt vectors are missing' error r=korken89 a=Disasm

This error happens when you have all the needed dependencies in Cargo.toml but do not use them yet in main.rs. Due to the fact that you already "have" a "svd2rust generated device crate", this error is quite confusing.

Co-authored-by: Vadim Kaushan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cortex-m
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants