Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update use of libc::timespec to prepare for future libc version #55

Merged
merged 1 commit into from Dec 3, 2022

Conversation

wesleywiser
Copy link
Contributor

In a future release of the libc crate, libc::timespec will contain private padding fields on *-linux-musl targets and so the struct will no longer be able to be created using the literal initialization syntax.

Update TS_ZERO to create a value by initializing an array of the correct size to 0 and then transmuting to libc::timespec. Update struct literal use of libc::timespec to initialize to TS_ZERO and then manually update the appropriate fields. Also updates a raw syscall to use the libc function instead as on musl 1.2, it correctly handles libc::timespec values which, in musl 1.2, are always 16 bytes in length regardless of platform.

See also rust-lang/libc#2088

In a future release of the `libc` crate, `libc::timespec` will contain
private padding fields on `*-linux-musl` targets and so the struct will
no longer be able to be created using the literal initialization syntax.

Update `TS_ZERO` to create a value by initializing an array of the
correct size to `0` and then transmuting to `libc::timespec`. Update
struct literal use of `libc::timespec` to initialize to `TS_ZERO` and
then manually update the appropriate fields. Also updates a raw syscall
to use the libc function instead as on musl 1.2, it correctly handles
`libc::timespec` values which, in musl 1.2, are always 16 bytes in
length regardless of platform.
Copy link
Collaborator

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines +266 to +267
const TS_ZERO: libc::timespec =
unsafe { std::mem::transmute([0u8; std::mem::size_of::<libc::timespec>()]) };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I thought const transmute was stable at 1.56, but it seems to compile successfully at 1.47?

https://doc.rust-lang.org/stable/std/intrinsics/fn.transmute.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that too, is that some kind of compiler magic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it was special cased in the compiler for 1.46 and that special casing was removed in 1.56 when reading union fields in const contexts was stabilized.

@taiki-e taiki-e merged commit 5343302 into smol-rs:master Dec 3, 2022
@taiki-e taiki-e mentioned this pull request Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants