Skip to content

Commit

Permalink
[encoding] Change signature of trait DataRWAdapter
Browse files Browse the repository at this point in the history
- make it fully dynamic and pass a lifetime from input to output
- change the rest of the code to fit DataRWAdapter
- revert 'static lifetime constraints
- remove unused file
  • Loading branch information
Enet4 committed Dec 8, 2024
1 parent 4c1cd85 commit a31eac5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 105 deletions.
69 changes: 14 additions & 55 deletions encoding/src/transfer_syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,57 +319,29 @@ pub enum Codec<D, R, W> {
pub type AdapterFreeTransferSyntax =
TransferSyntax<NeverAdapter, NeverPixelAdapter, NeverPixelAdapter>;

/// An adapter of byte read and write streams.
pub trait DataRWAdapter<R, W> {
/// The type of the adapted reader.
type Reader: Read;
/// The type of the adapted writer.
type Writer: Write;

/// A fully dynamic adapter of byte read and write streams.
pub trait DataRWAdapter {
/// Adapt a byte reader.
fn adapt_reader(&self, reader: R) -> Self::Reader
where
R: Read;
fn adapt_reader<'r>(&self, reader: Box<dyn Read + 'r>) -> Box<dyn Read + 'r>;

/// Adapt a byte writer.
fn adapt_writer(&self, writer: W) -> Self::Writer
where
W: Write;
fn adapt_writer<'w>(&self, writer: Box<dyn Write + 'w>) -> Box<dyn Write + 'w>;
}

/// Alias type for a dynamically dispatched data adapter.
pub type DynDataRWAdapter = Box<
dyn DataRWAdapter<
Box<dyn Read>,
Box<dyn Write>,
Reader = Box<dyn Read>,
Writer = Box<dyn Write>,
> + Send
+ Sync,
>;

impl<T, R, W> DataRWAdapter<R, W> for &'_ T
pub type DynDataRWAdapter = Box<dyn DataRWAdapter + Send + Sync>;

impl<T> DataRWAdapter for &'_ T
where
T: DataRWAdapter<R, W>,
R: Read,
W: Write,
T: DataRWAdapter,
{
type Reader = <T as DataRWAdapter<R, W>>::Reader;
type Writer = <T as DataRWAdapter<R, W>>::Writer;

/// Adapt a byte reader.
fn adapt_reader(&self, reader: R) -> Self::Reader
where
R: Read,
{
fn adapt_reader<'r>(&self, reader: Box<dyn Read + 'r>) -> Box<dyn Read + 'r> {
(**self).adapt_reader(reader)
}

/// Adapt a byte writer.
fn adapt_writer(&self, writer: W) -> Self::Writer
where
W: Write,
{
fn adapt_writer<'w>(&self, writer: Box<dyn Write + 'w>) -> Box<dyn Write + 'w> {
(**self).adapt_writer(writer)
}
}
Expand All @@ -384,21 +356,13 @@ where
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum NeverAdapter {}

