Skip to content

Commit

Permalink
avoid typecasting twice and use as _
Browse files Browse the repository at this point in the history
  • Loading branch information
bibin committed Aug 24, 2023
1 parent aa5591c commit f033c4c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl<W: Write> MSWriter<W> {
Some(record_handler::<W>),
(&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,
Expand Down
13 changes: 6 additions & 7 deletions src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<F>),
(&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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
20 changes: 10 additions & 10 deletions src/record.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn detect<T: AsRef<[u8]>>(buf: T) -> MSResult<RecordDetection> {
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,
))
}?;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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/).
Expand Down Expand Up @@ -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<time::OffsetDateTime> {
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<time::OffsetDateTime> {
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`)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
)
Expand Down
22 changes: 11 additions & 11 deletions src/trace.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,12 +43,12 @@ impl MSTraceId {

/// Returns the time of the the first sample.
pub fn start_time(&self) -> MSResult<OffsetDateTime> {
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<OffsetDateTime> {
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.
Expand Down Expand Up @@ -106,12 +106,12 @@ impl<'id> MSTraceSegment<'id> {

/// Returns the time of the the first sample.
pub fn start_time(&self) -> MSResult<OffsetDateTime> {
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<OffsetDateTime> {
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`)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -522,15 +522,15 @@ 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,
)
.map_err(|_| fmt::Error)?;

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,
)
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn nstime_to_time(nst: c_long) -> MSResult<time::OffsetDateTime> {
let mut nsec = 0;
unsafe {
check(raw::ms_nstime2time(
nst.into(),
nst as _,
&mut year,
&mut yday,
&mut hour,
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit f033c4c

Please sign in to comment.