From 0fd5d615aa41e2a53e7d11e04142b647ad04046b Mon Sep 17 00:00:00 2001 From: soruh Date: Fri, 31 Jul 2026 02:00:29 +0200 Subject: [PATCH] Update safety comments for case. --- src/lib.rs | 12 ++++++++---- src/send.rs | 14 ++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fc15ac7..44cc16a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,8 +83,10 @@ impl TypeErasedVec { /// Casts the present elements to the new type without calling their destructor. /// /// # Safety - /// The caller must ensure that the existing elements can be safely transmuted - /// into the new type `T` and that dropping them as `T` is sound. + /// `T` must have exactly the same [`Layout`] as the erased element type. + /// Matching size alone is insufficient because the allocation must also have + /// the alignment required by `T`. The caller must additionally ensure that + /// every existing element is valid as `T` and can be soundly dropped as `T`. pub unsafe fn cast_type(&mut self) -> ContentGuard<'_, T> { debug_assert_eq!(self.layout, Layout::new::()); @@ -171,8 +173,10 @@ impl TypeErasedVec { /// Convert the capacity of the erased `Vec` into a `Vec`. /// /// # Safety - /// The caller must ensure that the existing elements can be safely transmuted - /// into the new type `T` and that dropping them as `T` is sound. + /// `T` must have exactly the same [`Layout`] as the erased element type. + /// Matching size alone is insufficient because the allocation must also have + /// the alignment required by `T`. The caller must additionally ensure that + /// every existing element is valid as `T` and can be soundly dropped as `T`. #[must_use] pub unsafe fn cast_into_vec(mut self) -> Vec { unsafe { self.cast_type::() }.take() diff --git a/src/send.rs b/src/send.rs index 830f78a..126d569 100644 --- a/src/send.rs +++ b/src/send.rs @@ -58,8 +58,11 @@ macro_rules! define_thread_safe_erased_vec { /// Access the erased vector as `T` without clearing its elements. /// /// # Safety - /// The caller must ensure that the existing elements can be safely - /// transmuted into `T` and that dropping them as `T` is sound. + /// `T` must have exactly the same [`Layout`] as the erased element + /// type. Matching size alone is insufficient because the allocation + /// must also have the alignment required by `T`. The caller must + /// additionally ensure that every existing element is valid as `T` + /// and can be soundly dropped as `T`. pub unsafe fn cast_type(&mut self) -> ContentGuard<'_, T> where T: $first_bound $(+ $remaining_bound)*, @@ -128,8 +131,11 @@ macro_rules! define_thread_safe_erased_vec { /// Convert the erased vector into a `Vec` without clearing it. /// /// # Safety - /// The caller must ensure that the existing elements can be safely - /// transmuted into `T` and that dropping them as `T` is sound. + /// `T` must have exactly the same [`Layout`] as the erased element + /// type. Matching size alone is insufficient because the allocation + /// must also have the alignment required by `T`. The caller must + /// additionally ensure that every existing element is valid as `T` + /// and can be soundly dropped as `T`. #[must_use] pub unsafe fn cast_into_vec(self) -> Vec where