Skip to content

Commit 75a127c

Browse files
authored
Rollup merge of #65873 - lzutao:doc-vec-from-raw-parts, r=rkruppe
doc: explain why it is unsafe to construct Vec<u8> from Vec<u16>
2 parents 575058f + 3f98078 commit 75a127c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/liballoc/vec.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,11 @@ impl<T> Vec<T> {
411411
///
412412
/// Violating these may cause problems like corrupting the allocator's
413413
/// internal data structures. For example it is **not** safe
414-
/// to build a `Vec<u8>` from a pointer to a C `char` array and a `size_t`.
414+
/// to build a `Vec<u8>` from a pointer to a C `char` array with length `size_t`.
415+
/// It's also not safe to build one from a `Vec<u16>` and its length, because
416+
/// the allocator cares about the alignment, and these two types have different
417+
/// alignments. The buffer was allocated with alignment 2 (for `u16`), but after
418+
/// turning it into a `Vec<u8>` it'll be deallocated with alignment 1.
415419
///
416420
/// The ownership of `ptr` is effectively transferred to the
417421
/// `Vec<T>` which may then deallocate, reallocate or change the

0 commit comments

Comments
 (0)