Extend and drain with slices #146
Open
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.
Content
This pull request provides two new methods:
extend_from_slice
and the inversedrain_to_slice
. Both are only available for items which areCopy
(i.e. no customDrop
).A related refactor
In order to simplify the implementation, I slightly changed the operating mode of
copy_from_slice
andcopy_to_slice
: instead of expecting slices with the same length as the buffer, both methods now also accept slices which are smaller.This refactor allowed me to write the extend and drain methods as wrappers for copy from/to with an extra step for adjusting the read and write pointers.
This change should be backward compatible as current users would observe the same behaviour as before (unless they wrote a test with a
should_panic
specific to the previous equality check).Context
The idea proposed by @phip1611 in #134 to have
extend_from_slice
proved useful in a project of mine and, as I was referring to in #141#issuecomment-2643272235, I had worked on a similar implementation withdrain_to_slice
too.Performance
As for #141, having specialised methods for
Copy
types brings a substantial performance improvement for buffers smaller than a megabyte (and a slight speedup for larger sizes).E.g. (note: in the first chart the X axis is in microseconds, in the second chart it's in nanoseconds)
