Skip to content

Commit

Permalink
Use slice::from_raw_parts instead of mem::transmute
Browse files Browse the repository at this point in the history
Layout compatibility guarantees for `T` do not extend to types
containing `T` (like `&[T]`), so use `from_raw_parts` instead of
`mem::transmute`.
  • Loading branch information
ecstatic-morse committed Aug 27, 2021
1 parent bebe212 commit 586043f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,14 @@ impl<'me, Tuple: Ord> JoinInput<'me, (Tuple, ())> for &'me Relation<Tuple> {
assert_eq!(mem::align_of::<(Tuple, ())>(), mem::align_of::<Tuple>());

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

let elements: &'me [(Tuple, ())] =
unsafe { std::slice::from_raw_parts(elements.as_ptr() as *const _, len) };

f(elements)
}
Expand Down

0 comments on commit 586043f

Please sign in to comment.