oximedia-io 0.1.8

I/O layer for OxiMedia
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Buffered I/O: read-ahead buffering, write coalescing, and buffer pools.
//!
//! Provides a lightweight synchronous buffered I/O layer that sits on top of
//! any byte slice or cursor, offering configurable buffer sizes, read-ahead
//! semantics, and write coalescing to reduce syscall overhead.

use std::collections::VecDeque;
use std::io::{self, Read, Write};

// ---------------------------------------------------------------------------
// Buffer pool
// ---------------------------------------------------------------------------

/// A fixed-size pool of reusable byte buffers.
#[derive(Debug)]
pub struct BufferPool {
    buf_size: usize,
    free: VecDeque<Vec<u8>>,
    capacity: usize,
}

impl BufferPool {
    /// Create a new pool with `capacity` buffers of `buf_size` bytes each.
    #[must_use]
    pub fn new(buf_size: usize, capacity: usize) -> Self {
        let mut free = VecDeque::with_capacity(capacity);
        for _ in 0..capacity {
            free.push_back(vec![0u8; buf_size]);
        }
        Self {
            buf_size,
            free,
            capacity,
        }
    }

    /// Acquire a buffer from the pool. Returns `None` if the pool is exhausted.
    pub fn acquire(&mut self) -> Option<Vec<u8>> {
        if let Some(mut buf) = self.free.pop_front() {
            // Zero the buffer for safety before handing it out.
            buf.fill(0);
            Some(buf)
        } else {
            None
        }
    }

    /// Return a buffer to the pool.  If the pool is full the buffer is dropped.
    pub fn release(&mut self, buf: Vec<u8>) {
        if self.free.len() < self.capacity {
            self.free.push_back(buf);
        }
    }

    #[must_use]
    pub fn available(&self) -> usize {
        self.free.len()
    }

    #[must_use]
    pub fn buf_size(&self) -> usize {
        self.buf_size
    }

    #[must_use]
    pub fn capacity(&self) -> usize {
        self.capacity
    }
}

// ---------------------------------------------------------------------------
// Read-ahead buffer
// ---------------------------------------------------------------------------

/// Wraps a `Read` source with a configurable read-ahead buffer.
pub struct ReadAheadBuffer<R: Read> {
    inner: R,
    buf: Vec<u8>,
    pos: usize,
    filled: usize,
}

impl<R: Read> ReadAheadBuffer<R> {
    /// Create a new read-ahead buffer with `buf_size` bytes of capacity.
    pub fn new(inner: R, buf_size: usize) -> Self {
        Self {
            inner,
            buf: vec![0u8; buf_size],
            pos: 0,
            filled: 0,
        }
    }

    /// Fill the internal buffer from the underlying reader if it is empty.
    fn fill_buf(&mut self) -> io::Result<usize> {
        if self.pos >= self.filled {
            self.pos = 0;
            self.filled = self.inner.read(&mut self.buf)?;
        }
        Ok(self.filled - self.pos)
    }

    /// Read up to `dst.len()` bytes, filling from the buffer first.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if the underlying reader fails.
    pub fn read_bytes(&mut self, dst: &mut [u8]) -> io::Result<usize> {
        let available = self.fill_buf()?;
        if available == 0 {
            return Ok(0);
        }
        let n = dst.len().min(available);
        dst[..n].copy_from_slice(&self.buf[self.pos..self.pos + n]);
        self.pos += n;
        Ok(n)
    }

    /// Bytes currently available without hitting the underlying reader.
    pub fn buffered(&self) -> usize {
        self.filled.saturating_sub(self.pos)
    }

    /// Consume the wrapper and return the underlying reader.
    pub fn into_inner(self) -> R {
        self.inner
    }
}

// ---------------------------------------------------------------------------
// Write coalescing buffer
// ---------------------------------------------------------------------------

/// Coalesces small writes into larger chunks before flushing to the sink.
pub struct CoalescingWriter<W: Write> {
    inner: W,
    buf: Vec<u8>,
    threshold: usize,
    total_written: u64,
    flush_count: u64,
}

