-
Notifications
You must be signed in to change notification settings - Fork 13
Add buffer slices #20
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
base: master
Are you sure you want to change the base?
Conversation
56aeeb2
to
6d42fbe
Compare
ca66093
to
dcf30ff
Compare
@eldruin @thalesfragoso any thoughts? |
Sorry, I haven't had the time to review this yet. However, I would like to say that I agree with the underlying reasoning and I know that |
src/slice.rs
Outdated
fn coerced_range(&self, len: usize) -> Range<usize> { | ||
let start = self.range.start.min(len); | ||
let end = self.range.end.min(len); | ||
Range { start, end } | ||
} |
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.
Wouldn't an incorrect range be a reason to panic instead of silently attempting to correct the mistake.
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 agree. I changed the API to behave like the get
method of a slice
.
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'm not entirely sure what the most common use case will be, but I thought getting an invalid range always the user made a programming error. In that case it doesn't really make sense to have all these Option<>
wrappers. I would have expected it to be behave like buf[start..end]
which will panic if it's out of bounds.
I'm actually not sure what would be the most practical solution here. Maybe someone else should comment on this.
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 mean there is a non-panicking buf.get(start..end)
method and a panicking buf[start..end]
. Perhaps we could have both? I am trying to emulate regular slices as much as possible, and I feel you could argue buf.get(start..end)
isn't needed because it is always user error as well. However, I think the non-panicking alternative might be useful in code which should not panic, or to more easily handle cases of overflows. I agree, I'd be interested in seeing what others think.
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.
@eldruin I looked further into this. We would need four extension functions
into_write_buffer_slice
into_write_buffer_slice_checked
into_read_buffer_slice_checked
into_read_buffer_slice
or something like that. This seems quite verbose compared to just having two that can accomplish the same thing with than added .unwrap()
.
I am not sure a slice-like API is as easy as I thought it would be.
fa7694a
to
5dd0051
Compare
Depends on #19
Add a means for taking a slice of a buffer. I think the reasoning for this is best explained by the documentation I have written.
A [BufferSlice] is a slice which wraps either a [ReadBuffer] or a [WriteBuffer]
(0, len]
, wherelen
is the length of the original bufferUse Case
Many HALs use the length of a {Read,Write}Buffer to configure DMA Transfers. However, changing
the length of the buffer can be complicated. For instance, consider the case where we want to
change the length of a slice for a DMA transfer:
Considerations
into_buffer_slice
or not?let buf_slice = (&BUF).into_buffer_slice(123..);
since we have to add ()'s around&BUF
into_buffer_slice
implemented for Read and Write Buffers, not for all sized structs? We could instead have methodsinto_{write, read}_buffer_slice
.