Skip to content

Commit

Permalink
fix: change the type to c_char so it can be compiled for aarch64 (#337)
Browse files Browse the repository at this point in the history
* change the type to c_char so it can be compiled for aarch64
  • Loading branch information
cschin committed Oct 26, 2021
1 parent c7f68a7 commit a21aff2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
11 changes: 6 additions & 5 deletions src/bam/mod.rs
Expand Up @@ -16,6 +16,7 @@ pub mod record;
pub mod record_serde;

use std::ffi;
use std::os::raw::c_char;
use std::path::Path;
use std::rc::Rc;
use std::slice;
Expand Down Expand Up @@ -265,7 +266,7 @@ impl Reader {

// Invalidate the `text` representation of the header
unsafe {
let _ = htslib::sam_hdr_line_name(header, b"SQ".as_ptr().cast::<i8>(), 0);
let _ = htslib::sam_hdr_line_name(header, b"SQ".as_ptr().cast::<c_char>(), 0);
}

Ok(Reader {
Expand Down Expand Up @@ -940,10 +941,10 @@ impl Writer {
//println!("{}", str::from_utf8(&header_string).unwrap());
let rec = htslib::sam_hdr_parse(
((l_text + 1) as usize).try_into().unwrap(),
text as *const i8,
text as *const c_char,
);

(*rec).text = text as *mut i8;
(*rec).text = text as *mut c_char;
(*rec).l_text = l_text as u64;
rec
};
Expand Down Expand Up @@ -1209,8 +1210,8 @@ impl HeaderView {
header_string.len(),
);

let rec = htslib::sam_hdr_parse((l_text + 1) as u64, text as *const i8);
(*rec).text = text as *mut i8;
let rec = htslib::sam_hdr_parse((l_text + 1) as u64, text as *const c_char);
(*rec).text = text as *mut c_char;
(*rec).l_text = l_text as u64;
rec
};
Expand Down
33 changes: 17 additions & 16 deletions src/bam/record.rs
Expand Up @@ -9,6 +9,7 @@ use std::fmt;
use std::marker::PhantomData;
use std::mem::{size_of, MaybeUninit};
use std::ops;
use std::os::raw::c_char;
use std::rc::Rc;
use std::slice;
use std::str;
Expand Down Expand Up @@ -149,7 +150,7 @@ impl Record {
sam_copy.push(0);

let mut sam_string = htslib::kstring_t {
s: sam_copy.as_ptr() as *mut i8,
s: sam_copy.as_ptr() as *mut c_char,
l: sam_copy.len() as u64,
m: sam_copy.len() as u64,
};
Expand Down Expand Up @@ -580,7 +581,7 @@ impl Record {
let aux = unsafe {
htslib::bam_aux_get(
&self.inner as *const htslib::bam1_t,
c_str.as_ptr() as *mut i8,
c_str.as_ptr() as *mut c_char,
)
};
unsafe { Self::read_aux_field(aux).map(|(aux_field, _length)| aux_field) }
Expand Down Expand Up @@ -675,7 +676,7 @@ impl Record {
)
}
b'Z' | b'H' => {
let c_str = ffi::CStr::from_ptr(aux.offset(TYPE_ID_LEN).cast::<i8>());
let c_str = ffi::CStr::from_ptr(aux.offset(TYPE_ID_LEN).cast::<c_char>());
let rust_str = c_str.to_str().map_err(|_| Error::BamAuxParsingError)?;
(Aux::String(rust_str), c_str.to_bytes_with_nul().len())
}
Expand Down Expand Up @@ -790,70 +791,70 @@ impl Record {
return Err(Error::BamAuxTagAlreadyPresent);
}

let ctag = tag.as_ptr() as *mut i8;
let ctag = tag.as_ptr() as *mut c_char;
let ret = unsafe {
match value {
Aux::Char(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'A' as i8,
b'A' as c_char,
size_of::<u8>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Aux::I8(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'c' as i8,
b'c' as c_char,
size_of::<i8>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Aux::U8(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'C' as i8,
b'C' as c_char,
size_of::<u8>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Aux::I16(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b's' as i8,
b's' as c_char,
size_of::<i16>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Aux::U16(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'S' as i8,
b'S' as c_char,
size_of::<u16>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Aux::I32(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'i' as i8,
b'i' as c_char,
size_of::<i32>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Aux::U32(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'I' as i8,
b'I' as c_char,
size_of::<u32>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Aux::Float(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'f' as i8,
b'f' as c_char,
size_of::<f32>() as i32,
[v].as_mut_ptr() as *mut u8,
),
// Not part of specs but implemented in `htslib`:
Aux::Double(v) => htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'd' as i8,
b'd' as c_char,
size_of::<f64>() as i32,
[v].as_mut_ptr() as *mut u8,
),
Expand All @@ -862,7 +863,7 @@ impl Record {
htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'Z' as i8,
b'Z' as c_char,
(v.len() + 1) as i32,
c_str.as_ptr() as *mut u8,
)
Expand All @@ -872,7 +873,7 @@ impl Record {
htslib::bam_aux_append(
self.inner_ptr_mut(),
ctag,
b'H' as i8,
b'H' as c_char,
(v.len() + 1) as i32,
c_str.as_ptr() as *mut u8,
)
Expand Down Expand Up @@ -1006,7 +1007,7 @@ impl Record {
let aux = unsafe {
htslib::bam_aux_get(
&self.inner as *const htslib::bam1_t,
c_str.as_ptr() as *mut i8,
c_str.as_ptr() as *mut c_char,
)
};
unsafe {
Expand Down
11 changes: 6 additions & 5 deletions src/bcf/header.rs
Expand Up @@ -33,6 +33,7 @@
//! ```

use std::ffi;
use std::os::raw::c_char;
use std::slice;
use std::str;

Expand Down Expand Up @@ -115,7 +116,7 @@ impl Header {
htslib::bcf_hdr_subset(
header.inner,
samples.len() as i32,
name_pointers.as_ptr() as *const *mut i8,
name_pointers.as_ptr() as *const *mut c_char,
imap.as_mut_ptr() as *mut i32,
)
};
Expand Down Expand Up @@ -338,7 +339,7 @@ impl HeaderView {
match htslib::bcf_hdr_id2int(
self.inner,
htslib::BCF_DT_CTG as i32,
c_str.as_ptr() as *mut i8,
c_str.as_ptr() as *mut c_char,
) {
-1 => Err(Error::BcfUnknownContig {
contig: str::from_utf8(name).unwrap().to_owned(),
Expand All @@ -363,7 +364,7 @@ impl HeaderView {
let id = htslib::bcf_hdr_id2int(
self.inner,
htslib::BCF_DT_ID as i32,
c_str_tag.as_ptr() as *mut i8,
c_str_tag.as_ptr() as *mut c_char,
);
if id < 0 {
return Err(Error::BcfUndefinedTag { tag: tag_desc() });
Expand Down Expand Up @@ -400,7 +401,7 @@ impl HeaderView {
match htslib::bcf_hdr_id2int(
self.inner,
htslib::BCF_DT_ID as i32,
c_str.as_ptr() as *const i8,
c_str.as_ptr() as *const c_char,
) {
-1 => Err(Error::BcfUnknownID {
id: str::from_utf8(id).unwrap().to_owned(),
Expand Down Expand Up @@ -428,7 +429,7 @@ impl HeaderView {
match htslib::bcf_hdr_id2int(
self.inner,
htslib::BCF_DT_SAMPLE as i32,
c_str.as_ptr() as *const i8,
c_str.as_ptr() as *const c_char,
) {
-1 => Err(Error::BcfUnknownSample {
name: str::from_utf8(id).unwrap().to_owned(),
Expand Down
42 changes: 28 additions & 14 deletions src/bcf/record.rs
Expand Up @@ -10,6 +10,7 @@ use std::fmt;
use std::i32;
use std::marker::PhantomData;
use std::ops::Deref;
use std::os::raw::c_char;
use std::ptr;
use std::rc::Rc;
use std::slice;
Expand Down Expand Up @@ -316,7 +317,11 @@ impl Record {
pub fn set_id(&mut self, id: &[u8]) -> Result<()> {
let c_str = ffi::CString::new(id).unwrap();
if unsafe {
htslib::bcf_update_id(self.header().inner, self.inner, c_str.as_ptr() as *mut i8)
htslib::bcf_update_id(
self.header().inner,
self.inner,
c_str.as_ptr() as *mut c_char,
)
} == 0
{
Ok(())
Expand All @@ -329,7 +334,11 @@ impl Record {
pub fn clear_id(&mut self) -> Result<()> {
let c_str = ffi::CString::new(&b"."[..]).unwrap();
if unsafe {
htslib::bcf_update_id(self.header().inner, self.inner, c_str.as_ptr() as *mut i8)
htslib::bcf_update_id(
self.header().inner,
self.inner,
c_str.as_ptr() as *mut c_char,
)
} == 0
{
Ok(())
Expand All @@ -341,8 +350,13 @@ impl Record {
/// Add the ID string (the ID field is semicolon-separated), checking for duplicates.
pub fn push_id(&mut self, id: &[u8]) -> Result<()> {
let c_str = ffi::CString::new(id).unwrap();
if unsafe { htslib::bcf_add_id(self.header().inner, self.inner, c_str.as_ptr() as *mut i8) }
== 0
if unsafe {
htslib::bcf_add_id(
self.header().inner,
self.inner,
c_str.as_ptr() as *mut c_char,
)
} == 0
{
Ok(())
} else {
Expand Down Expand Up @@ -580,9 +594,9 @@ impl Record {
.iter()
.map(|vec| ffi::CString::new(*vec).unwrap())
.collect();
let mut ptrs: Vec<*const i8> = cstrings
let mut ptrs: Vec<*const c_char> = cstrings
.iter()
.map(|cstr| cstr.as_ptr() as *const i8)
.map(|cstr| cstr.as_ptr() as *const c_char)
.collect();
if unsafe {
htslib::bcf_update_alleles(
Expand Down Expand Up @@ -822,7 +836,7 @@ impl Record {
if htslib::bcf_update_format(
self.header().inner,
self.inner,
tag_c_str.as_ptr() as *mut i8,
tag_c_str.as_ptr() as *mut c_char,
data.as_ptr() as *const ::std::os::raw::c_void,
data.len() as i32,
ht as i32,
Expand Down Expand Up @@ -869,8 +883,8 @@ impl Record {
if htslib::bcf_update_format_string(
self.header().inner,
self.inner,
tag_c_str.as_ptr() as *mut i8,
c_ptrs.as_slice().as_ptr() as *mut *const i8,
tag_c_str.as_ptr() as *mut c_char,
c_ptrs.as_slice().as_ptr() as *mut *const c_char,
data.len() as i32,
) == 0
{
Expand Down Expand Up @@ -915,7 +929,7 @@ impl Record {
if htslib::bcf_update_info(
self.header().inner,
self.inner,
tag_c_str.as_ptr() as *mut i8,
tag_c_str.as_ptr() as *mut c_char,
data.as_ptr() as *const ::std::os::raw::c_void,
data.len() as i32,
ht as i32,
Expand Down Expand Up @@ -970,7 +984,7 @@ impl Record {
if htslib::bcf_update_info(
self.header().inner,
self.inner,
tag_c_str.as_ptr() as *mut i8,
tag_c_str.as_ptr() as *mut c_char,
c_str.as_ptr() as *const ::std::os::raw::c_void,
len as i32,
ht as i32,
Expand Down Expand Up @@ -1231,7 +1245,7 @@ impl<'a, 'b, B: BorrowMut<Buffer> + Borrow<Buffer> + 'b> Info<'a, B> {
htslib::bcf_get_info_values(
self.record.header().inner,
self.record.inner,
c_str.as_ptr() as *mut i8,
c_str.as_ptr() as *mut c_char,
&mut self.buffer.borrow_mut().inner,
&mut n,
data_type as i32,
Expand Down Expand Up @@ -1346,7 +1360,7 @@ impl<'a, 'b, B: BorrowMut<Buffer> + Borrow<Buffer> + 'b> Format<'a, B> {
htslib::bcf_get_fmt(
record.header().inner,
record.inner,
c_str.as_ptr() as *mut i8,
c_str.as_ptr() as *mut c_char,
)
};
Format {
Expand Down Expand Up @@ -1382,7 +1396,7 @@ impl<'a, 'b, B: BorrowMut<Buffer> + Borrow<Buffer> + 'b> Format<'a, B> {
htslib::bcf_get_format_values(
self.record.header().inner,
self.record.inner,
c_str.as_ptr() as *mut i8,
c_str.as_ptr() as *mut c_char,
&mut self.buffer.borrow_mut().inner,
&mut n,
data_type as i32,
Expand Down

0 comments on commit a21aff2

Please sign in to comment.