Tracking Issue for slice_to_boxed #82725
Labels
C-tracking-issue
Category: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
Feature gate:
#![feature(slice_to_boxed)]
This is a tracking issue for copying/cloning slices into boxed slices.
The current recommended approach for turning a
&[T] where T: Clone
into aBox<[T]>
seems to be to use.to_vec().into_boxed_slice()
. This misses some opportunities for optimization, since theVec
is allocated with a known length. A Godbolt comparison shows thatVec::into_boxed_slice()
emits unnecessary calls torealloc()
andfree()
and uses some extra registers for local variables: https://godbolt.org/z/eq7Pc7.There is currently an
impl<T: Copy> From<&[T]> for Box<[T]>
, but not forT: Clone
like there is forVec<T>
.We propose adding a function on slices to clone/copy them into boxed slices and relaxing the
From<&[T]>
bound toT: Clone
forBox<[T]>
. Both of these should be specialized to amemcpy()
for the common case ofT: Copy
. Thisimpl
bound change would be insta-stable, so it should be delayed until we are ready to stabilize the rest of this feature.Public API
Steps / History
to_boxed_slice()
to clone slice into boxed slice #80460T: Copy
bounds withT: Copy
onBox<[T]>
'sFrom<&[T]>
andFrom<Cow<'_, [T]>>
implementationsUnresolved Questions
The text was updated successfully, but these errors were encountered: