Skip to content

Commit

Permalink
Fix type mismatch errors for arm (#8)
Browse files Browse the repository at this point in the history
- Fix type mismatch errors for arm.
- Avoid typecasting twice and use `as _`.

---------

Co-authored-by: damb <dani.armbruster@gmail.com>
  • Loading branch information
BIBIN-EUGINE and damb authored Aug 24, 2023
1 parent 6d8d3e0 commit d18b34d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{raw, MSErrorCode, MSResult};

const MS_NOERROR: c_long = raw::MS_NOERROR as c_long;
const MS_ENDOFFILE: c_long = raw::MS_ENDOFFILE as c_long;
const MS_NSTERROR: c_long = raw::NSTERROR;
const MS_NSTERROR: c_long = raw::NSTERROR as c_long;

/// Utility function which turns a libmseed error into a result.
pub(crate) fn check<T: PartialOrd + AsPrimitive<c_long>>(code: T) -> MSResult<T> {
Expand Down
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,
encoding as _,
ptr::null_mut(),
flags.bits(),
0,
Expand Down
19 changes: 10 additions & 9 deletions src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ where
extra_ptr = cloned.into_raw();
}

let mut cnt_samples: c_long = 0;
let cnt_samples_ptr = &mut cnt_samples as *mut _;
let mut cnt_samples: i64 = 0;
let cnt_samples_ptr: *mut i64 = &mut cnt_samples;
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,
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;
(*msr).encoding = info.encoding as _;
(*msr).sampletype = {
use MSDataEncoding::*;
match info.encoding {
Expand All @@ -305,7 +305,8 @@ 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)))?;
.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 @@ -321,8 +322,8 @@ where
}
}

let mut cnt_samples: c_long = 0;
let cnt_samples_ptr = &mut cnt_samples as *mut _;
let mut cnt_samples: i64 = 0;
let cnt_samples_ptr: *mut i64 = &mut cnt_samples;

