io_utils/
io_utils.rs

1#![allow(dead_code)]
2#![allow(clippy::type_complexity)]
3
4use std::{
5    any::type_name,
6    cmp::min,
7    mem,
8    fmt::{self, Debug, Display, Formatter},
9    io::{self, Read, Seek, Write, Cursor, SeekFrom},
10    rc::Rc,
11    cell::RefCell,
12    ops::{Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeTo, RangeFull}
13};
14
15/// * The `Reader` trait, `Read + Seek + Debug`
16pub trait Reader: Read + Seek + Debug {}
17impl<T> Reader for T where T: Read + Seek + Debug {}
18
19/// * The `Writer` trait, `Write + Seek + Debug`
20pub trait Writer: Write + Seek + Debug {}
21impl<T> Writer for T where T: Write + Seek + Debug {}
22
23/// * The `ReadWrite` trait, `Read + Write + Seek + Debug`
24pub trait ReadWrite: Read + Write + Seek + Debug {}
25impl<T> ReadWrite for T where T: Read + Write + Seek + Debug {}
26
27/// * Encapsulated shared `Read + Seek + Debug`
28#[derive(Debug)]
29pub struct SharedReader<T> (Rc<RefCell<T>>) where T: Read + Seek + Debug;
30
31impl<T> SharedReader<T>
32where
33    T: Read + Seek + Debug {
34    pub fn new(reader: T) -> Self {
35        Self(Rc::new(RefCell::new(reader)))
36    }
37}
38
39impl<T> Read for SharedReader<T>
40where
41    T: Read + Seek + Debug {
42    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
43        self.0.borrow_mut().read(buf)
44    }
45}
46
47impl<T> Seek for SharedReader<T>
48where
49    T: Read + Seek + Debug {
50    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
51        self.0.borrow_mut().seek(pos)
52    }
53}
54
55impl<T> Clone for SharedReader<T>
56where
57    T: Read + Seek + Debug {
58    fn clone(&self) -> Self {
59        Self(self.0.clone())
60    }
61}
62
63/// * Encapsulated shared `Write + Seek + Debug`
64#[derive(Debug)]
65pub struct SharedWriter<T> (Rc<RefCell<T>>) where T: Write + Seek + Debug;
66
67impl<T> SharedWriter<T>
68where
69    T: Write + Seek + Debug {
70    pub fn new(reader: T) -> Self {
71        Self(Rc::new(RefCell::new(reader)))
72    }
73}
74
75impl<T> Write for SharedWriter<T>
76where
77    T: Write + Seek + Debug {
78    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
79        self.0.borrow_mut().write(buf)
80    }
81    fn flush(&mut self) -> io::Result<()> {
82        self.0.borrow_mut().flush()
83    }
84}
85
86impl<T> Seek for SharedWriter<T>
87where
88    T: Write + Seek + Debug {
89    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
90        self.0.borrow_mut().seek(pos)
91    }
92}
93
94impl<T> Clone for SharedWriter<T>
95where
96    T: Write + Seek + Debug {
97    fn clone(&self) -> Self {
98        Self(self.0.clone())
99    }
100}
101
102/// * Encapsulated shared `Read + Write + Seek + Debug`
103#[derive(Debug)]
104pub struct SharedReadWrite<T> (Rc<RefCell<T>>) where T: Read + Write + Seek + Debug;
105
106impl<T> SharedReadWrite<T>
107where
108    T: Read + Write + Seek + Debug {
109    pub fn new(readwrite: T) -> Self {
110        Self(Rc::new(RefCell::new(readwrite)))
111    }
112}
113
114impl<T> Read for SharedReadWrite<T>
115where
116    T: Read + Write + Seek + Debug {
117    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
118        self.0.borrow_mut().read(buf)
119    }
120}
121
122impl<T> Write for SharedReadWrite<T>
123where
124    T: Read + Write + Seek + Debug {
125    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
126        self.0.borrow_mut().write(buf)
127    }
128    fn flush(&mut self) -> io::Result<()> {
129        self.0.borrow_mut().flush()
130    }
131}
132
133impl<T> Seek for SharedReadWrite<T>
134where
135    T: Read + Write + Seek + Debug {
136    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
137        self.0.borrow_mut().seek(pos)
138    }
139}
140
141impl<T> Clone for SharedReadWrite<T>
142where
143    T: Read + Write + Seek + Debug {
144    fn clone(&self) -> Self {
145        Self(self.0.clone())
146    }
147}
148
149/// * Dishonest reader, a reader that reads data but modifies it.
150pub struct DishonestReader<T>
151where
152    T: Read + Seek + Debug {
153    reader: T,
154    on_read: Box<dyn FnMut(&mut T, usize) -> io::Result<Vec<u8>>>,
155    on_seek: Box<dyn FnMut(&mut T, SeekFrom) -> io::Result<u64>>,
156    cache: Vec<u8>,
157}
158
159impl<T> DishonestReader<T>
160where
161    T: Read + Seek + Debug {
162    pub fn new(
163        reader: T,
164        on_read: Box<dyn FnMut(&mut T, usize) -> io::Result<Vec<u8>>>,
165        on_seek: Box<dyn FnMut(&mut T, SeekFrom) -> io::Result<u64>>,
166    ) -> Self {
167        Self {
168            reader,
169            on_read,
170            on_seek,
171            cache: Vec::new(),
172        }
173    }
174}
175
176impl<T> Read for DishonestReader<T>
177where
178    T: Read + Seek + Debug {
179    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
180        let write_buf_and_cache = |data: &[u8], buf: &mut [u8], cache: &mut Vec<u8>| -> usize {
181            let len = min(data.len(), buf.len());
182            buf[..len].copy_from_slice(&data[..len]);
183            if len < data.len() {
184                *cache = data[len..].to_vec();
185            } else {
186                *cache = Vec::new();
187            }
188            len
189        };
190        if self.cache.is_empty() {
191            match (self.on_read)(&mut self.reader, buf.len()) {
192                Ok(data) => Ok(write_buf_and_cache(&data, buf, &mut self.cache)),
193                Err(e) => Err(e),
194            }
195        } else {
196            let to_write = self.cache.clone();
197            Ok(write_buf_and_cache(&to_write, buf, &mut self.cache))
198        }
199    }
200}
201
202impl<T> Seek for DishonestReader<T>
203where
204    T: Read + Seek + Debug {
205    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
206        (self.on_seek)(&mut self.reader, pos)
207    }
208}
209
210impl<T> Debug for DishonestReader<T>
211where
212    T: Read + Seek + Debug {
213    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
214        let typename = type_name::<T>();
215        f.debug_struct(&format!("DishonestReader<{typename}>"))
216        .field("reader", &self.reader)
217        .field("on_read", &format_args!("Box<dyn FnMut(&mut T, usize) -> io::Result<Vec<u8>>>"))
218        .field("on_seek", &format_args!("Box<dyn FnMut(&mut T, SeekFrom) -> io::Result<u64>>"))
219        .field("cache", &format_args!("[u8; {}]", self.cache.len()))
220        .finish()
221    }
222}
223
224/// * A Reader that combines two readers into one with the ability to `Read` and `Seek` and `Debug`
225#[derive(Debug)]
226pub struct CombinedReader<R1, R2>
227where
228    R1: Reader,
229    R2: Reader {
230    first: R1,
231    first_data_offset: u64,
232    first_data_length: u64,
233    second: R2,
234    second_data_offset: u64,
235    second_data_length: u64,
236    stream_pos: u64,
237    total_length: u64,
238}
239
240impl<R1, R2> CombinedReader<R1, R2>
241where
242    R1: Reader,
243    R2: Reader {
244    pub fn new(
245        first: R1,
246        first_data_offset: u64,
247        first_data_length: u64,
248        second: R2,
249        second_data_offset: u64,
250        second_data_length: u64,
251    ) -> Self {
252        Self {
253            first,
254            first_data_offset,
255            first_data_length,
256            second,
257            second_data_offset,
258            second_data_length,
259            stream_pos: 0,
260            total_length: first_data_length + second_data_length,
261        }
262    }
263
264    fn default_read<R>(reader: &mut R, buf: &mut [u8], reader_position: u64, reader_offset: u64, reader_length: u64) -> io::Result<usize>
265    where
266        R: Reader {
267        let bytes_to_read = min((reader_length - reader_position) as usize, buf.len());
268        reader.seek(SeekFrom::Start(reader_offset + reader_position))?;
269        reader.read(&mut buf[..bytes_to_read])
270    }
271}
272
273impl<R1, R2> Read for CombinedReader<R1, R2>
274where
275    R1: Reader,
276    R2: Reader {
277    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
278        if self.stream_pos < self.first_data_length {
279            let reader = &mut self.first;
280            let reader_position = self.stream_pos;
281            let reader_offset = self.first_data_offset;
282            let reader_length = self.first_data_length;
283            let n = Self::default_read(reader, buf, reader_position, reader_offset, reader_length)?;
284            self.stream_pos += n as u64;
285            Ok(n)
286        } else if self.stream_pos < self.total_length {
287            let reader = &mut self.second;
288            let reader_position = self.stream_pos - self.first_data_length;
289            let reader_offset = self.second_data_offset;
290            let reader_length = self.second_data_length;
291            let n = Self::default_read(reader, buf, reader_position, reader_offset, reader_length)?;
292            self.stream_pos += n as u64;
293            Ok(n)
294        } else {
295            return Ok(0)
296        }
297    }
298}
299
300impl<R1, R2> Seek for CombinedReader<R1, R2>
301where
302    R1: Reader,
303    R2: Reader {
304    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
305        self.stream_pos = min(match pos {
306            SeekFrom::Start(position) => position,
307            relative => {
308                let ipos = match relative {
309                    SeekFrom::End(offset) => self.total_length as i64 + offset,
310                    SeekFrom::Current(offset) => self.stream_pos as i64 + offset,
311                    _absolute => unreachable!(),
312                };
313                if ipos < 0 {
314                    return Err(io::Error::new(io::ErrorKind::InvalidInput, format!("Seek position out of bounds: {ipos}")))
315                }
316                ipos as u64
317            }
318        }, self.total_length);
319        Ok(self.stream_pos)
320    }
321}
322
323/// * A better `Cursor<Vec<u8>>` which has a friendlier `Debug` trait implementation
324#[derive(Clone)]
325pub struct CursorVecU8(Cursor<Vec<u8>>);
326
327impl CursorVecU8 {
328    pub fn new(data: Vec<u8>) -> Self {
329        Self(Cursor::new(data))
330    }
331
332    pub fn into_inner(self) -> Vec<u8> {
333        self.0.into_inner()
334    }
335
336    pub fn len(&self) -> usize {
337        self.0.get_ref().len()
338    }
339
340    pub fn set_len(&mut self, len: usize) {
341        self.0.get_mut().resize(len, 0);
342        if self.0.position() > len as u64 {
343            self.0.set_position(len as u64);
344        }
345    }
346
347    pub fn is_empty(&self) -> bool {
348        self.0.get_ref().is_empty()
349    }
350
351    pub fn clear(&mut self) {
352        self.0.get_mut().clear();
353        self.0.set_position(0);
354    }
355}
356
357impl Default for CursorVecU8 {
358    fn default() -> Self {
359        Self(Cursor::new(Vec::new()))
360    }
361}
362
363impl From<Cursor<Vec<u8>>> for CursorVecU8 {
364    fn from(cursor: Cursor<Vec<u8>>) -> Self {
365        Self(cursor)
366    }
367}
368
369impl From<CursorVecU8> for Cursor<Vec<u8>> {
370    fn from(val: CursorVecU8) -> Self {
371        val.0
372    }
373}
374
375impl Deref for CursorVecU8 {
376    type Target = Cursor<Vec<u8>>;
377
378    fn deref(&self) -> &Self::Target {
379        &self.0
380    }
381}
382
383impl DerefMut for CursorVecU8 {
384    fn deref_mut(&mut self) -> &mut Self::Target {
385        &mut self.0
386    }
387}
388
389impl Read for CursorVecU8 {
390    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
391        self.0.read(buf)
392    }
393}
394
395impl Write for CursorVecU8 {
396    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
397        self.0.write(buf)
398    }
399    fn flush(&mut self) -> io::Result<()> {
400        self.0.flush()
401    }
402}
403
404impl Seek for CursorVecU8 {
405    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
406        self.0.seek(pos)
407    }
408}
409
410impl Debug for CursorVecU8 {
411    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
412        f.debug_struct("Cursor")
413        .field("inner", &format_args!("[u8; {}]", self.0.get_ref().len()))
414        .field("pos", &self.0.position())
415        .finish()
416    }
417}
418
419impl Display for CursorVecU8 {
420    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
421        <Self as Debug>::fmt(self, f)
422    }
423}
424
425impl Index<usize> for CursorVecU8 {
426    type Output = u8;
427    fn index(&self, index: usize) -> &u8 {
428        &self.0.get_ref()[index]
429    }
430}
431
432impl IndexMut<usize> for CursorVecU8 {
433    fn index_mut(&mut self, index: usize) -> &mut u8 {
434        &mut self.0.get_mut()[index]
435    }
436}
437
438impl Index<Range<usize>> for CursorVecU8 {
439    type Output = [u8];
440    fn index(&self, range: Range<usize>) -> &[u8] {
441        &self.0.get_ref()[range]
442    }
443}
444
445impl IndexMut<Range<usize>> for CursorVecU8 {
446    fn index_mut(&mut self, range: Range<usize>) -> &mut [u8] {
447        &mut self.0.get_mut()[range]
448    }
449}
450
451impl Index<RangeFrom<usize>> for CursorVecU8 {
452    type Output = [u8];
453    fn index(&self, range: RangeFrom<usize>) -> &[u8] {
454        &self.0.get_ref()[range]
455    }
456}
457
458impl IndexMut<RangeFrom<usize>> for CursorVecU8 {
459    fn index_mut(&mut self, range: RangeFrom<usize>) -> &mut [u8] {
460        &mut self.0.get_mut()[range]
461    }
462}
463
464impl Index<RangeTo<usize>> for CursorVecU8 {
465    type Output = [u8];
466    fn index(&self, range: RangeTo<usize>) -> &[u8] {
467        &self.0.get_ref()[range]
468    }
469}
470
471impl IndexMut<RangeTo<usize>> for CursorVecU8 {
472    fn index_mut(&mut self, range: RangeTo<usize>) -> &mut [u8] {
473        &mut self.0.get_mut()[range]
474    }
475}
476
477impl Index<RangeFull> for CursorVecU8 {
478    type Output = [u8];
479    fn index(&self, _range: RangeFull) -> &[u8] {
480        &self.0.get_ref()[..]
481    }
482}
483
484impl IndexMut<RangeFull> for CursorVecU8 {
485    fn index_mut(&mut self, _range: RangeFull) -> &mut [u8] {
486        &mut self.0.get_mut()[..]
487    }
488}
489
490/// * The shared `Cursor`.
491/// * Because it's shared, when the 3rd library owned it, we still can access to it..
492#[derive(Debug)]
493pub struct SharedCursor (Rc<RefCell<CursorVecU8>>);
494
495impl SharedCursor {
496    pub fn new() -> Self {
497        Self::default()
498    }
499
500    /// * Get the inner data size
501    pub fn len(&self) -> usize {
502        self.0.borrow().get_ref().len()
503    }
504
505    /// * Check if the inner data is empty
506    pub fn is_empty(&self) -> bool {
507        self.len() == 0
508    }
509
510    /// * Get the inner data as `Vec<u8>`
511    pub fn get_vec(&self) -> Vec<u8> {
512        self.0.borrow().get_ref().to_vec()
513    }
514
515    /// * Discard current inner data, replace it with new data, and set the read/write position to the end of the data
516    pub fn set_vec(&mut self, data: &[u8], rw_pos: u64) {
517        let mut new_cursor = CursorVecU8::new(data.to_vec());
518        new_cursor.set_position(rw_pos);
519        *self.0.borrow_mut() = new_cursor;
520    }
521
522    /// * Discard the inner data, set the read/write position to 0
523    pub fn clear(&mut self) {
524        *self.0.borrow_mut() = CursorVecU8::default();
525    }
526}
527
528impl Default for SharedCursor {
529    fn default() -> Self {
530        Self(Rc::new(RefCell::new(CursorVecU8::default())))
531    }
532}
533
534impl Read for SharedCursor {
535    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
536        self.0.borrow_mut().read(buf)
537    }
538}
539
540impl Write for SharedCursor {
541    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
542        self.0.borrow_mut().write(buf)
543    }
544    fn flush(&mut self) -> io::Result<()> {
545        self.0.borrow_mut().flush()
546    }
547}
548
549impl Seek for SharedCursor {
550    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
551        self.0.borrow_mut().seek(pos)
552    }
553}
554
555impl Clone for SharedCursor {
556    fn clone(&self) -> Self {
557        Self(self.0.clone())
558    }
559}
560
561/// * The `StreamType<R, W, RW>` is for the `MultistreamIO<R, W, RW>` to manage multiple IO objects.
562/// * A stream can be a reader, writer, reader + writer, or cursor.
563/// * By using the `MultistreamIO<R, W, RW>` you can control the 3rd party library to write data into different streams and manipulate them.
564#[derive(Debug)]
565pub enum StreamType<R, W, RW>
566where
567    R: Read + Seek + Debug,
568    W: Write + Seek + Debug,
569    RW: Read + Write + Seek + Debug {
570    /// * The `Read + Seek + Debug`
571    Reader(R),
572
573    /// * The `Write + Seek + Debug`
574    Writer(W),
575
576    /// * The `Read + Write + Seek + Debug`, better use it with the `tempfile()`
577    ReadWrite(RW),
578
579    /// * The `Read + Write + Seek + Debug` cursor.
580    CursorU8(CursorVecU8)
581}
582
583impl<R, W, RW> StreamType<R, W, RW>
584where
585    R: Read + Seek + Debug,
586    W: Write + Seek + Debug,
587    RW: Read + Write + Seek + Debug {
588
589    pub fn as_reader(&mut self) -> &mut R {
590        let name_r = type_name::<R>();
591        let name_w = type_name::<W>();
592        let name_rw = type_name::<RW>();
593        match self {
594            Self::Reader(reader) => reader,
595            o => panic!("The `StreamType<{name_r}, {name_w}, {name_rw}>` is {:?}", o),
596        }
597    }
598
599    pub fn as_writer(&mut self) -> &mut W {
600        let name_r = type_name::<R>();
601        let name_w = type_name::<W>();
602        let name_rw = type_name::<RW>();
603        match self {
604            Self::Writer(writer) => writer,
605            o => panic!("The `StreamType<{name_r}, {name_w}, {name_rw}>` is {:?}", o),
606        }
607    }
608
609    pub fn as_readwrite(&mut self) -> &mut RW {
610        let name_r = type_name::<R>();
611        let name_w = type_name::<W>();
612        let name_rw = type_name::<RW>();
613        match self {
614            Self::ReadWrite(readwrite) => readwrite,
615            o => panic!("The `StreamType<{name_r}, {name_w}, {name_rw}>` is {:?}", o),
616        }
617    }
618
619    pub fn as_cursor(&mut self) -> &mut CursorVecU8 {
620        let name_r = type_name::<R>();
621        let name_w = type_name::<W>();
622        let name_rw = type_name::<RW>();
623        match self {
624            Self::CursorU8(cursor) => cursor,
625            o => panic!("The `StreamType<{name_r}, {name_w}, {name_rw}>` is {:?}", o),
626        }
627    }
628
629    /// * Take the cursor data, leaving an empty cursor here.
630    pub fn take_cursor_data(&mut self) -> Vec<u8> {
631        let name_r = type_name::<R>();
632        let name_w = type_name::<W>();
633        let name_rw = type_name::<RW>();
634        match self {
635            Self::CursorU8(cursor) => {
636                mem::take(cursor).into_inner()
637            }
638            o => panic!("The `StreamType<{name_r}, {name_w}, {name_rw}>` is {:?}", o),
639        }
640    }
641}
642
643impl<R, W, RW> Read for StreamType<R, W, RW>
644where
645    R: Read + Seek + Debug,
646    W: Write + Seek + Debug,
647    RW: Read + Write + Seek + Debug {
648    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
649        match self {
650            Self::Reader(reader) => {
651                reader.read(buf)
652            }
653            Self::ReadWrite(readwrite) => {
654                readwrite.read(buf)
655            }
656            Self::CursorU8(cursor) => {
657                cursor.read(buf)
658            }
659            Self::Writer(_) => Err(io::Error::new(io::ErrorKind::Unsupported, "`StreamType::Writer()` can't read.")),
660        }
661    }
662}
663
664impl<R, W, RW> Write for StreamType<R, W, RW>
665where
666    R: Read + Seek + Debug,
667    W: Write + Seek + Debug,
668    RW: Read + Write + Seek + Debug {
669    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
670        match self {
671            Self::Writer(writer) => {
672                writer.write(buf)
673            }
674            Self::ReadWrite(readwrite) => {
675                readwrite.write(buf)
676            }
677            Self::CursorU8(cursor) => {
678                cursor.write(buf)
679            }
680            Self::Reader(_) => Err(io::Error::new(io::ErrorKind::Unsupported, "`StreamType::Reader()` can't write.")),
681        }
682    }
683    fn flush(&mut self) -> io::Result<()> {
684        match self {
685            Self::Writer(writer) => {
686                writer.flush()
687            }
688            Self::ReadWrite(readwrite) => {
689                readwrite.flush()
690            }
691            Self::CursorU8(cursor) => {
692                cursor.flush()
693            }
694            Self::Reader(_) => Err(io::Error::new(io::ErrorKind::Unsupported, "`StreamType::Reader()` can't flush.")),
695        }
696    }
697}
698
699impl<R, W, RW> Seek for StreamType<R, W, RW>
700where
701    R: Read + Seek + Debug,
702    W: Write + Seek + Debug,
703    RW: Read + Write + Seek + Debug {
704    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
705        match self {
706            Self::Reader(reader) => {
707                reader.seek(pos)
708            }
709            Self::Writer(writer) => {
710                writer.seek(pos)
711            }
712            Self::ReadWrite(readwrite) => {
713                readwrite.seek(pos)
714            }
715            Self::CursorU8(cursor) => {
716                cursor.seek(pos)
717            }
718        }
719    }
720}
721
722/// * The `MultistreamIO<R, W, RW>` is for managing multiple IO objects.
723/// * This thing itself implements `Read + Write + Seek + Debug`, when these traits methods are called, the selected stream is manipulated.
724/// * by using this, you can control the 3rd party library to read or write data from/into different stream objects, and you can manipulate these data or streams.
725#[derive(Debug)]
726pub struct MultistreamIO<R, W, RW>
727where
728    R: Read + Seek + Debug,
729    W: Write + Seek + Debug,
730    RW: Read + Write + Seek + Debug {
731    pub streams: Vec<StreamType<R, W, RW>>,
732    pub cur_stream: usize,
733}
734
735impl<R, W, RW> Default for MultistreamIO<R, W, RW>
736where
737    R: Read + Seek + Debug,
738    W: Write + Seek + Debug,
739    RW: Read + Write + Seek + Debug {
740    fn default() -> Self {
741        Self {
742            streams: Vec::new(),
743            cur_stream: 0,
744        }
745    }
746}
747
748impl<R, W, RW> MultistreamIO<R, W, RW>
749where
750    R: Read + Seek + Debug,
751    W: Write + Seek + Debug,
752    RW: Read + Write + Seek + Debug {
753
754    pub fn new() -> Self {
755        Self::default()
756    }
757
758    /// * Get the current selected stream object
759    pub fn get_cur_stream(&self) -> &StreamType<R, W, RW> {
760        &self.streams[self.cur_stream]
761    }
762
763    /// * Get the current selected stream object as mutable
764    pub fn get_cur_stream_mut(&mut self) -> &mut StreamType<R, W, RW> {
765        &mut self.streams[self.cur_stream]
766    }
767
768    /// * Get a stream object using an index
769    pub fn get_stream(&self, index: usize) -> &StreamType<R, W, RW> {
770        &self.streams[index]
771    }
772
773    /// * Get a mutable stream object using an index
774    pub fn get_stream_mut(&mut self, index: usize) -> &mut StreamType<R, W, RW> {
775        &mut self.streams[index]
776    }
777
778    /// * Add a new stream
779    pub fn push_stream(&mut self, stream: StreamType<R, W, RW>) {
780        self.streams.push(stream);
781    }
782
783    /// * Pop out the last stream
784    pub fn pop_stream(&mut self) -> Option<StreamType<R, W, RW>> {
785        self.streams.pop()
786    }
787
788    /// * Set the current stream index
789    pub fn set_stream(&mut self, index: usize) {
790        self.cur_stream = index;
791    }
792
793    /// * The number of streams in total
794    pub fn len(&self) -> usize {
795        self.streams.len()
796    }
797
798    /// * Is there no stream objects?
799    pub fn is_empty(&self) -> bool {
800        self.streams.is_empty()
801    }
802}
803
804impl<R, W, RW> Read for MultistreamIO<R, W, RW>
805where
806    R: Read + Seek + Debug,
807    W: Write + Seek + Debug,
808    RW: Read + Write + Seek + Debug {
809    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
810        self.get_cur_stream_mut().read(buf)
811    }
812}
813
814impl<R, W, RW> Write for MultistreamIO<R, W, RW>
815where
816    R: Read + Seek + Debug,
817    W: Write + Seek + Debug,
818    RW: Read + Write + Seek + Debug {
819    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
820        self.get_cur_stream_mut().write(buf)
821    }
822    fn flush(&mut self) -> io::Result<()> {
823        self.get_cur_stream_mut().flush()
824    }
825}
826
827impl<R, W, RW> Seek for MultistreamIO<R, W, RW>
828where
829    R: Read + Seek + Debug,
830    W: Write + Seek + Debug,
831    RW: Read + Write + Seek + Debug {
832    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
833        self.get_cur_stream_mut().seek(pos)
834    }
835}
836
837impl<R, W, RW> Index<usize> for MultistreamIO<R, W, RW>
838where
839    R: Read + Seek + Debug,
840    W: Write + Seek + Debug,
841    RW: Read + Write + Seek + Debug {
842    type Output = StreamType<R, W, RW>;
843
844    fn index(&self, index: usize) -> &Self::Output {
845        &self.streams[index]
846    }
847}
848
849impl<R, W, RW> IndexMut<usize> for MultistreamIO<R, W, RW>
850where
851    R: Read + Seek + Debug,
852    W: Write + Seek + Debug,
853    RW: Read + Write + Seek + Debug {
854    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
855        &mut self.streams[index]
856    }
857}
858
859impl<R, W, RW> Index<Range<usize>> for MultistreamIO<R, W, RW>
860where
861    R: Read + Seek + Debug,
862    W: Write + Seek + Debug,
863    RW: Read + Write + Seek + Debug {
864    type Output = [StreamType<R, W, RW>];
865
866    fn index(&self, range: Range<usize>) -> &Self::Output {
867        &self.streams[range]
868    }
869}
870
871impl<R, W, RW> IndexMut<Range<usize>> for MultistreamIO<R, W, RW>
872where
873    R: Read + Seek + Debug,
874    W: Write + Seek + Debug,
875    RW: Read + Write + Seek + Debug {
876    fn index_mut(&mut self, range: Range<usize>) -> &mut Self::Output {
877        &mut self.streams[range]
878    }
879}
880
881impl<R, W, RW> Index<RangeFrom<usize>> for MultistreamIO<R, W, RW>
882where
883    R: Read + Seek + Debug,
884    W: Write + Seek + Debug,
885    RW: Read + Write + Seek + Debug {
886    type Output = [StreamType<R, W, RW>];
887
888    fn index(&self, range: RangeFrom<usize>) -> &Self::Output {
889        &self.streams[range]
890    }
891}
892
893impl<R, W, RW> IndexMut<RangeFrom<usize>> for MultistreamIO<R, W, RW>
894where
895    R: Read + Seek + Debug,
896    W: Write + Seek + Debug,
897    RW: Read + Write + Seek + Debug {
898    fn index_mut(&mut self, range: RangeFrom<usize>) -> &mut Self::Output {
899        &mut self.streams[range]
900    }
901}
902
903impl<R, W, RW> Index<RangeTo<usize>> for MultistreamIO<R, W, RW>
904where
905    R: Read + Seek + Debug,
906    W: Write + Seek + Debug,
907    RW: Read + Write + Seek + Debug {
908    type Output = [StreamType<R, W, RW>];
909
910    fn index(&self, range: RangeTo<usize>) -> &Self::Output {
911        &self.streams[range]
912    }
913}
914
915impl<R, W, RW> IndexMut<RangeTo<usize>> for MultistreamIO<R, W, RW>
916where
917    R: Read + Seek + Debug,
918    W: Write + Seek + Debug,
919    RW: Read + Write + Seek + Debug {
920    fn index_mut(&mut self, range: RangeTo<usize>) -> &mut Self::Output {
921        &mut self.streams[range]
922    }
923}
924
925impl<R, W, RW> Index<RangeFull> for MultistreamIO<R, W, RW>
926where
927    R: Read + Seek + Debug,
928    W: Write + Seek + Debug,
929    RW: Read + Write + Seek + Debug {
930    type Output = [StreamType<R, W, RW>];
931
932    fn index(&self, _range: RangeFull) -> &Self::Output {
933        &self.streams[..]
934    }
935}
936
937impl<R, W, RW> IndexMut<RangeFull> for MultistreamIO<R, W, RW>
938where
939    R: Read + Seek + Debug,
940    W: Write + Seek + Debug,
941    RW: Read + Write + Seek + Debug {
942    fn index_mut(&mut self, _range: RangeFull) -> &mut Self::Output {
943        &mut self.streams[..]
944    }
945}
946
947/// * The shared version of the `MultistreamIO`.
948/// * Because it's shared, when the 3rd library owned it, we still can access to it, e.g. switch it to a cursor stream to capture some data.
949#[derive(Debug)]
950pub struct SharedMultistreamIO<R, W, RW> (Rc<RefCell<MultistreamIO<R, W, RW>>>)
951where
952    R: Read + Seek + Debug,
953    W: Write + Seek + Debug,
954    RW: Read + Write + Seek + Debug;
955
956impl<R, W, RW> SharedMultistreamIO<R, W, RW>
957where
958    R: Read + Seek + Debug,
959    W: Write + Seek + Debug,
960    RW: Read + Write + Seek + Debug {
961    pub fn new(writer_with_cursor: MultistreamIO<R, W, RW>) -> Self {
962        Self(Rc::new(RefCell::new(writer_with_cursor)))
963    }
964}
965
966impl<R, W, RW> Deref for SharedMultistreamIO<R, W, RW>
967where
968    R: Read + Seek + Debug,
969    W: Write + Seek + Debug,
970    RW: Read + Write + Seek + Debug {
971    type Target = MultistreamIO<R, W, RW>;
972
973    fn deref(&self) -> &Self::Target {
974        unsafe { &*(self.0.as_ptr() as *const MultistreamIO<R, W, RW>) }
975    }
976}
977
978impl<R, W, RW> DerefMut for SharedMultistreamIO<R, W, RW>
979where
980    R: Read + Seek + Debug,
981    W: Write + Seek + Debug,
982    RW: Read + Write + Seek + Debug {
983    fn deref_mut(&mut self) -> &mut Self::Target {
984        unsafe { &mut *self.0.as_ptr() }
985    }
986}
987
988impl<R, W, RW> Read for SharedMultistreamIO<R, W, RW>
989where
990    R: Read + Seek + Debug,
991    W: Write + Seek + Debug,
992    RW: Read + Write + Seek + Debug {
993    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
994        self.0.borrow_mut().read(buf)
995    }
996}
997
998impl<R, W, RW> Write for SharedMultistreamIO<R, W, RW>
999where
1000    R: Read + Seek + Debug,
1001    W: Write + Seek + Debug,
1002    RW: Read + Write + Seek + Debug {
1003    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1004        self.0.borrow_mut().write(buf)
1005    }
1006    fn flush(&mut self) -> io::Result<()> {
1007        self.0.borrow_mut().flush()
1008    }
1009}
1010
1011impl<R, W, RW> Seek for SharedMultistreamIO<R, W, RW>
1012where
1013    R: Read + Seek + Debug,
1014    W: Write + Seek + Debug,
1015    RW: Read + Write + Seek + Debug {
1016    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
1017        self.0.borrow_mut().seek(pos)
1018    }
1019}
1020
1021impl<R, W, RW> Clone for SharedMultistreamIO<R, W, RW>
1022where
1023    R: Read + Seek + Debug,
1024    W: Write + Seek + Debug,
1025    RW: Read + Write + Seek + Debug {
1026    fn clone(&self) -> Self {
1027        Self(self.0.clone())
1028    }
1029}
1030
1031impl<R, W, RW> Default for SharedMultistreamIO<R, W, RW>
1032where
1033    R: Read + Seek + Debug,
1034    W: Write + Seek + Debug,
1035    RW: Read + Write + Seek + Debug {
1036    fn default() -> Self {
1037        Self(Rc::new(RefCell::new(MultistreamIO::new())))
1038    }
1039}
1040
1041/// * Go to an offset without using seek. It's achieved by using dummy reads.
1042pub fn goto_offset_without_seek<T>(
1043    mut reader: T,
1044    cur_pos: &mut u64,
1045    position: u64,
1046) -> io::Result<u64>
1047where
1048    T: Read,
1049{
1050    const SKIP_SIZE: u64 = 1024;
1051    let mut skip_buf = [0u8; SKIP_SIZE as usize];
1052    while *cur_pos + SKIP_SIZE <= position {
1053        reader.read_exact(&mut skip_buf)?;
1054        *cur_pos += SKIP_SIZE;
1055    }
1056    if *cur_pos < position {
1057        let mut skip_buf = vec![0u8; (position - *cur_pos) as usize];
1058        reader.read_exact(&mut skip_buf)?;
1059        *cur_pos = position;
1060    }
1061    if *cur_pos > position {
1062        Err(io::Error::new(
1063            io::ErrorKind::NotSeekable,
1064            format!(
1065                "The current position {cur_pos} has already exceeded the target position {position}"
1066            ),
1067        ))
1068    } else {
1069        Ok(*cur_pos)
1070    }
1071}
1072
1073/// * Copy data from a reader to a writer from the current position.
1074pub fn copy<R, W>(reader: &mut R, writer: &mut W, bytes_to_copy: u64) -> io::Result<()>
1075where
1076    R: Read,
1077    W: Write,
1078{
1079    const BUFFER_SIZE: u64 = 1024;
1080    let mut buf = vec![0u8; BUFFER_SIZE as usize];
1081    let mut to_copy = bytes_to_copy;
1082    while to_copy >= BUFFER_SIZE {
1083        reader.read_exact(&mut buf)?;
1084        writer.write_all(&buf)?;
1085        to_copy -= BUFFER_SIZE;
1086    }
1087    if to_copy > 0 {
1088        buf.resize(to_copy as usize, 0);
1089        reader.read_exact(&mut buf)?;
1090        writer.write_all(&buf)?;
1091    }
1092    Ok(())
1093}
1094
1095/// * This is for read/write strings from/to file with specific encoding and size, or read/write as NUL-terminated strings.
1096pub mod string_io {
1097    use savagestr::{SavageStringCodecs, StringCodecMaps};
1098    use std::io::{self, Read, Write};
1099
1100    /// * Read some bytes, and return the bytes, without you to create a local `vec![0u8; size]` and scratch your head with the messy codes
1101    pub fn read_bytes<T: Read>(r: &mut T, size: usize) -> io::Result<Vec<u8>> {
1102        let mut buf = vec![0u8; size];
1103        r.read_exact(&mut buf)?;
1104        Ok(buf)
1105    }
1106
1107    /// * Read a fixed-size string and decode it using the `StringCodecMaps`
1108    pub fn read_str<T: Read>(
1109        r: &mut T,
1110        size: usize,
1111        text_encoding: &StringCodecMaps,
1112    ) -> io::Result<String> {
1113        let mut buf = vec![0u8; size];
1114        r.read_exact(&mut buf)?;
1115        Ok(text_encoding
1116            .decode(&buf)
1117            .trim_matches(char::from(0))
1118            .to_string())
1119    }
1120
1121    /// * Read a fixed-size string and decode it using the `StringCodecMaps` while you can specify the code page.
1122    pub fn read_str_by_code_page<T: Read>(
1123        r: &mut T,
1124        size: usize,
1125        text_encoding: &StringCodecMaps,
1126        code_page: u32,
1127    ) -> io::Result<String> {
1128        let mut buf = vec![0u8; size];
1129        r.read_exact(&mut buf)?;
1130        Ok(text_encoding
1131            .decode_bytes_by_code_page(&buf, code_page)
1132            .trim_matches(char::from(0))
1133            .to_string())
1134    }
1135
1136    /// * Read a NUL terminated string by raw, not decode it.
1137    pub fn read_sz_raw<T: Read>(r: &mut T) -> io::Result<Vec<u8>> {
1138        let mut buf = Vec::<u8>::new();
1139        loop {
1140            let b = [0u8; 1];
1141            r.read_exact(&mut buf)?;
1142            let b = b[0];
1143            if b != 0 {
1144                buf.push(b);
1145            } else {
1146                break;
1147            }
1148        }
1149        Ok(buf)
1150    }
1151
1152    /// * Read a NUL terminated string and decode it.
1153    pub fn read_sz<T: Read>(
1154        r: &mut T,
1155        text_encoding: &StringCodecMaps,
1156    ) -> io::Result<String> {
1157        Ok(text_encoding
1158            .decode(&read_sz_raw(r)?)
1159            .trim_matches(char::from(0))
1160            .to_string())
1161    }
1162
1163    /// * Read a NUL terminated string and decode it with the specified code page.
1164    pub fn read_sz_by_code_page<T: Read>(
1165        r: &mut T,
1166        text_encoding: &StringCodecMaps,
1167        code_page: u32,
1168    ) -> io::Result<String> {
1169        Ok(text_encoding
1170            .decode_bytes_by_code_page(&read_sz_raw(r)?, code_page)
1171            .trim_matches(char::from(0))
1172            .to_string())
1173    }
1174
1175    /// * Write a fixed-size encoded string.
1176    pub fn write_str_sized<T: Write + ?Sized>(
1177        w: &mut T,
1178        data: &str,
1179        size: usize,
1180        text_encoding: &StringCodecMaps,
1181    ) -> io::Result<()> {
1182        let mut data = text_encoding.encode(data);
1183        data.resize(size, 0);
1184        w.write_all(&data)?;
1185        Ok(())
1186    }
1187
1188    /// * Write an encoded string.
1189    pub fn write_str<T: Write + ?Sized>(
1190        w: &mut T,
1191        data: &str,
1192        text_encoding: &StringCodecMaps,
1193    ) -> io::Result<()> {
1194        let data = text_encoding.encode(data);
1195        w.write_all(&data)?;
1196        Ok(())
1197    }
1198
1199    /// * Write an encoded string encoded with the specified code page.
1200    pub fn write_str_by_code_page<T: Write + ?Sized>(
1201        w: &mut T,
1202        data: &str,
1203        text_encoding: &StringCodecMaps,
1204        code_page: u32,
1205    ) -> io::Result<()> {
1206        let data = text_encoding.encode_strings_by_code_page(data, code_page);
1207        w.write_all(&data)?;
1208        Ok(())
1209    }
1210}