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

Add TakeCell::get_mut function #2077

Merged
merged 2 commits into from Sep 9, 2020
Merged

Add TakeCell::get_mut function #2077

merged 2 commits into from Sep 9, 2020

Conversation

daboross
Copy link
Contributor

Pull Request Overview

This adds TakeCell::get_mut, a function with the signature fn get_mut(&mut self) -> Option<&mut T>. Specifically, it bypasses the Cell nature of take_cell for when you already have access to an &mut TakeCell.

This is entirely safe, and works similarly to the corresponding method in Cell, Cell::get_mut. It's made safe by the fact that the returned value is lifetime bound to the mutable borrow of the TakeCell itself, not to 'a - and thus, requires no new unsafe code to implement.

This was useful for an odd interaction with the rubble interface when implementing tock_rubble - it needs to retrieve an &mut [u8] multiple times safely, and then also use that buffer as an &'static mut [u8] which can be taken out. Thus a TakeCell, with the addition of this get_mut method, works well.

Testing Strategy

CI

TODO or Help Wanted

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

This bypasses the Cell nature of take_cell for situations where you
already have access to an `&mut TakeCell`. Similarly to how the
corresponding method in `Cell`, `Cell::get_mut` is safe,
`TakeCell::get_mut` is entirely safe and requires no new unsafe code to
implement. It's made safe by the fact that the returned value is
lifetime bound to the takecell itself, not to `'a`.

This was useful for an odd interaction with the rubble interface when
implementing tock_rubble - it needs to retrieve an `&mut [u8]` multiple
times safely, and then also use that buffer as an `&'static mut [u8]`
which can be taken out. Thus a `TakeCell`, with the addition of this
`get_mut` method, works well.

Signed-off-by: David Ross <David.Ross@wdc.com>
@daboross daboross mentioned this pull request Aug 15, 2020
15 tasks
hudson-ayers
hudson-ayers previously approved these changes Aug 17, 2020
Copy link
Contributor

@hudson-ayers hudson-ayers left a comment

Choose a reason for hiding this comment

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

I am comfortable this is safe now that TakeCell uses no unsafe, and I can imagine this being useful.

libraries/tock-cells/src/take_cell.rs Outdated Show resolved Hide resolved
Signed-off-by: David Ross <David.Ross@wdc.com>
@bradjc bradjc requested a review from alevy August 19, 2020 14:32
@phil-levis
Copy link
Contributor

bors r+

@bors bors bot merged commit 5daa83c into tock:master Sep 9, 2020
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.

None yet

4 participants