Skip to content

Commit

Permalink
vcf/variant/io/read: Change Read::variant_records to return an iterat…
Browse files Browse the repository at this point in the history
…or over Record trait objects
  • Loading branch information
zaeleus committed Mar 18, 2024
1 parent 20cf3e9 commit fc124c4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
10 changes: 7 additions & 3 deletions noodles-bcf/src/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,13 @@ where

fn variant_records<'r, 'h: 'r>(
&'r mut self,
header: &'h vcf::Header,
) -> Box<dyn Iterator<Item = io::Result<RecordBuf>> + 'r> {
Box::new(self.record_bufs(header))
_: &'h vcf::Header,
) -> Box<dyn Iterator<Item = io::Result<Box<dyn vcf::variant::Record>>> + 'r> {
Box::new(
self.records().map(|result| {
result.map(|record| Box::new(record) as Box<dyn vcf::variant::Record>)
}),
)
}
}

Expand Down
3 changes: 3 additions & 0 deletions noodles-util/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* util/variant: Move readers (`Reader` and `IndexedReader`) and writer
(`Writer`) to `io` module.

* util/variant/io/reader: Change `Reader::records` to return an iterator
over `vcf::variant::Record` trait objects.

* util/variant/io/writer: Change `Writer::write_record` to accept `&dyn
vcf::variant::Record`.

Expand Down
2 changes: 1 addition & 1 deletion noodles-util/examples/util_variant_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() -> io::Result<()> {

for result in reader.records(&header) {
let record = result?;
writer.write_record(&header, &record)?;
writer.write_record(&header, record.as_ref())?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion noodles-util/examples/util_variant_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> io::Result<()> {

for result in reader.records(&header) {
let record = result?;
writer.write_variant_record(&header, &record)?;
writer.write_variant_record(&header, record.as_ref())?;
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions noodles-util/src/variant/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use self::builder::Builder;

use std::io::{self, BufRead};

use noodles_vcf::{self as vcf, variant::RecordBuf};
use noodles_vcf::{self as vcf, variant::Record};

/// A variant reader.
pub struct Reader<R> {
Expand Down Expand Up @@ -69,7 +69,7 @@ where
pub fn records<'a>(
&'a mut self,
header: &'a vcf::Header,
) -> impl Iterator<Item = io::Result<RecordBuf>> + 'a {
) -> impl Iterator<Item = io::Result<Box<dyn Record>>> + 'a {
self.inner.variant_records(header)
}
}
10 changes: 7 additions & 3 deletions noodles-vcf/src/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,13 @@ where

fn variant_records<'r, 'h: 'r>(
&'r mut self,
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<RecordBuf>> + 'r> {
Box::new(self.record_bufs(header))
_: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<Box<dyn crate::variant::Record>>> + 'r> {
Box::new(
self.records().map(|result| {
result.map(|record| Box::new(record) as Box<dyn crate::variant::Record>)
}),
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions noodles-vcf/src/variant/io/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;

use crate::{variant::RecordBuf, Header};
use crate::{variant::Record, Header};

/// A variant format reader.
pub trait Read<R> {
Expand All @@ -11,5 +11,5 @@ pub trait Read<R> {
fn variant_records<'r, 'h: 'r>(
&'r mut self,
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<RecordBuf>> + 'r>;
) -> Box<dyn Iterator<Item = io::Result<Box<dyn Record>>> + 'r>;
}

0 comments on commit fc124c4

Please sign in to comment.