@@ -11,27 +11,21 @@ use dicom_core::Tag;
11
11
use std:: io:: Read ;
12
12
13
13
pub mod basic;
14
- pub mod erased ;
14
+ pub mod primitive_value ;
15
15
16
16
/** Obtain the default data element decoder.
17
17
* According to the standard, data elements are encoded in Implicit
18
18
* VR Little Endian by default.
19
19
*/
20
- pub fn get_default_reader < S > ( ) -> StandardImplicitVRLittleEndianDecoder < S >
21
- where
22
- S : Read ,
23
- {
20
+ pub fn default_reader ( ) -> StandardImplicitVRLittleEndianDecoder {
24
21
ImplicitVRLittleEndianDecoder :: default ( )
25
22
}
26
23
27
24
/** Obtain a data element decoder for reading the data elements in a DICOM
28
25
* file's Meta information. According to the standard, these are always
29
26
* encoded in Explicit VR Little Endian.
30
27
*/
31
- pub fn get_file_header_decoder < S > ( ) -> ExplicitVRLittleEndianDecoder < S >
32
- where
33
- S : Read ,
34
- {
28
+ pub fn file_header_decoder ( ) -> ExplicitVRLittleEndianDecoder {
35
29
ExplicitVRLittleEndianDecoder :: default ( )
36
30
}
37
31
@@ -248,9 +242,6 @@ where
248
242
* may depend on the transfer syntax.
249
243
*/
250
244
pub trait Decode {
251
- /// The data source's type.
252
- type Source : ?Sized + Read ;
253
-
254
245
/** Fetch and decode the next data element header from the given source.
255
246
* This method returns only the header of the element. At the end of this operation, the source
256
247
* will be pointing at the element's value data, which should be read or skipped as necessary.
@@ -260,34 +251,47 @@ pub trait Decode {
260
251
*
261
252
* Returns the expected header and the exact number of bytes read from the source.
262
253
*/
263
- fn decode_header ( & self , source : & mut Self :: Source ) -> Result < ( DataElementHeader , usize ) > ;
254
+ fn decode_header < S > ( & self , source : & mut S ) -> Result < ( DataElementHeader , usize ) >
255
+ where
256
+ S : ?Sized + Read ;
264
257
265
258
/** Fetch and decode the next sequence item head from the given source. It is a separate method
266
259
* because value representation is always implicit when reading item headers and delimiters.
267
260
* This method returns only the header of the item. At the end of this operation, the source
268
261
* will be pointing at the beginning of the item's data, which should be traversed if necessary.
269
262
*/
270
- fn decode_item_header ( & self , source : & mut Self :: Source ) -> Result < SequenceItemHeader > ;
263
+ fn decode_item_header < S > ( & self , source : & mut S ) -> Result < SequenceItemHeader >
264
+ where
265
+ S : ?Sized + Read ;
271
266
272
267
/// Decode a DICOM attribute tag from the given source.
273
- fn decode_tag ( & self , source : & mut Self :: Source ) -> Result < Tag > ;
268
+ fn decode_tag < S > ( & self , source : & mut S ) -> Result < Tag >
269
+ where
270
+ S : ?Sized + Read ;
274
271
}
275
272
276
273
impl < T : ?Sized > Decode for Box < T >
277
274
where
278
275
T : Decode ,
279
276
{
280
- type Source = <T as Decode >:: Source ;
281
-
282
- fn decode_header ( & self , source : & mut Self :: Source ) -> Result < ( DataElementHeader , usize ) > {
277
+ fn decode_header < S > ( & self , source : & mut S ) -> Result < ( DataElementHeader , usize ) >
278
+ where
279
+ S : ?Sized + Read ,
280
+ {
283
281
( * * self ) . decode_header ( source)
284
282
}
285
283
286
- fn decode_item_header ( & self , source : & mut Self :: Source ) -> Result < SequenceItemHeader > {
284
+ fn decode_item_header < S > ( & self , source : & mut S ) -> Result < SequenceItemHeader >
285
+ where
286
+ S : ?Sized + Read ,
287
+ {
287
288
( * * self ) . decode_item_header ( source)
288
289
}
289
290
290
- fn decode_tag ( & self , source : & mut Self :: Source ) -> Result < Tag > {
291
+ fn decode_tag < S > ( & self , source : & mut S ) -> Result < Tag >
292
+ where
293
+ S : ?Sized + Read ,
294
+ {
291
295
( * * self ) . decode_tag ( source)
292
296
}
293
297
}
@@ -296,17 +300,108 @@ impl<'a, T: ?Sized> Decode for &'a T
296
300
where
297
301
T : Decode ,
298
302
{
299
- type Source = <T as Decode >:: Source ;
303
+ fn decode_header < S > ( & self , source : & mut S ) -> Result < ( DataElementHeader , usize ) >
304
+ where
305
+ S : ?Sized + Read ,
306
+ {
307
+ ( * * self ) . decode_header ( source)
308
+ }
309
+
310
+ fn decode_item_header < S > ( & self , source : & mut S ) -> Result < SequenceItemHeader >
311
+ where
312
+ S : ?Sized + Read ,
313
+ {
314
+ ( * * self ) . decode_item_header ( source)
315
+ }
316
+
317
+ fn decode_tag < S > ( & self , source : & mut S ) -> Result < Tag >
318
+ where
319
+ S : ?Sized + Read ,
320
+ {
321
+ ( * * self ) . decode_tag ( source)
322
+ }
323
+ }
324
+
325
+ /** Type trait for reading and decoding DICOM data elements from a specific source
326
+ * reader type.
327
+ *
328
+ * The specific behaviour of decoding, even when abstracted from the original source,
329
+ * may depend on the transfer syntax.
330
+ */
331
+ pub trait DecodeFrom < S : ?Sized + Read > {
332
+ /** Fetch and decode the next data element header from the given source.
333
+ * This method returns only the header of the element. At the end of this operation, the source
334
+ * will be pointing at the element's value data, which should be read or skipped as necessary.
335
+ *
336
+ * Decoding an item or sequence delimiter is considered valid, and so should be properly handled
337
+ * by the decoder. The value representation in this case should be `UN`.
338
+ *
339
+ * Returns the expected header and the exact number of bytes read from the source.
340
+ */
341
+ fn decode_header ( & self , source : & mut S ) -> Result < ( DataElementHeader , usize ) > ;
342
+
343
+ /** Fetch and decode the next sequence item head from the given source. It is a separate method
344
+ * because value representation is always implicit when reading item headers and delimiters.
345
+ * This method returns only the header of the item. At the end of this operation, the source
346
+ * will be pointing at the beginning of the item's data, which should be traversed if necessary.
347
+ */
348
+ fn decode_item_header ( & self , source : & mut S ) -> Result < SequenceItemHeader > ;
349
+
350
+ /// Decode a DICOM attribute tag from the given source.
351
+ fn decode_tag ( & self , source : & mut S ) -> Result < Tag > ;
352
+ }
353
+
354
+ impl < S : ?Sized , T : ?Sized > DecodeFrom < S > for & T
355
+ where
356
+ S : Read ,
357
+ T : DecodeFrom < S > ,
358
+ {
359
+ fn decode_header ( & self , source : & mut S ) -> Result < ( DataElementHeader , usize ) > {
360
+ ( * * self ) . decode_header ( source)
361
+ }
362
+
363
+ fn decode_item_header ( & self , source : & mut S ) -> Result < SequenceItemHeader > {
364
+ ( * * self ) . decode_item_header ( source)
365
+ }
300
366
301
- fn decode_header ( & self , source : & mut Self :: Source ) -> Result < ( DataElementHeader , usize ) > {
367
+ fn decode_tag ( & self , source : & mut S ) -> Result < Tag > {
368
+ ( * * self ) . decode_tag ( source)
369
+ }
370
+ }
371
+
372
+ impl < S : ?Sized , T : ?Sized > DecodeFrom < S > for Box < T >
373
+ where
374
+ S : Read ,
375
+ T : DecodeFrom < S > ,
376
+ {
377
+ fn decode_header ( & self , source : & mut S ) -> Result < ( DataElementHeader , usize ) > {
302
378
( * * self ) . decode_header ( source)
303
379
}
304
380
305
- fn decode_item_header ( & self , source : & mut Self :: Source ) -> Result < SequenceItemHeader > {
381
+ fn decode_item_header ( & self , source : & mut S ) -> Result < SequenceItemHeader > {
306
382
( * * self ) . decode_item_header ( source)
307
383
}
308
384
309
- fn decode_tag ( & self , source : & mut Self :: Source ) -> Result < Tag > {
385
+ fn decode_tag ( & self , source : & mut S ) -> Result < Tag > {
310
386
( * * self ) . decode_tag ( source)
311
387
}
312
388
}
389
+
390
+ #[ cfg( test) ]
391
+ mod tests {
392
+ use super :: * ;
393
+
394
+ fn is_decode_from < T : DecodeFrom < dyn Read > > ( _decoder : & T ) { }
395
+
396
+ #[ allow( unused) ]
397
+ fn boxed_decoder_from_is_decoder_from < T > ( decoder : T )
398
+ where
399
+ T : DecodeFrom < dyn Read > ,
400
+ {
401
+ is_decode_from ( & decoder) ;
402
+ let boxed = Box :: new ( decoder) ;
403
+ is_decode_from ( & boxed) ;
404
+ let erased = boxed as Box < dyn DecodeFrom < dyn Read > > ;
405
+ is_decode_from ( & erased) ;
406
+ }
407
+ }
0 commit comments