impl<W: Write> CoalescingWriter<W> {
    /// Create a new coalescing writer with a flush `threshold` in bytes.
    pub fn new(inner: W, threshold: usize) -> Self {
        Self {
            inner,
            buf: Vec::with_capacity(threshold),
            threshold,
            total_written: 0,
            flush_count: 0,
        }
    }

    /// Buffer `data`, flushing to the sink when the threshold is reached.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if flushing to the underlying writer fails.
    pub fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
        self.buf.extend_from_slice(data);
        self.total_written += data.len() as u64;
        if self.buf.len() >= self.threshold {
            self.flush()?;
        }
        Ok(())
    }

    /// Flush any buffered data to the underlying writer.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if writing to the underlying writer fails.
    pub fn flush(&mut self) -> io::Result<()> {
        if !self.buf.is_empty() {
            self.inner.write_all(&self.buf)?;
            self.buf.clear();
            self.flush_count += 1;
        }
        Ok(())
    }

    pub fn buffered_bytes(&self) -> usize {
        self.buf.len()
    }

    pub fn total_written(&self) -> u64 {
        self.total_written
    }

    pub fn flush_count(&self) -> u64 {
        self.flush_count
    }

    /// Flush and return the inner writer.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if the final flush fails.
    pub fn into_inner(mut self) -> io::Result<W> {
        self.flush()?;
        Ok(self.inner)
    }
}

// ---------------------------------------------------------------------------
// I/O cursor helper for tests
// ---------------------------------------------------------------------------

/// A simple in-memory cursor that implements `Read` and `Write`.
#[derive(Debug, Default)]
pub struct MemCursor {
    data: Vec<u8>,
    read_pos: usize,
}

impl MemCursor {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn from_bytes(data: Vec<u8>) -> Self {
        Self { data, read_pos: 0 }
    }

    #[must_use]
    pub fn as_slice(&self) -> &[u8] {
        &self.data
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

impl Read for MemCursor {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let remaining = self.data.len().saturating_sub(self.read_pos);
        if remaining == 0 {
            return Ok(0);
        }
        let n = buf.len().min(remaining);
        buf[..n].copy_from_slice(&self.data[self.read_pos..self.read_pos + n]);
        self.read_pos += n;
        Ok(n)
    }
}

impl Write for MemCursor {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.data.extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Double-buffered reader
// ---------------------------------------------------------------------------

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex};

/// Shared state between the caller and the background fill thread.
struct SharedState<R: Read + Send + 'static> {
    /// The underlying reader, moved here so the background thread can own it.
    reader: Mutex<Option<R>>,
    /// The back buffer being filled by the background thread.
    back_buf: Mutex<BackBuf>,
    /// Condvar signalled by the background thread when `back_buf` is ready,
    /// and by the caller when the back buffer should be refilled.
    condvar: Condvar,
    /// Set to `true` by `Drop` to ask the background thread to stop.
    cancelled: AtomicBool,
}

/// Contents of the back buffer.
struct BackBuf {
    data: Vec<u8>,
    len: usize,
    /// `true` once the background thread has finished a fill pass.
    ready: bool,
    /// `true` if the underlying reader returned 0 bytes (EOF).
    eof: bool,
    /// `true` if the underlying reader returned an error.
    error: bool,
}

