-
Notifications
You must be signed in to change notification settings - Fork 246
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
Fix lifetimes and mutability of get_buf
and get_buf_mut
#649
Conversation
This sounds reasonable, but I don't know enough about this code to evaluate the implications. @ithinuel, what do you think? |
I'm curious, why did you remove |
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.
I'd rather avoid code duplication and keep the unsafe fn get_buf_parts
as this is orthogonal to the issue mentionned about lifetimes. I'm not against using a match statement vs an if statement. In this situation I don't think it will have a significant impact on the generated code.
Lifetimes should have been '_ and not static.
Based on my reading of lifetime elision
, '_
is expands to fn get_buf_mut<'self>(&'self mut self) -> &'self mut [u8]
.
The reference targets somewhere in the DPRAM
block which is a hardware element that lives for 'static
.
So arguably, reusing the lifetime of 'self
isn't more correct than using 'static
.
A benefit of this would be that it'd prevent one from keeping that reference and continue using it after the Endpoint
has be destroyed but it is not possible to do such a thing at present anyway.
get_buf_mut
created a mutable reference from an immutable reference.
It is not. It is built from a *mut u8
pointer to a dedicated ram region and a length.
As far as I know, *const T as *mut T
is UB but not *mut T as *const T
.
If the concern comes from the use of offset
, it is implemented for both *const T
and *mut T
(the latter being our usecase in get_buf_parts
) so there's no hidden conversion there.
With the old signature, |
Fair enough :) |
Thank you for the replies.
I'm currently unsure about the safety rules regarding temporarily mutable references and pointers, and
In my perspective, More pragmatically:
changing the type to be |
Indeed. If you don't mind keeping the |
Just as a side note: |
Lifetimes should have been `'_` and not static. `get_buf_mut` created a mutable reference from an immutable reference.
Lifetimes should have been
'_
and not static.get_buf_mut
created a mutable reference from an immutable reference.SAFETY annotation has also been added.