Skip to content

Commit 7e96bee

Browse files
Merge branch 'master' into use-arcs-in-records
2 parents 907dabb + 9680bb4 commit 7e96bee

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

src/bam/buffer.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
use std::collections::{vec_deque, VecDeque};
77
use std::mem;
8-
use std::rc::Rc;
98
use std::str;
9+
use std::sync::Arc;
1010

1111
use crate::bam;
1212
use crate::bam::Read;
@@ -19,11 +19,11 @@ use crate::errors::{Error, Result};
1919
#[derive(Debug)]
2020
pub struct RecordBuffer {
2121
reader: bam::IndexedReader,
22-
inner: VecDeque<Rc<bam::Record>>,
23-
overflow: Option<Rc<bam::Record>>,
22+
inner: VecDeque<Arc<bam::Record>>,
23+
overflow: Option<Arc<bam::Record>>,
2424
cache_cigar: bool,
2525
min_refetch_distance: u64,
26-
buffer_record: Rc<bam::Record>,
26+
buffer_record: Arc<bam::Record>,
2727
start_pos: Option<u64>,
2828
}
2929

@@ -44,7 +44,7 @@ impl RecordBuffer {
4444
overflow: None,
4545
cache_cigar,
4646
min_refetch_distance: 1,
47-
buffer_record: Rc::new(bam::Record::new()),
47+
buffer_record: Arc::new(bam::Record::new()),
4848
start_pos: Some(0),
4949
}
5050
}
@@ -113,7 +113,7 @@ impl RecordBuffer {
113113
loop {
114114
match self
115115
.reader
116-
.read(Rc::get_mut(&mut self.buffer_record).unwrap())
116+
.read(Arc::get_mut(&mut self.buffer_record).unwrap())
117117
{
118118
None => break,
119119
Some(res) => res?,
@@ -132,10 +132,11 @@ impl RecordBuffer {
132132

133133
// Record is kept, do not reuse it for next iteration
134134
// and thus create a new one.
135-
let mut record = mem::replace(&mut self.buffer_record, Rc::new(bam::Record::new()));
135+
let mut record =
136+
mem::replace(&mut self.buffer_record, Arc::new(bam::Record::new()));
136137

137138
if self.cache_cigar {
138-
Rc::get_mut(&mut record).unwrap().cache_cigar();
139+
Arc::get_mut(&mut record).unwrap().cache_cigar();
139140
}
140141

141142
if pos >= end as i64 {
@@ -157,12 +158,12 @@ impl RecordBuffer {
157158
}
158159

159160
/// Iterate over records that have been fetched with `fetch`.
160-
pub fn iter(&'_ self) -> vec_deque::Iter<'_, Rc<bam::Record>> {
161+
pub fn iter(&self) -> vec_deque::Iter<'_, Arc<bam::Record>> {
161162
self.inner.iter()
162163
}
163164

164165
/// Iterate over mutable references to records that have been fetched with `fetch`.
165-
pub fn iter_mut(&'_ mut self) -> vec_deque::IterMut<'_, Rc<bam::Record>> {
166+
pub fn iter_mut(&mut self) -> vec_deque::IterMut<'_, Arc<bam::Record>> {
166167
self.inner.iter_mut()
167168
}
168169

src/bam/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,7 @@ CCCCCCCCCCCCCCCCCCC"[..],
16921692
// fix qual offset
16931693
let qual: Vec<u8> = quals[i].iter().map(|&q| q - 33).collect();
16941694
assert_eq!(rec.qual(), &qual[..]);
1695+
assert_eq!(rec.aux(b"X"), Err(Error::BamAuxStringError));
16951696
assert_eq!(rec.aux(b"NotAvailableAux"), Err(Error::BamAuxTagNotFound));
16961697
}
16971698

@@ -2006,7 +2007,8 @@ CCCCCCCCCCCCCCCCCCC"[..],
20062007
rec.remove_aux(b"YT").unwrap();
20072008
}
20082009

2009-
assert!(rec.remove_aux(b"ab").is_err());
2010+
assert_eq!(rec.remove_aux(b"X"), Err(Error::BamAuxStringError));
2011+
assert_eq!(rec.remove_aux(b"ab"), Err(Error::BamAuxTagNotFound));
20102012

20112013
assert_eq!(rec.aux(b"XS"), Err(Error::BamAuxTagNotFound));
20122014
assert_eq!(rec.aux(b"YT"), Err(Error::BamAuxTagNotFound));

src/bam/record.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,13 @@ impl Record {
644644
/// Only the first two bytes of a given tag are used for the look-up of a field.
645645
/// See [`Aux`] for more details.
646646
pub fn aux(&self, tag: &[u8]) -> Result<Aux<'_>> {
647-
let c_str = ffi::CString::new(tag).map_err(|_| Error::BamAuxStringError)?;
647+
if tag.len() < 2 {
648+
return Err(Error::BamAuxStringError);
649+
}
648650
let aux = unsafe {
649651
htslib::bam_aux_get(
650652
&self.inner as *const htslib::bam1_t,
651-
c_str.as_ptr() as *mut c_char,
653+
tag.as_ptr() as *const c_char,
652654
)
653655
};
654656
unsafe { Self::read_aux_field(aux).map(|(aux_field, _length)| aux_field) }
@@ -1224,11 +1226,13 @@ impl Record {
12241226

12251227
// Delete auxiliary tag.
12261228
pub fn remove_aux(&mut self, tag: &[u8]) -> Result<()> {
1227-
let c_str = ffi::CString::new(tag).map_err(|_| Error::BamAuxStringError)?;
1229+
if tag.len() < 2 {
1230+
return Err(Error::BamAuxStringError);
1231+
}
12281232
let aux = unsafe {
12291233
htslib::bam_aux_get(
12301234
&self.inner as *const htslib::bam1_t,
1231-
c_str.as_ptr() as *mut c_char,
1235+
tag.as_ptr() as *const c_char,
12321236
)
12331237
};
12341238
unsafe {

src/bcf/header.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,15 +519,8 @@ impl HeaderView {
519519
/// Create an empty record using this header view.
520520
///
521521
/// The record can be reused multiple times.
522-
///
523-
/// # Performance
524-
///
525-
/// This is quite slow and resource-intensive as it clones the actual
526-
/// header, instead of the reference to it. Therefore, whenever possible
527-
/// / feasible you should use [`Record::new`](crate::bcf::Record::new) with
528-
/// a reference to your header.
529-
pub fn empty_record(&self) -> crate::bcf::Record {
530-
crate::bcf::Record::new(Arc::new(self.clone()))
522+
pub fn empty_record(self: &Arc<Self>) -> crate::bcf::Record {
523+
crate::bcf::Record::new(self.clone())
531524
}
532525
}
533526

0 commit comments

Comments
 (0)