/// A reader that uses two alternating buffers so a background thread can fill
/// the next buffer while the caller drains the current one, hiding I/O latency.
///
/// # Concurrency model
///
/// * The **background thread** owns the reader (via `SharedState::reader`).
///   It loops: fill the back buffer → lock `back_buf`, set `ready=true`,
///   signal the condvar → wait until `ready` is cleared again before refilling.
/// * The **foreground (caller)** drains the front buffer via [`Read::read`].
///   When the front is exhausted it waits on the condvar for `ready=true`,
///   swaps front and back, clears `ready`, signals the condvar (let background
///   refill), then continues serving bytes from the new front.
pub struct DoubleBufferedReader<R: Read + Send + 'static> {
    /// Buffer being consumed by the caller.
    front: Vec<u8>,
    /// How many valid bytes are in `front`.
    front_len: usize,
    /// Next unread offset within `front`.
    front_pos: usize,
    /// Shared state for coordination with the background thread.
    shared: Arc<SharedState<R>>,
    /// Handle to the background fill thread.
    fill_thread: Option<std::thread::JoinHandle<()>>,
    /// Set once we have drained the very last filled buffer.
    eof: bool,
}

impl<R: Read + Send + 'static> DoubleBufferedReader<R> {
    /// Create a new `DoubleBufferedReader` wrapping `reader` with two buffers
    /// of `buf_size` bytes each, and spawn the background fill thread.
    pub fn new(reader: R, buf_size: usize) -> Self {
        let buf_size = buf_size.max(1);

        let shared = Arc::new(SharedState {
            reader: Mutex::new(Some(reader)),
            back_buf: Mutex::new(BackBuf {
                data: vec![0u8; buf_size],
                len: 0,
                ready: false,
                eof: false,
                error: false,
            }),
            condvar: Condvar::new(),
            cancelled: AtomicBool::new(false),
        });

        let shared_clone = Arc::clone(&shared);
        let fill_thread = std::thread::spawn(move || {
            Self::fill_loop(shared_clone, buf_size);
        });

        // The front buffer starts empty; the first `read()` call will block
        // until the background thread completes its first fill.
        Self {
            front: vec![0u8; buf_size],
            front_len: 0,
            front_pos: 0,
            shared,
            fill_thread: Some(fill_thread),
            eof: false,
        }
    }

    /// Background thread loop: fill the back buffer then wait to be asked again.
    fn fill_loop(shared: Arc<SharedState<R>>, buf_size: usize) {
        loop {
            if shared.cancelled.load(Ordering::Acquire) {
                break;
            }

            // Read into a temporary local buffer to avoid holding the lock
            // during I/O.
            let mut tmp = vec![0u8; buf_size];
            let (n, eof, error) = {
                let mut guard = shared.reader.lock().unwrap_or_else(|e| e.into_inner());
                match guard.as_mut() {
                    None => (0, true, false),
                    Some(r) => match r.read(&mut tmp) {
                        Ok(0) => (0, true, false),
                        Ok(n) => (n, false, false),
                        Err(_) => (0, false, true),
                    },
                }
            };

            // Publish results into the back buffer and signal the caller.
            {
                let mut back = shared.back_buf.lock().unwrap_or_else(|e| e.into_inner());
                back.data[..n].copy_from_slice(&tmp[..n]);
                back.len = n;
                back.ready = true;
                back.eof = eof;
                back.error = error;
            }
            shared.condvar.notify_one();

            if eof || error || shared.cancelled.load(Ordering::Acquire) {
                break;
            }

            // Wait until the caller swaps and clears `ready` before we
            // overwrite the back buffer.
            let mut back = shared.back_buf.lock().unwrap_or_else(|e| e.into_inner());
            back = shared
                .condvar
                .wait_while(back, |b| {
                    b.ready && !shared.cancelled.load(Ordering::Acquire)
                })
                .unwrap_or_else(|e| e.into_inner());
            drop(back);
        }
    }

    /// Wait for the back buffer to be ready, then swap it with the front.
    /// Returns `false` if the back buffer signals EOF or error.
    fn swap_buffers(&mut self) -> io::Result<bool> {
        let mut back = self
            .shared
            .back_buf
            .lock()
            .unwrap_or_else(|e| e.into_inner());

        // Wait until the background thread marks back as ready.
        back = self
            .shared
            .condvar
            .wait_while(back, |b| !b.ready)
            .unwrap_or_else(|e| e.into_inner());

        if back.error {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "background fill thread encountered a read error",
            ));
        }

        let filled = back.len;
        let eof = back.eof;

        // Swap front and back buffers.
        std::mem::swap(&mut self.front, &mut back.data);
        self.front_len = filled;
        self.front_pos = 0;

        // Signal the background thread that it may refill the back buffer.
        back.ready = false;
        drop(back);
        self.shared.condvar.notify_one();

        if eof && filled == 0 {
            self.eof = true;
            return Ok(false);
        }

        Ok(true)
    }
}

