asyncio-utils 0.4.4

Support limit,skip on AsyncRead traits
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use tokio::io::{AsyncRead, AsyncSeek, SeekFrom, AsyncSeekExt, AsyncReadExt, AsyncWriteExt};
use std::pin::Pin;
use core::task::Poll;
use tokio::io::ReadBuf;
use std::error::Error;
use std::io::Read;
use std::io::Write;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ObserverDecision {
    Continue,
    Abort,
}
/// Observe a stream's operation when user code is not involved.
/// For example: When you call EhRead.stream_to function, and you may want to calculate
/// the checksum (SHA1, SHA256 or MD5 along the way, you can observe the stream copy using the observer)
pub trait StreamObserver {
    /// Your observer's begin will be called before stream copy
    /// If you plan to reuse the observer, you can reset it here
    fn begin(&mut self) {
        // Default initialize does nothing
    }

    /// Before the upstream is read. If you want to abort, abort it here by
    /// overriding the before_read()
    fn before_read(&mut self) -> ObserverDecision {
        return ObserverDecision::Continue;
    }
    /// A chunk of data had been read from upstream, about to be written now
    /// You can intercept it by aborting it here
    fn before_write(&mut self, _: &[u8]) -> ObserverDecision {
        return ObserverDecision::Continue;
    }

    /// A chunk of data had been written to down stream.
    /// You can intercept it by aborting it here
    fn after_write(&mut self, _:&[u8]) -> ObserverDecision {
        return ObserverDecision::Continue;
    }

    /// The copy ended and `size` bytes had been copied
    /// If there is error, err with be Some(cause)
    /// Note, different from Result<usize, Box<dyn Error>>, the bytes copied is always given
    /// Even if it is zero
    fn end(&mut self, _:usize, _:Option<Box<&dyn Error>>) {

    }
}

struct DumbObserver;
impl StreamObserver for DumbObserver {
}
/// Enhanced Reader for std::io::Read
/// It provides convenient methods for exact reading without throwing error
/// It allow you to send it to writer
pub trait EhRead:Read {
    /// Try to fully read to fill the buffer, similar to read_exact, 
    /// However, this method never throw errors on error on EOF.
    /// On EOF, it also returns Ok(size) but size might be smaller than available buffer.
    /// When size is smaller than buffer size, it must be EOF.
    /// 
    /// Upon EOF, you may read again, but you will get EOF anyway with the EOF error.
    fn try_read_exact(&mut self, buffer: &mut [u8]) -> Result<usize, Box<dyn Error>> {
        let wanted = buffer.len();
        let mut copied:usize = 0;

        loop {
            let rr = self.read(&mut buffer[copied..])?;
            if rr == 0 {
                // EOF reached, return copied bytes so far. 
                // Caller upon seeing result shorter than expected can either:
                // a) declare EOF
                // b) call try_read_exact again but receive 0 bytes as result
                return Ok(copied);
            }
            copied = copied + rr;
            if copied >= wanted {
                return Ok(copied);
            }
        }
    }


