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

linux/timerfd: stop explicitly casting to c_long for timespec.tv_nsec #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/linux/timerfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,15 @@ impl TimerFd {
{
spec.it_value.tv_sec = dur.as_secs() as libc::time_t;
}
// nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
let nsec = dur.subsec_nanos() as i32;
spec.it_value.tv_nsec = libc::c_long::from(nsec);
spec.it_value.tv_nsec = dur.subsec_nanos() as _;
Copy link
Contributor

Choose a reason for hiding this comment

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

Very much personal preference but I find as _ to be quite unreadable.

Suggested change
spec.it_value.tv_nsec = dur.subsec_nanos() as _;
spec.it_value.tv_nsec = dur.subsec_nanos().try_into().unwrap();


if let Some(int) = interval {
// https://github.com/rust-lang/libc/issues/1848
#[cfg_attr(target_env = "musl", allow(deprecated))]
{
spec.it_interval.tv_sec = int.as_secs() as libc::time_t;
}
// nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
let nsec = int.subsec_nanos() as i32;
spec.it_interval.tv_nsec = libc::c_long::from(nsec);
spec.it_interval.tv_nsec = int.subsec_nanos() as _;
Copy link
Contributor

Choose a reason for hiding this comment

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

Duplicate of 7f7d0f1#r1168297816

Suggested change
spec.it_interval.tv_nsec = int.subsec_nanos() as _;
spec.it_interval.tv_nsec = int.subsec_nanos().try_into().unwrap();

Copy link
Author

Choose a reason for hiding this comment

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

Once again, this is 2nd day since I looked what rust is, to begin with, so I know nothing about the right constructs required here. I'm fine as long as it works. "My" version has been suggested by someone on IRC, - I didn't even know rust requires explicit type conversion between two different integer types.

FWIW, this issue happens on x32 only. On all the other platforms, timespec.tv_nsec is of long datatype. It is documented as being long. But on x32 it is int64_t. Apparently this is a bug in the architecture specifications. Also, x32 is a very rare thing.

Still, I like the code without explicit integer conversion more. It might also be a good idea to retain the comment which was previously there.

}

// SAFETY: Safe because this doesn't modify any memory and we check the return value.
Expand Down