impl<R: Read + Send + 'static> Read for DoubleBufferedReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if self.eof {
            return Ok(0);
        }

        // Refill front if exhausted (including the very first call).
        while self.front_pos >= self.front_len {
            if !self.swap_buffers()? {
                return Ok(0); // EOF
            }
            if self.front_len > 0 {
                break;
            }
        }

        let available = self.front_len - self.front_pos;
        let n = buf.len().min(available);
        buf[..n].copy_from_slice(&self.front[self.front_pos..self.front_pos + n]);
        self.front_pos += n;
        Ok(n)
    }
}

impl<R: Read + Send + 'static> Drop for DoubleBufferedReader<R> {
    fn drop(&mut self) {
        // Signal the background thread to stop.
        self.shared.cancelled.store(true, Ordering::Release);

        // Drop the inner reader so any blocked `read()` in the background
        // thread will get an error / return 0 (for Cursor-like sources it
        // returns immediately).
        if let Ok(mut guard) = self.shared.reader.lock() {
            *guard = None;
        }

        // Wake the background thread in case it is waiting on the condvar.
        {
            if let Ok(mut back) = self.shared.back_buf.lock() {
                back.ready = false; // allow the thread to exit its wait_while
            }
        }
        self.shared.condvar.notify_all();

        // Join the fill thread.
        if let Some(handle) = self.fill_thread.take() {
            let _ = handle.join();
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_buffer_pool_acquire_and_release() {
        let mut pool = BufferPool::new(512, 4);
        assert_eq!(pool.available(), 4);
        let buf = pool.acquire().expect("acquire should succeed");
        assert_eq!(pool.available(), 3);
        assert_eq!(buf.len(), 512);
        pool.release(buf);
        assert_eq!(pool.available(), 4);
    }

    #[test]
    fn test_buffer_pool_exhaustion() {
        let mut pool = BufferPool::new(64, 2);
        let _b1 = pool.acquire().expect("acquire should succeed");
        let _b2 = pool.acquire().expect("acquire should succeed");
        assert!(pool.acquire().is_none());
    }

    #[test]
    fn test_buffer_pool_capacity_and_buf_size() {
        let pool = BufferPool::new(1024, 8);
        assert_eq!(pool.capacity(), 8);
        assert_eq!(pool.buf_size(), 1024);
    }

    #[test]
    fn test_buffer_pool_release_beyond_capacity_drops() {
        let mut pool = BufferPool::new(16, 1);
        // Already full
        pool.release(vec![0u8; 16]);
        assert_eq!(pool.available(), 1); // still 1, extra was dropped
    }

    #[test]
    fn test_read_ahead_buffer_reads_all_data() {
        let src = MemCursor::from_bytes(vec![1, 2, 3, 4, 5, 6, 7, 8]);
        let mut rab = ReadAheadBuffer::new(src, 4);
        let mut out = [0u8; 8];
        let mut total = 0;
        while total < 8 {
            let n = rab
                .read_bytes(&mut out[total..])
                .expect("read_bytes should succeed");
            if n == 0 {
                break;
            }
            total += n;
        }
        assert_eq!(total, 8);
        assert_eq!(&out, &[1, 2, 3, 4, 5, 6, 7, 8]);
    }

    #[test]
    fn test_read_ahead_buffer_returns_zero_on_eof() {
        let src = MemCursor::from_bytes(vec![]);
        let mut rab = ReadAheadBuffer::new(src, 16);
        let mut buf = [0u8; 4];
        let n = rab.read_bytes(&mut buf).expect("read_bytes should succeed");
        assert_eq!(n, 0);
    }

    #[test]
    fn test_read_ahead_buffer_buffered_count() {
        let src = MemCursor::from_bytes(vec![10, 20, 30, 40]);
        let mut rab = ReadAheadBuffer::new(src, 4);
        // Prime the buffer
        let mut buf = [0u8; 2];
        rab.read_bytes(&mut buf).expect("read_bytes should succeed");
        assert_eq!(rab.buffered(), 2);
    }

    #[test]
    fn test_coalescing_writer_does_not_flush_below_threshold() {
        let sink = MemCursor::new();
        let mut cw = CoalescingWriter::new(sink, 16);
        cw.write_bytes(&[1, 2, 3])
            .expect("write_bytes should succeed");
        assert_eq!(cw.buffered_bytes(), 3);
        assert_eq!(cw.flush_count(), 0);
    }

    #[test]
    fn test_coalescing_writer_flushes_at_threshold() {
        let sink = MemCursor::new();
        let mut cw = CoalescingWriter::new(sink, 4);
        cw.write_bytes(&[1, 2, 3, 4])
            .expect("write_bytes should succeed");
        assert_eq!(cw.flush_count(), 1);
        assert_eq!(cw.buffered_bytes(), 0);
    }

    #[test]
    fn test_coalescing_writer_total_written() {
        let sink = MemCursor::new();
        let mut cw = CoalescingWriter::new(sink, 256);
        cw.write_bytes(&[0u8; 100])
            .expect("write_bytes should succeed");
        cw.write_bytes(&[0u8; 50])
            .expect("write_bytes should succeed");
        assert_eq!(cw.total_written(), 150);
    }

    #[test]
    fn test_coalescing_writer_into_inner_flushes() {
        let sink = MemCursor::new();
        let mut cw = CoalescingWriter::new(sink, 256);
        cw.write_bytes(&[9, 8, 7])
            .expect("write_bytes should succeed");
        let out = cw.into_inner().expect("into_inner should succeed");
        assert_eq!(out.as_slice(), &[9, 8, 7]);
    }

    #[test]
    fn test_mem_cursor_read_write() {
        let mut cur = MemCursor::new();
        cur.write_all(&[1, 2, 3]).expect("failed to write");
        assert_eq!(cur.len(), 3);
        assert!(!cur.is_empty());
    }

    #[test]
    fn test_mem_cursor_from_bytes() {
        let cur = MemCursor::from_bytes(vec![5, 6]);
        assert_eq!(cur.as_slice(), &[5, 6]);
    }

    // -----------------------------------------------------------------------
    // DoubleBufferedReader tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_double_buffered_same_as_plain_read() {
        let original: Vec<u8> = (0u8..=255).collect();
        let cursor = std::io::Cursor::new(original.clone());
        let mut dbr = DoubleBufferedReader::new(cursor, 64);
        let mut out = Vec::new();
        io::Read::read_to_end(&mut dbr, &mut out).expect("read_to_end should succeed");
        assert_eq!(out, original);
    }

    #[test]
    fn test_double_buffered_empty() {
        let cursor = std::io::Cursor::new(Vec::<u8>::new());
        let mut dbr = DoubleBufferedReader::new(cursor, 64);
        let mut out = Vec::new();
        io::Read::read_to_end(&mut dbr, &mut out).expect("read_to_end should succeed");
        assert!(out.is_empty());
    }

    #[test]
    fn test_double_buffered_larger_than_buf() {
        // Source is 1 KiB, buffer size is 32 bytes — many swap cycles.
        let original: Vec<u8> = (0u8..=255).cycle().take(1024).collect();
        let cursor = std::io::Cursor::new(original.clone());
        let mut dbr = DoubleBufferedReader::new(cursor, 32);
        let mut out = Vec::new();
        io::Read::read_to_end(&mut dbr, &mut out).expect("read_to_end should succeed");
        assert_eq!(out, original);
    }
}