    /// Skip bytes from the reader. Return the actual size skipped or the error.
    /// If EOF reached before skip is complete, UnexpectedEOF error is returned.
    /// On success, the size must be equal to the input bytes
    fn skip(&mut self, bytes: usize) -> Result<usize, Box<dyn Error>> {
        if bytes == 0 {
            return Ok(0);
        }
        let mut buffer = [0u8; 4096];
        let mut remaining = bytes;
        while remaining > 0 {
            let rr = self.try_read_exact(&mut buffer[..remaining])?;
            if rr == 0 {
                // EOF reached
                return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "Insufficient bytes to skip").into());
            }
            remaining -= rr;
        }
        Ok(bytes)
    }

    /// Copy all content until EOF to Write. 
    /// Using buffer_size buffer. If not given, 4096 is used.
    /// If buffer_size is Some(0), 4096 is used
    /// 
    /// Return the number of bytes copied, or any error encountered. 
    /// 
    /// If error is EOF, then error is not returned and size would be 0.
    fn stream_to<W>(&mut self, w:&mut W, buffer_size:Option<usize>, observer:Option<Box<dyn StreamObserver>>) -> Result<usize, Box<dyn Error>> 
        where W:Write + Sized
    {
        let mut buffer_size = buffer_size.unwrap_or(4096);
        if buffer_size == 0 {
            buffer_size = 4096;
        }
        let mut buffer = vec![0u8; buffer_size];
        return self.stream_to_with_buffer(w, &mut buffer, observer);
    }

    /// Same as stream_to, but use externally provided buffer
    fn stream_to_with_buffer<W>(&mut self, w:&mut W, buffer:&mut[u8], observer:Option<Box<dyn StreamObserver>>) -> Result<usize, Box<dyn Error>>
        where W:Write+Sized {
        let mut observer = observer;
        let default_ob: Box<dyn StreamObserver> = Box::new(DumbObserver);
        let mut obs = observer.take().unwrap_or(default_ob);
        let mut copied:usize = 0;
        loop {
            let decision = obs.before_read();
            if decision == ObserverDecision::Abort {
                break;
            }
            let rr = self.read(buffer);
            if rr.is_err() {
                let err = rr.err().unwrap();
                obs.end(copied, Some(Box::new(&err)));
                return Err(err.into());
            }
            let rr = rr.unwrap();
            if rr == 0 {
                // EOF
                break;
            }
            let decision = obs.before_write(&buffer[0..rr]);
            if decision == ObserverDecision::Abort {
                break;
            }
            let wr = w.write_all(&buffer[0..rr]);
            if wr.is_err() {
                let err = wr.err().unwrap();
                obs.end(copied, Some(Box::new(&err)));
                return Err(err.into());
            }
            let decision = obs.after_write(&buffer[0..rr]);
            if decision == ObserverDecision::Abort {
                break;
            }
            copied += rr;
        }
        return Ok(copied);
    }


}


/// Blanked implementation for EhRead for all Read for free
/// 
/// You can use EhRead functions on all Read's implementations as long as 
/// you use this library and import EhRead trait.
impl <T> EhRead for T where T:Read{}
/// Undo reader supports unread(&[u8])
/// Useful when you are doing serialization/deserialization where you 
/// need to put data back (undo the read)
/// You can use UndoReader as if it is a normal AsyncRead
/// Additionally, UndoReader supports a limit as well. It would stop reading after limit is reached (EOF)

/// Example:
/// ```
/// // You can have rust code between fences inside the comments
/// // If you pass --test to `rustdoc`, it will even test it for you!
/// async fn do_test() -> Result<(), Box<dyn std::error::Error>> {
///     use tokio::io::{AsyncRead,AsyncSeek,AsyncReadExt, AsyncSeekExt};
///     let f = tokio::fs::File::open("input.txt").await?;
///     let mut my_undo = crate::asyncio_utils::UndoReader::new(f, Some(10)); // only read 10 bytes
///     let mut buff = vec![0u8; 10];
///     let read_count = my_undo.read(&mut buff).await?;
///     if read_count > 0 {
///         // inspect the data read check if it is ok
///         my_undo.unread(&mut buff[0..read_count]); // put all bytes back
///     }
///     let data = "XYZ".as_bytes();
///     my_undo.unread(&data);
///     // this should be 3 (the "XYZ" should be read here)
///     let second_read_count = my_undo.read(&mut buff).await?;
///     // this should be equal to read_count because it would have been reread here
///     let third_read_count = my_undo.read(&mut buff).await?;
///     // ...
///     Ok(())
/// }
/// ```
pub struct UndoReader<T>
    where T:AsyncRead + Unpin
{
    src: T,
    read_count: usize,
    limit: usize,
    buffer: Vec<Vec<u8>>
}

