From f033c4cac34de3ec4f351afe0ad0f3dd144ec961 Mon Sep 17 00:00:00 2001 From: bibin Date: Thu, 24 Aug 2023 13:04:56 -0400 Subject: [PATCH] avoid typecasting twice and use as _ --- src/io.rs | 2 +- src/pack.rs | 13 ++++++------- src/record.rs | 20 ++++++++++---------- src/trace.rs | 22 +++++++++++----------- src/util.rs | 4 ++-- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/io.rs b/src/io.rs index c9d3dcb..1ec7bbe 100644 --- a/src/io.rs +++ b/src/io.rs @@ -266,7 +266,7 @@ impl MSWriter { Some(record_handler::), (&mut self.writer) as *mut _ as *mut c_void, max_rec_len, - (encoding as c_char).try_into().unwrap(), + encoding as _, ptr::null_mut(), flags.bits(), 0, diff --git a/src/pack.rs b/src/pack.rs index 942fb3c..dd46262 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -69,14 +69,14 @@ where } let cnt_samples: c_long = 0; - let cnt_samples_ptr = &mut cnt_samples.into(); + let cnt_samples_ptr = (&mut cnt_samples.into()) as *mut _; let cnt_records = unsafe { check(raw::mstl3_pack( mstl.get_raw_mut(), Some(rh_wrapper::), (&mut record_handler) as *mut _ as *mut c_void, info.rec_len, - (info.encoding as c_char).try_into().unwrap(), + info.encoding as _, cnt_samples_ptr, flags.bits(), 0, @@ -289,7 +289,7 @@ where unsafe { let sid_len = info.sid().as_bytes_with_nul().len(); ptr::copy_nonoverlapping(info.sid().as_ptr(), (*msr).sid.as_mut_ptr(), sid_len); - (*msr).encoding = (info.encoding as c_char).try_into().unwrap(); + (*msr).encoding = info.encoding as _; (*msr).sampletype = { use MSDataEncoding::*; match info.encoding { @@ -305,8 +305,7 @@ where (*msr).pubversion = info.pub_version; (*msr).formatversion = info.format_version; (*msr).numsamples = c_long::try_from(data_samples.len()) - .map_err(|e| MSError::from_str(&format!("invalid data sample length ({})", e)))? - .into(); + .map_err(|e| MSError::from_str(&format!("invalid data sample length ({})", e)))? as _; (*msr).datasamples = data_samples.as_mut_ptr() as *mut _ as *mut c_void; (*msr).datasize = mem::size_of_val(data_samples); (*msr).extralength = 0; @@ -323,7 +322,7 @@ where } let cnt_samples: c_long = 0; - let cnt_samples_ptr = &mut cnt_samples.into(); + let cnt_samples_ptr = (&mut cnt_samples.into()) as *mut _; let cnt_records = unsafe { check(raw::msr3_pack( @@ -382,7 +381,7 @@ where F: FnMut(&[u8]), { let cnt_samples: c_long = 0; - let cnt_samples_ptr = &mut cnt_samples.into(); + let cnt_samples_ptr = (&mut cnt_samples.into()) as *mut _; let cnt_records = unsafe { check(raw::msr3_pack( diff --git a/src/record.rs b/src/record.rs index 4299ca7..cab4023 100644 --- a/src/record.rs +++ b/src/record.rs @@ -1,4 +1,4 @@ -use std::ffi::{c_char, c_double, c_long, c_uchar, c_uint, c_ulong, c_ushort, CStr}; +use std::ffi::{c_char, c_double, c_long, c_uchar, c_uint, c_ushort, CStr}; use std::fmt; use std::ptr; use std::slice::from_raw_parts; @@ -32,7 +32,7 @@ pub fn detect>(buf: T) -> MSResult { let rec_len = unsafe { check(raw::ms3_detect( buf.as_ptr() as *const _, - (buf.len() as c_ulong).into(), + buf.len() as _, format_version_ptr, )) }?; @@ -180,7 +180,7 @@ impl MSRecord { let buf = &*(buf as *const [_] as *const [c_char]); check(raw::msr3_parse( buf.as_ptr(), - (buf.len() as c_ulong).into(), + buf.len() as _, (&mut msr) as *mut *mut MS3Record, flags.bits(), 0, @@ -213,7 +213,7 @@ impl MSRecord { if !self.ptr().datasamples.is_null() { return Ok(self.num_samples()); } - unsafe { check(raw::msr3_unpack_data(self.0, 0).try_into().unwrap()) } + unsafe { check(raw::msr3_unpack_data(self.0, 0) as _) } } /// Returns the [FDSN source identifier](https://docs.fdsn.org/projects/source-identifiers/). @@ -278,12 +278,12 @@ impl MSRecord { /// Returns the start time of the record (i.e. the time of the first sample). pub fn start_time(&self) -> MSResult { - util::nstime_to_time(self.ptr().starttime.try_into().unwrap()) + util::nstime_to_time(self.ptr().starttime as _) } /// Calculates the end time of the last sample in the record. pub fn end_time(&self) -> MSResult { - unsafe { util::nstime_to_time(check_nst(raw::msr3_endtime(self.0).try_into().unwrap())?) } + unsafe { util::nstime_to_time(check_nst(raw::msr3_endtime(self.0) as _)?) } } /// Returns the nominal sample rate as samples per second (`Hz`) @@ -303,7 +303,7 @@ impl MSRecord { /// Returns the number of data samples as indicated by the raw record. pub fn sample_cnt(&self) -> c_long { - self.ptr().samplecnt.try_into().unwrap() + self.ptr().samplecnt as _ } /// Returns the CRC of the record. @@ -352,7 +352,7 @@ impl MSRecord { /// Returns the number of (unpacked) data samples. pub fn num_samples(&self) -> c_long { - self.ptr().numsamples.try_into().unwrap() + self.ptr().numsamples as _ } /// Returns the record sample type. @@ -411,7 +411,7 @@ impl fmt::Display for MSRecord { v.samplecnt, self.sample_rate_hz(), util::nstime_to_string( - v.starttime.try_into().unwrap(), + v.starttime as _, MSTimeFormat::IsoMonthDayDoyZ, MSSubSeconds::NanoMicro ) @@ -470,7 +470,7 @@ impl fmt::Display for RecordDisplay<'_> { )?; let start_time = unsafe { (*self.rec.get_raw()).starttime }; let start_time = util::nstime_to_string( - start_time.try_into().unwrap(), + start_time as _, MSTimeFormat::IsoMonthDayDoyZ, MSSubSeconds::NanoMicro, ) diff --git a/src/trace.rs b/src/trace.rs index 057cebe..1c103ea 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -1,4 +1,4 @@ -use std::ffi::{c_char, c_double, c_float, c_int, c_long, c_uchar, c_uint, c_ulong}; +use std::ffi::{c_double, c_float, c_int, c_long, c_uchar, c_uint}; use std::fmt; use std::ptr; use std::slice::from_raw_parts; @@ -43,12 +43,12 @@ impl MSTraceId { /// Returns the time of the the first sample. pub fn start_time(&self) -> MSResult { - util::nstime_to_time(self.ptr().earliest.try_into().unwrap()) + util::nstime_to_time(self.ptr().earliest as _) } /// Returns the time of the the last sample. pub fn end_time(&self) -> MSResult { - util::nstime_to_time(self.ptr().latest.try_into().unwrap()) + util::nstime_to_time(self.ptr().latest as _) } /// Returns the number of [`MSTraceSegment`]s for this trace identifier. @@ -106,12 +106,12 @@ impl<'id> MSTraceSegment<'id> { /// Returns the time of the the first sample. pub fn start_time(&self) -> MSResult { - util::nstime_to_time(self.ptr().starttime.try_into().unwrap()) + util::nstime_to_time(self.ptr().starttime as _) } /// Returns the time of the the last sample. pub fn end_time(&self) -> MSResult { - util::nstime_to_time(self.ptr().endtime.try_into().unwrap()) + util::nstime_to_time(self.ptr().endtime as _) } /// Returns the nominal sample rate as samples per second (`Hz`) @@ -121,7 +121,7 @@ impl<'id> MSTraceSegment<'id> { /// Returns the number of samples in trace coverage. pub fn sample_cnt(&self) -> c_long { - self.ptr().samplecnt.try_into().unwrap() + self.ptr().samplecnt as _ } /// Returns the data samples of the trace segment. @@ -151,7 +151,7 @@ impl<'id> MSTraceSegment<'id> { /// Returns the number of (unpacked) data samples. pub fn num_samples(&self) -> c_long { - self.ptr().numsamples.try_into().unwrap() + self.ptr().numsamples as _ } /// Returns the trace segment sample type. @@ -390,7 +390,7 @@ impl MSTraceList { check(raw::mstl3_readbuffer( (&mut rv.get_raw_mut()) as *mut *mut _, buf.as_ptr(), - (buf.len() as c_ulong).into(), + buf.len() as _, 0, flags.bits(), ptr::null_mut(), @@ -430,7 +430,7 @@ impl MSTraceList { rec.into_raw(), ptr::null_mut(), 0, - (autoheal as c_char).try_into().unwrap(), + autoheal as _, MSControlFlags::empty().bits(), ptr::null_mut(), ) @@ -522,7 +522,7 @@ impl fmt::Display for TraceListDisplay<'_> { for tseg in tid.iter() { let start_time = unsafe { (*tseg.inner).starttime }; let start_time_str = util::nstime_to_string( - start_time.try_into().unwrap(), + start_time as _, self.time_format, MSSubSeconds::NanoMicro, ) @@ -530,7 +530,7 @@ impl fmt::Display for TraceListDisplay<'_> { let end_time = unsafe { (*tseg.inner).endtime }; let end_time_str = util::nstime_to_string( - end_time.try_into().unwrap(), + end_time as _, self.time_format, MSSubSeconds::NanoMicro, ) diff --git a/src/util.rs b/src/util.rs index 14e3042..5124501 100644 --- a/src/util.rs +++ b/src/util.rs @@ -73,7 +73,7 @@ pub fn nstime_to_time(nst: c_long) -> MSResult { let mut nsec = 0; unsafe { check(raw::ms_nstime2time( - nst.into(), + nst as _, &mut year, &mut yday, &mut hour, @@ -102,7 +102,7 @@ pub fn nstime_to_string( .into_raw(); unsafe { if raw::ms_nstime2timestr( - nst.into(), + nst as _, time, time_format.as_raw(), subsecond_format.as_raw(),