|
| 1 | +diff --git a/library/alloc/src/vec/in_place_collect.rs b/library/alloc/src/vec/in_place_collect.rs |
| 2 | +index e4f96fd7640..cbf209a9090 100644 |
| 3 | +--- a/library/alloc/src/vec/in_place_collect.rs |
| 4 | ++++ b/library/alloc/src/vec/in_place_collect.rs |
| 5 | +@@ -168,7 +168,7 @@ const fn in_place_collectible<DEST, SRC>( |
| 6 | + step_merge: Option<NonZeroUsize>, |
| 7 | + step_expand: Option<NonZeroUsize>, |
| 8 | + ) -> bool { |
| 9 | +- if DEST::IS_ZST || mem::align_of::<SRC>() < mem::align_of::<DEST>() { |
| 10 | ++ if const { SRC::IS_ZST || DEST::IS_ZST || mem::align_of::<SRC>() < mem::align_of::<DEST>() } { |
| 11 | + return false; |
| 12 | + } |
| 13 | + |
| 14 | +@@ -186,6 +186,32 @@ const fn in_place_collectible<DEST, SRC>( |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | ++const fn needs_realloc<SRC, DEST>(src_cap: usize, dst_cap: usize) -> bool { |
| 19 | ++ if const { mem::align_of::<SRC>() != mem::align_of::<DEST>() } { |
| 20 | ++ return true; |
| 21 | ++ } |
| 22 | ++ |
| 23 | ++ if const { |
| 24 | ++ // examples that may require reallocs unless src/dest capacities turn out to be multiples at runtime |
| 25 | ++ // Vec<u8> -> Vec<[u8; 4]> |
| 26 | ++ let dst_larger = mem::size_of::<SRC>() < mem::size_of::<DEST>(); |
| 27 | ++ |
| 28 | ++ // Vec<[u8; 3]> -> Vec<[u8; 2]> |
| 29 | ++ // dest_sz can't actually be 0 since in_place_collectible() checks for that but const eval doesn't know that |
| 30 | ++ let dest_sz = mem::size_of::<DEST>(); |
| 31 | ++ let src_not_multiple_of_dest = |
| 32 | ++ dest_sz == 0 || mem::size_of::<SRC>() % mem::size_of::<DEST>() != 0; |
| 33 | ++ |
| 34 | ++ dst_larger || src_not_multiple_of_dest |
| 35 | ++ } { |
| 36 | ++ return src_cap * mem::size_of::<SRC>() != dst_cap * mem::size_of::<DEST>(); |
| 37 | ++ } |
| 38 | ++ |
| 39 | ++ // Equal size + alignment won't need a realloc. |
| 40 | ++ // src size being an integer multiple of the dest size works too |
| 41 | ++ return false; |
| 42 | ++} |
| 43 | ++ |
| 44 | + /// This provides a shorthand for the source type since local type aliases aren't a thing. |
| 45 | + #[rustc_specialization_trait] |
| 46 | + trait InPlaceCollect: SourceIter<Source: AsVecIntoIter> + InPlaceIterable { |
| 47 | +@@ -259,12 +285,7 @@ impl<T, I> SpecFromIter<T, I> for Vec<T> |
| 48 | + // that wasn't a multiple of the destination type size. |
| 49 | + // Since the discrepancy should generally be small this should only result in some |
| 50 | + // bookkeeping updates and no memmove. |
| 51 | +- if (const { |
| 52 | +- let src_sz = mem::size_of::<I::Src>(); |
| 53 | +- src_sz > 0 && mem::size_of::<T>() % src_sz != 0 |
| 54 | +- } && src_cap * mem::size_of::<I::Src>() != dst_cap * mem::size_of::<T>()) |
| 55 | +- || const { mem::align_of::<T>() != mem::align_of::<I::Src>() } |
| 56 | +- { |
| 57 | ++ if needs_realloc::<I::Src, T>(src_cap, dst_cap) { |
| 58 | + let alloc = Global; |
| 59 | + unsafe { |
| 60 | + // The old allocation exists, therefore it must have a valid layout. |
| 61 | +@@ -286,6 +307,8 @@ impl<T, I> SpecFromIter<T, I> for Vec<T> |
| 62 | + let Ok(reallocated) = result else { handle_alloc_error(new_layout) }; |
| 63 | + dst_buf = reallocated.as_ptr() as *mut T; |
| 64 | + } |
| 65 | ++ } else { |
| 66 | ++ debug_assert_eq!(src_cap * mem::size_of::<I::Src>(), dst_cap * mem::size_of::<T>()); |
| 67 | + } |
| 68 | + |
| 69 | + mem::forget(dst_guard); |
| 70 | +diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs |
| 71 | +index 81de7085e09..3d3175ba3a9 100644 |
| 72 | +--- a/library/alloc/tests/vec.rs |
| 73 | ++++ b/library/alloc/tests/vec.rs |
| 74 | +@@ -1213,6 +1213,14 @@ fn assert_in_place_trait<T: InPlaceIterable>(_: &T) {} |
| 75 | + assert_ne!(src_bytes, sink_bytes); |
| 76 | + assert_eq!(sink.len(), 2); |
| 77 | + |
| 78 | ++ let mut src: Vec<[u8; 3]> = Vec::with_capacity(17); |
| 79 | ++ src.resize( 8, [0; 3]); |
| 80 | ++ let iter = src.into_iter().map(|[a, b, _]| [a, b]); |
| 81 | ++ assert_in_place_trait(&iter); |
| 82 | ++ let sink: Vec<[u8; 2]> = iter.collect(); |
| 83 | ++ assert_eq!(sink.len(), 8); |
| 84 | ++ assert!(sink.capacity() <= 25); |
| 85 | ++ |
| 86 | + let src = vec![[0u8; 4]; 256]; |
| 87 | + let srcptr = src.as_ptr(); |
| 88 | + let iter = src |
0 commit comments