impl<T> UndoReader<T>
    where T:AsyncRead + Unpin
{
    /// Destruct this UndoReader. 
    /// 
    /// Returns the buffer that has been unread but has not been consumed as well as the raw AsyncRead
    /// Example:
    /// ```
    /// // initialize my_undo
    /// async fn do_test() -> Result<(), Box<dyn std::error::Error>> {
    ///     let f = tokio::fs::File::open("input.txt").await?;
    ///     let my_undo = crate::asyncio_utils::UndoReader::new(f, None);
    ///     let (remaining, raw) = my_undo.destruct();
    ///     // ...
    ///     Ok(())
    /// }
    /// // remaining is the bytes to be consumed.
    /// // raw is the raw AsyncRead
    /// ```
    /// 
    /// The UndoReader can't be used anymore after this call
    pub fn destruct(self) -> (Vec<u8>, T) {
        let count = self.count_unread();
        let mut resultv = vec![0u8; count];
        self.copy_into(&mut resultv); 
        return (resultv, self.src)
    }

    // Copy the remaining buffer to given bytes
    // Internal use only
    fn copy_into(&self, buf:&mut [u8]) -> usize{
        let mut copied = 0;
        for i in 0.. self.buffer.len() {
            let v = &self.buffer[ self.buffer.len() - i - 1];
            for i in 0..v.len() {
                buf[copied + i] = v[i];
            }
            copied += v.len();
        }
        return copied;
    }

    /// Get the limit of the UndoReader
    /// If the limit was None, this would be the usize's max value.
    pub fn limit(&self)->usize {
        self.limit
    }

    /// Count the number of bytes in the unread buffer
    pub fn count_unread(&self) -> usize {
        let mut result:usize = 0;
        for v in &self.buffer {
            result += v.len();
        }
        return result;
    }

    /// Create new UndoReader with limitation.
    /// If limit is None, `std::usize::MAX` will be used
    /// If limit is Some(limit:usize), the limit will be used
    pub fn new(src:T, limit:Option<usize>) -> UndoReader<T> {
        UndoReader {
            src, 
            limit: match limit {
                None => std::usize::MAX,
                Some(actual) => actual
            },
            read_count: 0,
            buffer: Vec::new()
        }
    }

    /// Put data for unread so it can be read again.
    /// 
    /// Reading of unread data does not count towards the limit because we assume you 
    /// unconsumed something you consumed in the first place. 
    /// 
    /// However, practically, you can arbitrarily unread any data. So the limit may 
    /// break the promise in such cases
    pub fn unread(&mut self, data:&[u8]) -> &mut Self {
        if data.len() > 0 {
            let mut new = vec![0u8;data.len()];
            for (index, payload) in data.iter().enumerate() {
                new[index] = *payload;
            }
            self.buffer.push(new);
        }
        return self;
    }
}


/// Implementation of AsyncRead for UndoReader
impl<T> AsyncRead for UndoReader<T>
    where T:AsyncRead + Unpin
{
    fn poll_read(mut self: Pin<&mut Self>, ctx: &mut std::task::Context<'_>, 
        data: &mut ReadBuf<'_>) -> Poll<Result<(), std::io::Error>> { 
        loop {
            let next = self.buffer.pop();
            match next {
                Some(bufdata) => {
                    if bufdata.len() == 0 {
                        continue;
                    }
                    // give this data out
                    let available = bufdata.len();
                    let remaining = data.remaining();
                    if available <= remaining {
                        data.put_slice(&bufdata);
                    } else {
                        data.put_slice(&bufdata[0..remaining]);
                        let left_over = &bufdata[remaining..];
                        let mut new_vec = vec![0u8;left_over.len()];
                        for (index, payload) in left_over.iter().enumerate() {
                            new_vec[index] = *payload;
                        }
                        self.buffer.push(new_vec);
                    }
                    return Poll::Ready(Ok(()));
                },
                None => {
                    break;
                }
            }
        }
        if self.read_count >= self.limit {
            // Mark EOF directly
            return Poll::Ready(Ok(()));
        }
        let ms = &mut *self;
        let p = Pin::new(&mut ms.src);
        let before_filled = data.filled().len();
        let result = p.poll_read(ctx, data);
        let after_filled = data.filled().len();
        let this_read = after_filled - before_filled;
        self.read_count += this_read;

        let overread = self.read_count > self.limit;
        if overread {
            let overread_count = self.read_count - self.limit;
            //undo overread portion
            data.set_filled(after_filled - overread_count);
            self.read_count = self.limit;
        }
        
        return result;
    }
}


