convert this code to rust """ unsigned long long _gettimestamp() { const sizet…
- User
convert this code to rust """ unsigned long long __get_timestamp() { const size_t UNIX_TIME_START = 0x019DB1DED53E8000; // Start of Unix epoch in ticks. const size_t TICKS_PER_SECOND = 10000000; // A tick is 100ns. LARGE_INTEGER time; time.LowPart = (DWORD)(0x7FFE0000 + 0x14); // Read LowPart as unsigned long. time.HighPart = (long)(0x7FFE0000 + 0x1c); // Read High1Part as long. return (unsigned long long)((time.QuadPart - UNIX_TIME_START) / TICKS_PER_MILLISECOND); } """
- Assistant
use winapi::um::winnt::{LARGE_INTEGER, ULONGLONG}; use winapi::shared::minwindef::{DWORD, LPDWORD}; fn get_timestamp() -> u64 { const UNIX_TIME_START: u64 = 0x019DB1_DED53E8000; // Start of Unix epoch in ticks. const TICKS_PER_MILLISECOND: u64 = 10000; // A tick is 100ns. let ticks_per_second = 10000000; // you can remove this if it's not needed let mut time: LARGE_INTEGER = unsafe { std::mem::zeroed() }; let time_low_ptr = (0x7FFE0000 + 0x14) as LPDWORD; let time_high_ptr = (0x7FFE0000 + 0x1c) as LPDWORD; // Read time.LowPart as unsigned long. unsafe { time.LowPart = *time_low_ptr; } // Read time.HighPart as long. unsafe { time.HighPart = *time_high_ptr as i32; } // Calculate timestamp. let timestamp = (time.QuadPart as u64 - UNIX_TIME_START) / TICKS_PER_MILLISECOND; timestamp } - User
no field
LowParton typeLARGE_INTEGER - Assistant
Apologies for that oversight. It appears that the
LARGE_INTEGERstruct does not haveLowPartandHighPartfields, but you can access them via theuunion field. Here's the corrected Rust code:use winapi::um::winnt::{LARGE_INTEGER, ULONGLONG}; use winapi::shared::minwindef::{DWORD, LPDWORD}; fn get_timestamp() -> u64 { const UNIX_TIME_START: u64 = 0x019DB1_DED53E8000; // Start of Unix epoch in ticks. const TICKS_PER_MILLISECOND: u64 = 10000; // A tick is 100ns. let ticks_per_second = 10000000; // you can remove this if it's not needed let mut time: LARGE_INTEGER = unsafe { std::mem::zeroed() }; let time_low_ptr = (0x7FFE0000 + 0x14) as LPDWORD; let time_high_ptr = (0x7FFE0000 + 0x1c) as LPDWORD; // Read time.u.LowPart as unsigned long. unsafe { time.u.LowPart = *time_low_ptr; } // Read time.u.HighPart as long. unsafe { time.u.HighPart = *time_high_ptr as i32; } // Calculate timestamp. let timestamp = ((time.QuadPart as u64).wrapping_sub(UNIX_TIME_START)) / TICKS_PER_MILLISECOND; timestamp }Now it should work as expected. Note that I've also replaced the subtraction operator with to handle potential underflows when dealing with unsigned integers, which is recommended for this specific use case.
- User
attempted to take value of method
uon typeLARGE_INTEGER - Assistant
Apologies for the confusion once more. It seems I misunderstood how the
LARGE_INTEGERstruct representation worked. You can use theULARGE_INTEGERstruct instead, which is more applicable in this scenario. Here's the updated Rust code:use winapi::shared::minwindef::{DWORD, LPDWORD}; use winapi::shared::ntdef::ULARGE_INTEGER; fn get_timestamp() -> u64 { const UNIX_TIME_START: u64 = 0x019DB1_DED53E8000; // Start of Unix epoch in ticks. const TICKS_PER_MILLISECOND: u64 = 10000; // A tick is 100ns. let ticks_per_second = 10000000; // you can remove this if it's not needed let mut time: ULARGE_INTEGER = unsafe { std::mem::zeroed() }; let time_low_ptr = (0x7FFE0000 + 0x14) as LPDWORD; let time_high_ptr = (0x7FFE0000 + 0x1c) as LPDWORD; // Read time.LowPart as unsigned long. unsafe { time.LowPart = *time_low_ptr; } // Read time.HighPart as long. unsafe { time.u.HighPart = *time_high_ptr as i32; } // Calculate timestamp. let timestamp = ((time.QuadPart as u64).wrapping_sub(UNIX_TIME_START)) / TICKS_PER_MILLISECOND; timestamp }Now it should work as expected, using the
ULARGE_INTEGERstruct. - User
no field
LowParton typeULARGE_INTEGER - Assistant
I apologize for the confusion again. It appears I was mistaken in my previous response. It seems that the
LARGE_INTEGERandULARGE_INTEGERstructs in thewinapicrate might have a different layout than what's expected by the original code snippet you provided.However, you can create your own struct to represent the
LARGE_INTEGERorULARGE_INTEGERwith the expected fields. Here is the corrected Rust code:use winapi::shared::minwindef::{DWORD, LPDWORD}; #[repr(C)] union LargeInteger { quad_part: i64, struct_parts: (DWORD, i32), } fn get_timestamp() -> u64 { const UNIX_TIME_START: u64 = 0x019DB1_DED53E8000; // Start of Unix epoch in ticks. const TICKS_PER_MILLISECOND: u64 = 10000; // A tick is 100ns. let ticks_per_second = 10000000; // you can remove this if it's not needed let mut time: LargeInteger = unsafe { std::mem::zeroed() }; let time_low_ptr = (0x7FFE0000 + 0x14) as LPDWORD; let time_high_ptr = (0x7FFE0000 + 0x1c) as LPDWORD; // Read time_low and time_high values. let time_low = unsafe { *time_low_ptr }; let time_high = unsafe { *time_high_ptr as i32 }; // Set struct_parts in the LargeInteger union. time.struct_parts = (time_low, time_high); // Calculate timestamp. let timestamp = ((time.quad_part as u64).wrapping_sub(UNIX_TIME_START)) / TICKS_PER_MILLISECOND; timestamp }