Skip to content

Commit

Permalink
Make unions #[repr(C)]
Browse files Browse the repository at this point in the history
This is necessary to guarantee that the variants have the same address.
  • Loading branch information
jhpratt committed May 30, 2023
1 parent e83d01f commit eb39e1b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions time/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ mod sealed {

pub trait IsOffsetKindFixed:
MaybeOffset<
Self_ = offset_kind::Fixed,
MemoryOffsetType = UtcOffset,
LogicalOffsetType = UtcOffset,
>
Self_ = offset_kind::Fixed,
MemoryOffsetType = UtcOffset,
LogicalOffsetType = UtcOffset,
>
{
}
impl IsOffsetKindFixed for offset_kind::Fixed {}
Expand Down Expand Up @@ -147,6 +147,7 @@ const fn maybe_offset_as_offset_opt<O: MaybeOffset>(
if O::STATIC_OFFSET.is_some() {
O::STATIC_OFFSET
} else if O::HAS_MEMORY_OFFSET {
#[repr(C)] // needed to guarantee they align at the start
union Convert<O: MaybeOffset> {
input: O::MemoryOffsetType,
output: UtcOffset,
Expand All @@ -172,6 +173,7 @@ const fn maybe_offset_as_offset<O: MaybeOffset + HasLogicalOffset>(
pub(crate) const fn maybe_offset_from_offset<O: MaybeOffset>(
offset: UtcOffset,
) -> O::MemoryOffsetType {
#[repr(C)] // needed to guarantee the types align at the start
union Convert<O: MaybeOffset> {
input: UtcOffset,
output: O::MemoryOffsetType,
Expand Down

0 comments on commit eb39e1b

Please sign in to comment.