/// An AsyncRead + AsyncSeek wrapper
/// It supports limit the bytes can be read.
/// Typically used when you want to read a specific segments from a file
/// 
/// Example
/// ```
/// 
/// async fn run_test() -> Result<(), Box<dyn std::error::Error>> {
///     use tokio::io::SeekFrom;
///     use tokio::io::{AsyncRead,AsyncSeek, AsyncReadExt, AsyncSeekExt};
///     let f = tokio::fs::File::open("input.txt").await?;
///     let read_from: u64 = 18; // start read from 18'th byte
///     let mut lsr = crate::asyncio_utils::LimitSeekerReader::new(f, Some(20)); // read up to 20 bytes
///     lsr.seek(SeekFrom::Start(read_from)); // do seek
/// 
///     let mut buf = vec![0u8; 1024];
///     lsr.read(&mut buf); // read it
///     return Ok(());
/// }
/// ```
pub struct LimitSeekerReader<T>
    where T:AsyncRead + AsyncSeek + Unpin
{
    src: T,
    read_count: usize,
    limit: usize,
}

/// Implement a limit reader for AsyncRead and AsyncSeek together. Typically a file
/// Note that if your seek does not affect total reads. You can seek with positive/negative
/// from current/begin of file/end of file, but it does not change the total bytes would be 
/// read from the reader.
/// 
/// This is a little bit weird though. Typically what you want to do is just seek before reading.
/// 
/// This is useful when you want to service Http Get with Range requests.
/// 
/// You open tokio::fs::File 
/// you seek the position
/// you set limit on number of bytes to read
/// you start reading and serving.
/// 
impl<T> LimitSeekerReader<T>
    where T:AsyncRead + AsyncSeek + Unpin
{
    /// Destruct the LimitSeekerReader and get the bytes read so far and the original reader
    /// Returns the size read and the original reader. 
    /// 
    /// You can't use the LimitSeekerReader after this call
    pub fn destruct(self) -> (usize, T) {
        (self.read_count, self.src)
    }

    /// Create new LimitSeekerReader from another AsyncRead + AsyncSeek (typically file)
    /// 
    /// Argument src is the underlying reader + seeker
    /// limit is the byte limit. Node that the limit can be
    ///     Some(limit)
    ///     None -> No limit (std::usize::MAX)
    pub fn new(src:T, limit:Option<usize>) -> LimitSeekerReader<T> {
        LimitSeekerReader {
            src, 
            limit: {
                match limit {
                    None => std::usize::MAX,
                    Some(actual_limit) => actual_limit
                }
            },
            read_count: 0
        }
    }
}

/// Implementation of AsyncRead
impl<T> AsyncRead for LimitSeekerReader<T>
    where T:AsyncRead + AsyncSeek + Unpin
{
    fn poll_read(mut self: Pin<&mut Self>, ctx: &mut std::task::Context<'_>, 
        data: &mut ReadBuf<'_>) -> Poll<Result<(), std::io::Error>> { 
        if self.read_count >= self.limit {
            // Mark EOF directly
            return Poll::Ready(Ok(()));
        }
        let ms = &mut *self;
        let p = Pin::new(&mut ms.src);
        let before_filled = data.filled().len();
        let result = p.poll_read(ctx, data);
        let after_filled = data.filled().len();
        let this_read = after_filled - before_filled;
        self.read_count += this_read;

        let overread = self.read_count > self.limit;
        if overread {
            let overread_count = self.read_count - self.limit;
            //undo overread portion
            data.set_filled(after_filled - overread_count);
            self.read_count = self.limit;
        }
        
        return result;
    }
}


/// Implementation of AsyncSeek
impl<T> AsyncSeek for LimitSeekerReader<T>
    where T:AsyncRead + AsyncSeek + Unpin
{
    fn start_seek(mut self: Pin<&mut Self>, from: SeekFrom) -> Result<(), std::io::Error> {
        let ms = &mut *self;
        let p = Pin::new(&mut ms.src);
        return p.start_seek(from);
    }

    fn poll_complete(mut self: Pin<&mut Self>, ctx: &mut std::task::Context<'_>) -> Poll<Result<u64, std::io::Error>> { 
        let ms = &mut *self;
        let p = Pin::new(&mut ms.src);
        return p.poll_complete(ctx);
    }
}


/// Pure implementation for LimitReader to restrict number of bytes can be read
/// Useful when you want to read stream but want to end early no matter what
/// 
/// E.g. you can't accept more than 20MiB as HTTP Post body, you can limit it here
/// Example:
/// ```
/// async fn do_test() -> Result<(), Box<dyn std::error::Error>> {
///     use tokio::io::{AsyncReadExt};
///     let mut f = tokio::fs::File::open("input.txt").await?;
///     let mut reader = crate::asyncio_utils::LimitReader::new(f, Some(18)); // only read at most 18 bytes
/// 
///     let mut buf = vec![0u8; 2096];
///     reader.read(&mut buf).await?;
///     return Ok(());
/// }
/// ```
pub struct LimitReader<T>
    where T:AsyncRead + Unpin
{
    src: T,
    read_count: usize,
    limit: usize,
}