let cnt_records = unsafe {
check(raw::msr3_pack(
Expand Down Expand Up @@ -380,8 +381,8 @@ pub fn pack_record<F>(
where
F: FnMut(&[u8]),
{
let mut cnt_samples: c_long = 0;
let cnt_samples_ptr = &mut cnt_samples as *mut _;
let mut cnt_samples: i64 = 0;
let cnt_samples_ptr: *mut i64 = &mut cnt_samples;

let cnt_records = unsafe {
check(raw::msr3_pack(
Expand Down
64 changes: 32 additions & 32 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,
buf.len() as _,
format_version_ptr,
))
}?;
Expand All @@ -50,7 +50,7 @@ pub fn detect<T: AsRef<[u8]>>(buf: T) -> MSResult<RecordDetection> {
}

/// An enumeration of possible sample types.
#[repr(i8)]
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum MSSampleType {
/// Unknown data sample type.
Expand All @@ -67,7 +67,7 @@ pub enum MSSampleType {

impl MSSampleType {
/// Creates a `MSSampleType` from the given `ch`.
pub fn from_char(ch: c_char) -> Self {
pub fn from_char(ch: u8) -> Self {
match ch {
116 => Self::Text, // t
105 => Self::Integer32, // i
Expand All @@ -79,41 +79,41 @@ impl MSSampleType {
}

/// An enumeration of possible data encodings.
#[repr(i8)]
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum MSDataEncoding {
/// Text encoding (UTF-8)
Text = raw::DE_TEXT as c_char,
Text = raw::DE_TEXT as u8,
/// 16-bit integer encoding
Integer16 = raw::DE_INT16 as c_char,
Integer16 = raw::DE_INT16 as u8,
/// 32-bit integer encoding
Integer32 = raw::DE_INT32 as c_char,
Integer32 = raw::DE_INT32 as u8,
/// 32-bit floating point encoding (IEEE)
Float32 = raw::DE_FLOAT32 as c_char,
Float32 = raw::DE_FLOAT32 as u8,
/// 64-bit floating point encoding (IEEE)
Float64 = raw::DE_FLOAT64 as c_char,
Float64 = raw::DE_FLOAT64 as u8,
/// Steim-1 compressed integer encoding
Steim1 = raw::DE_STEIM1 as c_char,
Steim1 = raw::DE_STEIM1 as u8,
/// Steim-2 compressed integer encoding
Steim2 = raw::DE_STEIM2 as c_char,
Steim2 = raw::DE_STEIM2 as u8,
/// **Legacy**: GEOSCOPE 24-bit integer encoding
GeoScope24 = raw::DE_GEOSCOPE24 as c_char,
GeoScope24 = raw::DE_GEOSCOPE24 as u8,
/// **Legacy**: GEOSCOPE 16-bit gain ranged, 3-bit exponent
GeoScope163 = raw::DE_GEOSCOPE163 as c_char,
GeoScope163 = raw::DE_GEOSCOPE163 as u8,
/// **Legacy**: GEOSCOPE 16-bit gain ranged, 4-bit exponent
GeoScope164 = raw::DE_GEOSCOPE164 as c_char,
GeoScope164 = raw::DE_GEOSCOPE164 as u8,
/// **Legacy**: CDSN 16-bit gain ranged
CDSN = raw::DE_CDSN as c_char,
CDSN = raw::DE_CDSN as u8,
/// **Legacy**: SRO 16-bit gain ranged
SRO = raw::DE_SRO as c_char,
SRO = raw::DE_SRO as u8,
/// **Legacy**: DWWSSN 16-bit gain ranged
DWWSSN = raw::DE_DWWSSN as c_char,
DWWSSN = raw::DE_DWWSSN as u8,
}

impl MSDataEncoding {
/// Create a `MSDataEncoding` from the given `ch`.
pub fn from_char(ch: c_char) -> MSResult<Self> {
match ch as c_uint {
pub fn from_char(ch: u8) -> MSResult<Self> {
match ch as u32 {
raw::DE_TEXT => Ok(Self::Text),
raw::DE_INT16 => Ok(Self::Integer16),
raw::DE_INT32 => Ok(Self::Integer32),
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,
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)) }
unsafe { check(raw::msr3_unpack_data(self.0, 0) as _) }
}

/// Returns the [FDSN source identifier](https://docs.fdsn.org/projects/source-identifiers/).
Expand All @@ -224,7 +224,7 @@ impl MSRecord {

/// Returns a lossy version of the [FDSN source identifier](https://docs.fdsn.org/projects/source-identifiers/).
pub fn sid_lossy(&self) -> String {
util::i8_to_string(&(self.ptr().sid))
util::to_string(&(self.ptr().sid))
}

/// Returns the network code identifier of the record.
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)
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))?) }
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 @@ -293,7 +293,7 @@ impl MSRecord {

/// Returns the data encoding format of the record.
pub fn encoding(&self) -> MSResult<MSDataEncoding> {
MSDataEncoding::from_char(self.ptr().encoding)
MSDataEncoding::from_char(self.ptr().encoding as _)
}

/// Returns the record publication version.
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
self.ptr().samplecnt as _
}

/// Returns the CRC of the record.
Expand Down Expand Up @@ -352,17 +352,17 @@ impl MSRecord {

/// Returns the number of (unpacked) data samples.
pub fn num_samples(&self) -> c_long {
self.ptr().numsamples
self.ptr().numsamples as _
}

/// Returns the record sample type.
pub fn sample_type(&self) -> MSSampleType {
MSSampleType::from_char(self.ptr().sampletype)
MSSampleType::from_char(self.ptr().sampletype as _)
}

/// Creates a new independently owned [`MSRecord`] from the underlying record.
pub fn try_clone(&self) -> MSResult<Self> {
let rv = unsafe { raw::msr3_duplicate(self.0, true as i8) };
let rv = unsafe { raw::msr3_duplicate(self.0, true as _) };

if rv.is_null() {
return Err(MSError::from_str("failed to duplicate"));
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,
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,
start_time as _,
MSTimeFormat::IsoMonthDayDoyZ,
MSSubSeconds::NanoMicro,
)
Expand Down
Loading

0 comments on commit d18b34d

Please sign in to comment.