Skip to content

Commit 586043f

Browse files
Use slice::from_raw_parts instead of mem::transmute
Layout compatibility guarantees for `T` do not extend to types containing `T` (like `&[T]`), so use `from_raw_parts` instead of `mem::transmute`.
1 parent bebe212 commit 586043f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/join.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,14 @@ impl<'me, Tuple: Ord> JoinInput<'me, (Tuple, ())> for &'me Relation<Tuple> {
210210
assert_eq!(mem::align_of::<(Tuple, ())>(), mem::align_of::<Tuple>());
211211

212212
// SAFETY: https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html#structs-with-1-zst-fields
213-
// guarantees that `T` is layout compatible with `(T, ())`, since `()` is a 1-ZST.
213+
// guarantees that `T` is layout compatible with `(T, ())`, since `()` is a 1-ZST. We use
214+
// `slice::from_raw_parts` because the layout compatibility guarantee does not extend to
215+
// containers like `&[T]`.
214216
let elements: &'me [Tuple] = self.elements.as_slice();
215-
let elements: &'me [(Tuple, ())] = unsafe { std::mem::transmute(elements) };
217+
let len = elements.len();
218+
219+
let elements: &'me [(Tuple, ())] =
220+
unsafe { std::slice::from_raw_parts(elements.as_ptr() as *const _, len) };
216221

217222
f(elements)
218223
}

0 commit comments

Comments
 (0)