-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Added RFC on dereferencing *T #43
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| - Start Date: 2014-04-14 | ||
| - RFC PR #: (leave this empty) | ||
| - Rust Issue #: (leave this empty) | ||
|
|
||
| # Summary | ||
|
|
||
| Make a way to dereference *T in a simpler way. | ||
|
|
||
| # Motivation | ||
|
|
||
| In most of the cases, embedded hardware is configured with memory-mapped registers. Often a peripheral would have a moderately big number of registers to the point where tracking each one by its address is hard and prone to errors. | ||
|
|
||
| It's common to have structs mapping a given peripheral's ioregs in embedded C world. Unfortunately with rust using such structs require "junk code". | ||
|
|
||
| # Detailed design | ||
|
|
||
| Consider the following example: | ||
|
|
||
| ```rust | ||
| // this struct maps over a number of ioregs | ||
| pub struct UART { | ||
| REG_A: u32, | ||
| REG_B: u32, | ||
| _pad_0: u32, | ||
| REG_C: u32, | ||
| } | ||
|
|
||
| impl UART { | ||
| // given the nature of regs, we must use volatile access | ||
| pub fn get_REG_A(&self) { | ||
| unsafe { volatile_load(&(self.REG_A)) } | ||
| } | ||
|
|
||
| pub fn set_REG_A(&self, val: u32) { | ||
| unsafe { volatile_store(&mut (self.REG_A), val) } | ||
| } | ||
| ... | ||
| } | ||
|
|
||
| // instantiate all peripherals over corresponding ioregs | ||
| pub static UART0 : *mut UART = 0xdeadbeef as *mut UART; | ||
| ... | ||
| ``` | ||
|
|
||
| Rust requires an "unsafe" block and explicit dereferencing to access the registers: | ||
|
|
||
| ```rust | ||
| let val: u32 = unsafe { (*UART0).REG_A() }; | ||
| ``` | ||
|
|
||
| Instead of much simpler to read and understand syntax: | ||
|
|
||
| ```rust | ||
| let val: u32 = UART0.REG_A(); | ||
| ``` | ||
|
|
||
| # Alternatives | ||
|
|
||
| It is possible to make a wrapping struct: | ||
|
|
||
| ```rust | ||
| struct UARTWrapper { | ||
| uart: *mut UART, | ||
| } | ||
|
|
||
| // move all methods from UART to UARTWrapper | ||
|
|
||
| static UART0Regs : *mut UART = 0xdeadbeef as *mut UART; | ||
| pub static UART0 : UARTWrapper = UARTWrapper { uart: UART0Regs }; | ||
| ``` | ||
|
|
||
| In this case, accessing methods on `UART0` is done with simple syntax, but the actual | ||
| `static UART0` gets into output binary (as u32 0xdeadbeef), which requres developer to make a trade between syntax and binary size. | ||
|
|
||
| # Unresolved questions | ||
|
|
||
| How to actually simplify the syntax? | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like the more fundamental problem, and the one that would fix your issue: Rust doesn't offer flexible enough control over statics to be able to use safe(r) wrappers for this use case.
(That said, it is just essentially just 8 bytes in rodata, right?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's 4 bytes in rodata, four more instructions to fetch the address from there at each call invocation. That isn't critically significant, but easily adds useless weight to binary.