fix: Buffer::into_mutable return error instead of panic for converting owned sliced when not start at 0 and fix returned Mutable length#10118
Conversation
alamb
left a comment
There was a problem hiding this comment.
Thanks @rluvaton
I agree we should avoid panics and intead return error. However, this PR seems to be trying to allow some sort of mutable buffer conversion even when the buffer is sliced.
I am not sure about the usecase for this API change -- it seems like the API would get more complicated with this PR.
If you want to allow reusing an allocation from a sliced buffer, how about something like
let unsliced_buffer = buffer.unslice(); <-- new API, clear the offset
let mutable = unsliced_buffer.int_mutable();
..Then we could add a doc example showing how to unslice a buffer and how to reuse a sliced buffer
|
I like the idea of unslice will split to 2 PRs |
|
Marking as draft as I think this PR is no longer waiting on feedback and I am trying to make it easier to find PRs in need of review. Please mark it as ready for review when it is ready for another look |
Buffer::into_mutable and add Buffer::is_sliced and Buffer::into_mutable_unslicedBuffer::into_mutable return error instead of panic for converting owned sliced when not start at 0 and fix returned Mutable length
|
@alamb I fixed the code, it fix 2 bugs, the panic and different length for sliced |
|
✅ thank you @rluvaton |
Which issue does this PR close?
Buffer::into_mutableis not consistent regarding sliced data and can lead to panics #10117Rationale for this change
This include 2 fixes:
Buffer::into_mutableon owned sliced thatBuffer::ptr_offset() > 0panicked before, now returning errorBuffer::into_mutableon owned slice thatBuffer::ptr_offset() == 0returnedMutableBufferwith length greater than the original buffer lengthWhat changes are included in this PR?
Buffer::into_mutableto return error whenptr_offset > 0MutableBufferlength to the original buffer length so length is the sameAre these changes tested?
yes
Are there any user-facing changes?
not really, except error instead of panic and correct mutable length