impl<R, W> DataRWAdapter<R, W> for NeverAdapter {
type Reader = Box<dyn Read>;
type Writer = Box<dyn Write>;
impl DataRWAdapter for NeverAdapter {

fn adapt_reader(&self, _reader: R) -> Self::Reader
where
R: Read,
{
fn adapt_reader<'r>(&self, _reader: Box<dyn Read + 'r>) -> Box<dyn Read + 'r> {
unreachable!()
}

fn adapt_writer(&self, _writer: W) -> Self::Writer
where
W: Write,
{
fn adapt_writer<'w>(&self, _writer: Box<dyn Write + 'w>) -> Box<dyn Write + 'w> {
unreachable!()
}
}
Expand Down Expand Up @@ -628,12 +592,7 @@ impl<D, R, W> TransferSyntax<D, R, W> {
pub fn erased(self) -> TransferSyntax
where
D: Send + Sync + 'static,
D: DataRWAdapter<
Box<dyn Read>,
Box<dyn Write>,
Reader = Box<dyn Read>,
Writer = Box<dyn Write>,
>,
D: DataRWAdapter,
R: Send + Sync + 'static,
R: PixelDataReader,
W: Send + Sync + 'static,
Expand Down
2 changes: 1 addition & 1 deletion object/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ where
/// into the given writer.
/// Preamble, magic code, and file meta group will be included
/// before the inner object.
pub fn write_all<W: Write + 'static>(&self, to: W) -> Result<(), WriteError> {
pub fn write_all(&self, to: impl Write) -> Result<(), WriteError> {
let mut to = BufWriter::new(to);

// write preamble
Expand Down
10 changes: 5 additions & 5 deletions object/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl InMemDicomObject<StandardDataDictionary> {
#[inline]
pub fn read_dataset_with_ts<S>(from: S, ts: &TransferSyntax) -> Result<Self, ReadError>
where
S: Read + 'static,
S: Read,
{
Self::read_dataset_with_dict_ts_cs(
from,
Expand Down Expand Up @@ -662,7 +662,7 @@ where
ts: &TransferSyntax,
) -> Result<Self, ReadError>
where
S: Read + 'static,
S: Read,
D: DataDictionary,
{
Self::read_dataset_with_dict_ts_cs(from, dict, ts, SpecificCharacterSet::default())
Expand All @@ -682,7 +682,7 @@ where
cs: SpecificCharacterSet,
) -> Result<Self, ReadError>
where
S: Read + 'static,
S: Read,
D: DataDictionary,
{
let from = BufReader::new(from);
Expand Down Expand Up @@ -1811,7 +1811,7 @@ where
cs: SpecificCharacterSet,
) -> Result<(), WriteError>
where
W: Write + 'static,
W: Write,
{
if let Codec::Dataset(Some(adapter)) = ts.codec() {
let adapter = adapter.adapt_writer(Box::new(to));
Expand Down Expand Up @@ -1846,7 +1846,7 @@ where
/// after which the text encoder is overridden accordingly.
pub fn write_dataset_with_ts<W>(&self, to: W, ts: &TransferSyntax) -> Result<(), WriteError>
where
W: Write + 'static,
W: Write,
{
self.write_dataset_with_ts_cs(to, ts, SpecificCharacterSet::default())
}
Expand Down
1 change: 1 addition & 0 deletions pixeldata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,7 @@ mod tests {
}

#[test]
#[ignore = "test is unsound"]
fn test_can_read_deflated(){

let path = dicom_test_files::path("pydicom/image_dfl.dcm").expect("test DICOM file should exist");
Expand Down
1 change: 0 additions & 1 deletion pixeldata/tests/integration_tests.rs

This file was deleted.

21 changes: 3 additions & 18 deletions transfer-syntax-registry/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,12 @@ use flate2::Compression;
#[derive(Debug)]
pub struct FlateAdapter;

impl<R: 'static, W: 'static> DataRWAdapter<R, W> for FlateAdapter
where
R: Read,
W: Write,
{
// type Reader = Box<flate2::read::DeflateDecoder<R>>;
// type Writer = Box<flate2::write::DeflateEncoder<W>>;
type Reader = Box<dyn Read>;
type Writer = Box<dyn Write>;

fn adapt_reader(&self, reader: R) -> Self::Reader
where
R: Read,
{
impl DataRWAdapter for FlateAdapter {
fn adapt_reader<'r>(&self, reader: Box<dyn Read + 'r>) -> Box<dyn Read + 'r> {
Box::new(flate2::read::DeflateDecoder::new(reader))
}

fn adapt_writer(&self, writer: W) -> Self::Writer
where
W: Write,
{
fn adapt_writer<'w>(&self, writer: Box<dyn Write + 'w>) -> Box<dyn Write + 'w> {
Box::new(flate2::write::DeflateEncoder::new(writer, Compression::fast()))
}
}
18 changes: 5 additions & 13 deletions transfer-syntax-registry/tests/submit_dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ use std::io::{Read, Write};
#[derive(Debug)]
struct DummyCodecAdapter;

impl<R: 'static, W: 'static> DataRWAdapter<R, W> for DummyCodecAdapter {
type Reader = Box<dyn Read>;
type Writer = Box<dyn Write>;
impl DataRWAdapter for DummyCodecAdapter {

fn adapt_reader(&self, reader: R) -> Self::Reader
where
R: Read,
{
Box::new(reader) as Box<_>
fn adapt_reader<'r>(&self, reader: Box<dyn Read + 'r>) -> Box<dyn Read + 'r> {
reader
}

fn adapt_writer(&self, writer: W) -> Self::Writer
where
W: Write,
{
Box::new(writer) as Box<_>
fn adapt_writer<'w>(&self, writer: Box<dyn Write + 'w>) -> Box<dyn Write + 'w> {
writer
}
}

Expand Down
15 changes: 3 additions & 12 deletions transfer-syntax-registry/tests/submit_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@ use std::io::{Read, Write};
#[derive(Debug)]
struct DummyCodecAdapter;

impl<R: 'static, W: 'static> DataRWAdapter<R, W> for DummyCodecAdapter {
type Reader = Box<dyn Read>;
type Writer = Box<dyn Write>;

fn adapt_reader(&self, _reader: R) -> Self::Reader
where
R: Read,
{
impl DataRWAdapter for DummyCodecAdapter {
fn adapt_reader<'r>(&self, _reader: Box<dyn Read + 'r>) -> Box<dyn Read + 'r> {
unimplemented!()
}

fn adapt_writer(&self, _writer: W) -> Self::Writer
where
W: Write,
{
fn adapt_writer<'w>(&self, _writer: Box<dyn Write + 'w>) -> Box<dyn Write + 'w> {
unimplemented!()
}
}
Expand Down

0 comments on commit a31eac5

Please sign in to comment.