/// Implementation of AysncRead for LimitReader
impl<T> LimitReader<T>
    where T:AsyncRead + Unpin
{
    /// Create new LimitReader from another AsyncRead (typically File or Stream)
    /// 
    /// Argument src is the underlying reader
    /// limit is the byte limit. Node that the limit can be
    ///     Some(limit) -> The limit is set
    ///     None -> No limit (std::usize::MAX)
    pub fn new(src:T, limit:Option<usize>) -> LimitReader<T> {
        LimitReader {
            src, 
            limit: {
                match limit {
                    None => std::usize::MAX,
                    Some(actual_limit) => actual_limit
                }
            },
            read_count: 0
        }
    }

    /// Destruct the LimitReader and get the total read bytes and the original reader
    /// 
    /// Takes the ownership and You can't use the LimitReader after this call
    pub fn destruct(self) -> (usize, T) {
        (self.read_count, self.src)
    }
}

/// Implementation of AsyncRead
impl<T> AsyncRead for LimitReader<T>
    where T:AsyncRead + Unpin
{
    fn poll_read(mut self: Pin<&mut Self>, ctx: &mut std::task::Context<'_>, 
        data: &mut ReadBuf<'_>) -> Poll<Result<(), std::io::Error>> { 
        if self.read_count >= self.limit {
            // Mark EOF directly
            return Poll::Ready(Ok(()));
        }
        let ms = &mut *self;
        let p = Pin::new(&mut ms.src);
        let before_filled = data.filled().len();
        let result = p.poll_read(ctx, data);
        let after_filled = data.filled().len();
        let this_read = after_filled - before_filled;
        self.read_count += this_read;

        let overread = self.read_count > self.limit;
        if overread {
            let overread_count = self.read_count - self.limit;
            //undo overread portion
            data.set_filled(after_filled - overread_count);
            self.read_count = self.limit;
        }
        
        return result;
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_undo() {
        let file = tokio::fs::File::open("test.data").await.unwrap();
        let mut undor = UndoReader::new(file, Some(10));
        let mut buf = [0u8; 1024];
        let undo = "XXX".as_bytes();
        let undo1 = "YYY".as_bytes();
        undor.unread(undo);
        undor.unread(undo1);
        let rr = undor.read(&mut buf).await.unwrap();
        // Note Unread bytes does not count as actual consumption
        assert_eq!(&buf[0..rr], "YYY".as_bytes()); //undo read is priority
        let rr = undor.read(&mut buf).await.unwrap();
        // Note Unread bytes does not count as actual consumption
        assert_eq!(&buf[0..rr], "XXX".as_bytes()); //undo read is priority
        let rr = undor.read(&mut buf).await.unwrap();
        assert_eq!(&buf[0..rr], "123456789\n".as_bytes());

        //assert_eq!(result, 4);
    }
    #[tokio::test]
    async fn test_limit() {
        let file = tokio::fs::File::open("test.data").await.unwrap();
        let mut limitr = LimitReader::new(file, Some(10));
        let mut buf = [0u8; 1024];
        let rr = limitr.read(&mut buf).await.unwrap();
        assert_eq!(&buf[0..rr], "123456789\n".as_bytes());
        let rr = limitr.read(&mut buf).await.unwrap();
        assert_eq!(&buf[0..rr], "".as_bytes());
        //assert_eq!(result, 4);
    }

    #[tokio::test]
    async fn test_seek() {
        let file = tokio::fs::File::open("test.data").await.unwrap();
        let mut limitr = LimitSeekerReader::new(file, Some(10));
        limitr.seek(SeekFrom::Current(13)).await.unwrap();
        let mut buf = [0u8; 1024];
        let rr = limitr.read(&mut buf).await.unwrap();
        assert_eq!(&buf[0..rr], "456789\n123".as_bytes());
        let rr = limitr.read(&mut buf).await.unwrap();
        assert_eq!(&buf[0..rr], "".as_bytes());
        //assert_eq!(result, 4);
    }
}