Skip to content

Commit

Permalink
use enum instead of dyn to next_interval
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Jul 21, 2023
1 parent 6ee52cb commit 209c6dc
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions benches/random_intervals.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bedder::chrom_ordering::parse_genome;
use bedder::intersection::IntersectionIterator;
use bedder::interval::Interval;
use bedder::position::{Position, Positioned, PositionedIterator};
use bedder::position::{Position, PositionedIterator};
use bedder::string::String;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl PositionedIterator for Intervals {
String::from(format!("{}:{}", self.name, self.i))
}

fn next_position(&mut self, _q: Option<&dyn Positioned>) -> Option<io::Result<Position>> {
fn next_position(&mut self, _q: Option<&Position>) -> Option<io::Result<Position>> {
if self.i < self.n_intervals {
self.i += 1;
let r: f64 = self.rng.gen();
Expand Down
1 change: 0 additions & 1 deletion examples/intersect_bed_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use clap::Parser;
extern crate bedder;
use crate::bedder::chrom_ordering::parse_genome;
use crate::bedder::intersection::IntersectionIterator;
use crate::bedder::position::Positioned;

#[derive(Parser, Debug)]
struct Args {
Expand Down
2 changes: 1 addition & 1 deletion src/bedder_bed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
{
fn next_position(
&mut self,
_q: Option<&dyn crate::position::Positioned>,
_q: Option<&crate::position::Position>,
) -> Option<std::result::Result<Position, std::io::Error>> {
self.buf.clear();
loop {
Expand Down
4 changes: 2 additions & 2 deletions src/bedder_vcf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Positioned for vcf::record::Record {
impl<'a> crate::position::PositionedIterator for BedderVCF<'a> {
fn next_position(
&mut self,
_q: Option<&dyn crate::position::Positioned>,
_q: Option<&crate::position::Position>,
) -> Option<std::result::Result<Position, std::io::Error>> {
let mut v = vcf::Record::default();

Expand All @@ -177,7 +177,7 @@ impl<'a> crate::position::PositionedIterator for BedderVCF<'a> {
}
}
fn name(&self) -> String {
String::from("vcf")
String::from("vcf line number:") + &self.record_number.to_string()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io::{Error, ErrorKind};
use std::rc::Rc;
//use std::sync::Arc as Rc;

use crate::position::{Position, Positioned, PositionedIterator};
use crate::position::{Position, PositionedIterator};

/// An iterator that returns the intersection of multiple iterators.
pub struct IntersectionIterator<'a> {
Expand Down Expand Up @@ -299,7 +299,7 @@ impl<'a> IntersectionIterator<'a> {
.expect("expected interval iterator at file index");
// for a given base_interval, we make sure to call next_position with Some, only once.
// subsequent calls will be with None.
let arg: Option<&dyn Positioned> = if !self.called[file_index] {
let arg: Option<&Position> = if !self.called[file_index] {
self.called[file_index] = true;
Some(base_interval.as_ref())
} else {
Expand Down Expand Up @@ -392,7 +392,7 @@ mod tests {
String::from(format!("{}:{}", self.name, self.i))
}

fn next_position(&mut self, _o: Option<&dyn Positioned>) -> Option<io::Result<Position>> {
fn next_position(&mut self, _o: Option<&Position>) -> Option<io::Result<Position>> {
if self.i >= self.ivs.len() {
return None;
}
Expand Down
12 changes: 6 additions & 6 deletions src/interval.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::position::{Field, FieldError, Positioned, Value};
use crate::position::{Field, FieldError, Value};
use crate::string::String;
/// Interval type is a simple struct that can be used as a default interval type.
/// It has a chromosome, start, and stop field along with a (linear) HashMap of Values.
Expand All @@ -13,22 +13,22 @@ pub struct Interval {
pub fields: LinearMap<String, Value>,
}

impl Positioned for Interval {
impl Interval {
#[inline]
fn start(&self) -> u64 {
pub fn start(&self) -> u64 {
self.start
}
#[inline]
fn stop(&self) -> u64 {
pub fn stop(&self) -> u64 {
self.stop
}
#[inline]
fn chrom(&self) -> &str {
pub fn chrom(&self) -> &str {
&self.chrom
}

#[inline]
fn value(&self, f: Field) -> Result<Value, FieldError> {
pub fn value(&self, f: Field) -> Result<Value, FieldError> {
match f {
Field::String(name) => match self.fields.get(&name) {
None => Err(FieldError::InvalidFieldName(name)),
Expand Down
12 changes: 6 additions & 6 deletions src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ pub enum Position {
Other(Box<dyn Positioned>),
}

impl Positioned for Position {
impl Position {
#[inline]
fn chrom(&self) -> &str {
pub fn chrom(&self) -> &str {
match self {
Position::Bed(b) => b.chrom(),
Position::Vcf(v) => v.chrom(),
Expand All @@ -78,7 +78,7 @@ impl Positioned for Position {
}

#[inline]
fn start(&self) -> u64 {
pub fn start(&self) -> u64 {
match self {
Position::Bed(b) => b.start(),
Position::Vcf(v) => v.start(),
Expand All @@ -89,7 +89,7 @@ impl Positioned for Position {
}

#[inline]
fn stop(&self) -> u64 {
pub fn stop(&self) -> u64 {
match self {
Position::Bed(b) => b.stop(),
Position::Vcf(v) => v.stop(),
Expand All @@ -100,7 +100,7 @@ impl Positioned for Position {
}

#[inline]
fn value(&self, f: Field) -> result::Result<Value, FieldError> {
pub fn value(&self, f: Field) -> result::Result<Value, FieldError> {
match self {
Position::Bed(b) => b.value(f),
Position::Vcf(v) => v.value(f),
Expand Down Expand Up @@ -155,6 +155,6 @@ pub trait PositionedIterator {
/// returned position (Positioned equal to previously returned position should have already been returned).
fn next_position(
&mut self,
q: Option<&dyn Positioned>,
q: Option<&Position>,
) -> Option<std::result::Result<Position, io::Error>>;
}

0 comments on commit 209c6dc

Please sign in to comment.