-
Notifications
You must be signed in to change notification settings - Fork 17
Experimental autodetection of 64 bit timespec on 32 bit platforms #18
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,33 @@ | |
| #![allow(non_upper_case_globals)] | ||
| #![no_std] | ||
|
|
||
| use libc::{FILE, pid_t, pollfd, timespec, timeval}; | ||
| use libc::{FILE, pid_t, pollfd, timeval}; | ||
|
|
||
| #[cfg(target_pointer_width = "64")] | ||
| #[derive(Copy, Clone, Eq, PartialEq, Default)] | ||
| #[repr(C)] | ||
| pub struct timespec([i64; 2]); | ||
|
|
||
| #[cfg(target_pointer_width = "64")] | ||
| impl timespec { | ||
| #[inline(always)] | ||
| pub fn tv_sec(&self) -> i64 { self.0[0] } | ||
| #[inline(always)] | ||
| pub fn tv_nsec(&self) -> i64 { self.0[1] } | ||
| } | ||
|
|
||
| #[cfg(target_pointer_width = "32")] | ||
| #[derive(Copy, Clone, Eq, PartialEq, Default)] | ||
| #[repr(C)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also do
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And good idea to use those native-width |
||
| pub struct timespec([i32; 4]); | ||
|
|
||
| #[cfg(target_pointer_width = "32")] | ||
| impl timespec { | ||
| fn is64(&self) -> bool { self.0[2] != 0 || self.0[3] != 0 } | ||
| pub fn tv_sec(&self) -> i64 { self.0[0] as i64 } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If pub fn tv_sec(&self) -> i64 {
if self.is64() {
#[cfg(target_endian = "little")]
((self.0[1] as i64) << 32) | (self.0[0] as u32 as i64)
#[cfg(target_endian = "big")]
((self.0[0] as i64) << 32) | (self.0[1] as u32 as i64)
} else {
self.0[0] as i64
}
}...I think 🤯 time problems are hard.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @roderickvd Yeah and there is an edge case where is64 would return false because tv_nsec actually is 0. This would fail on big endian :-( |
||
| pub fn tv_nsec(&self) -> i64 { if self.is64() { (self.0[2] + self.0[3]) as i64 } else { self.0[1] as i64 } } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @roderickvd Having thought about it, I think we should apply this fix on little endian only. Big endian is incredibly uncommon.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As uncommon as it may be, wouldn't it be nice for |
||
| } | ||
|
|
||
|
|
||
| #[cfg(feature = "use-bindgen")] | ||
| include!(concat!(env!("OUT_DIR"), "/generated.rs")); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debugmay be nice to implement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your rewrite I think these were dropped altogether (because they can't be derived on the union). That creates an asymmetry with
libc::timespec, which does derive them.