oximedia-io 0.1.2

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
//! Async I/O abstractions: completion queue, polling, and cancellation tokens.
//!
//! Provides lightweight building blocks for async I/O patterns without
//! requiring additional external dependencies beyond `tokio`.

#![allow(dead_code)]
#![allow(clippy::cast_precision_loss)]

use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

// ---------------------------------------------------------------------------
// Cancellation token
// ---------------------------------------------------------------------------

/// A lightweight cancellation token that can be shared across tasks.
#[derive(Debug, Clone)]
pub struct CancellationToken {
    cancelled: Arc<AtomicBool>,
}

impl CancellationToken {
    #[must_use]
    pub fn new() -> Self {
        Self {
            cancelled: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Signal cancellation.
    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::SeqCst);
    }

    /// Returns `true` if cancellation has been signalled.
    #[must_use]
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::SeqCst)
    }

    /// Create a child token that shares the same cancel flag.
    #[must_use]
    pub fn child(&self) -> Self {
        Self {
            cancelled: Arc::clone(&self.cancelled),
        }
    }
}

impl Default for CancellationToken {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// I/O operation descriptor
// ---------------------------------------------------------------------------

/// Unique identifier for an async I/O operation.
pub type OpId = u64;

/// The type of an async I/O operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OpKind {
    Read,
    Write,
    Flush,
    Seek,
}

/// Status of a queued I/O operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OpStatus {
    Pending,
    InFlight,
    Completed { bytes: usize },
    Failed { reason: String },
    Cancelled,
}

/// Descriptor for a single async I/O operation.
#[derive(Debug, Clone)]
pub struct IoOp {
    pub id: OpId,
    pub kind: OpKind,
    pub offset: u64,
    pub length: usize,
    pub status: OpStatus,
    pub cancel: CancellationToken,
}

impl IoOp {
    #[must_use]
    pub fn new_read(id: OpId, offset: u64, length: usize) -> Self {
        Self {
            id,
            kind: OpKind::Read,
            offset,
            length,
            status: OpStatus::Pending,
            cancel: CancellationToken::new(),
        }
    }

    #[must_use]
    pub fn new_write(id: OpId, offset: u64, length: usize) -> Self {
        Self {
            id,
            kind: OpKind::Write,
            offset,
            length,
            status: OpStatus::Pending,
            cancel: CancellationToken::new(),
        }
    }

    #[must_use]
    pub fn is_pending(&self) -> bool {
        self.status == OpStatus::Pending
    }

    #[must_use]
    pub fn is_complete(&self) -> bool {
        matches!(self.status, OpStatus::Completed { .. })
    }

    #[must_use]
    pub fn is_failed(&self) -> bool {
        matches!(self.status, OpStatus::Failed { .. })
    }

    pub fn cancel(&mut self) {
        self.cancel.cancel();
        if self.status == OpStatus::Pending || self.status == OpStatus::InFlight {
            self.status = OpStatus::Cancelled;
        }
    }
}

// ---------------------------------------------------------------------------
// Completion queue
// ---------------------------------------------------------------------------

/// A simple synchronous completion queue for I/O operations.
/// In production this would integrate with an OS completion mechanism
/// (`io_uring`, IOCP, kqueue).  Here we model the data structures.
#[derive(Debug, Default)]
pub struct CompletionQueue {
    pending: VecDeque<IoOp>,
    completed: VecDeque<IoOp>,
    next_id: OpId,
}

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

    pub fn next_id(&mut self) -> OpId {
        let id = self.next_id;
        self.next_id += 1;
        id
    }

    /// Submit an operation to the queue.
    pub fn submit(&mut self, op: IoOp) {
        self.pending.push_back(op);
    }

    /// Simulate completion: move all pending ops to completed, marking them
    /// as succeeded with `bytes_per_op` bytes transferred.
    pub fn drain_pending(&mut self, bytes_per_op: usize) {
        while let Some(mut op) = self.pending.pop_front() {
            if op.cancel.is_cancelled() {
                op.status = OpStatus::Cancelled;
            } else {
                op.status = OpStatus::Completed {
                    bytes: bytes_per_op,
                };
            }
            self.completed.push_back(op);
        }
    }

    /// Pop the next completed operation.
    pub fn pop_completed(&mut self) -> Option<IoOp> {
        self.completed.pop_front()
    }

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

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

// ---------------------------------------------------------------------------
// Polling state machine
// ---------------------------------------------------------------------------

/// State of a polling async operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PollState {
    /// Operation not yet started.
    Idle,
    /// Waiting for I/O to become ready.
    Waiting,
    /// Data is available; the operation can proceed.
    Ready,
    /// Operation is complete.
    Done,
    /// Operation failed.
    Error(String),
}

/// Simulates an async poller over a byte buffer.
#[derive(Debug)]
pub struct BytePoller {
    data: Vec<u8>,
    pos: usize,
    state: PollState,
    poll_count: u32,
    /// After this many polls, the poller becomes Ready.
    ready_after: u32,
}

impl BytePoller {
    #[must_use]
    pub fn new(data: Vec<u8>, ready_after: u32) -> Self {
        Self {
            data,
            pos: 0,
            state: PollState::Idle,
            poll_count: 0,
            ready_after,
        }
    }

    /// Advance the state machine one step.
    pub fn poll(&mut self) -> &PollState {
        self.poll_count += 1;
        match &self.state {
            PollState::Idle => {
                self.state = PollState::Waiting;
            }
            PollState::Waiting => {
                if self.poll_count >= self.ready_after {
                    self.state = PollState::Ready;
                }
            }
            PollState::Ready => {
                self.state = PollState::Done;
            }
            PollState::Done | PollState::Error(_) => {}
        }
        &self.state
    }

    /// Read bytes from the underlying data (only valid in Ready/Done state).
    pub fn read_chunk(&mut self, n: usize) -> Option<&[u8]> {
        if !matches!(self.state, PollState::Ready | PollState::Done) {
            return None;
        }
        let end = (self.pos + n).min(self.data.len());
        let slice = &self.data[self.pos..end];
        self.pos = end;
        Some(slice)
    }

    #[must_use]
    pub fn state(&self) -> &PollState {
        &self.state
    }

    #[must_use]
    pub fn poll_count(&self) -> u32 {
        self.poll_count
    }
}

// ---------------------------------------------------------------------------
// Timeout helper (synchronous mock)
// ---------------------------------------------------------------------------

/// A simple deadline tracker (synchronous; does not use `tokio` timers).
#[derive(Debug, Clone)]
pub struct Deadline {
    pub timeout: Duration,
    elapsed: Duration,
}

impl Deadline {
    #[must_use]
    pub fn new(timeout: Duration) -> Self {
        Self {
            timeout,
            elapsed: Duration::ZERO,
        }
    }

    /// Advance the simulated clock by `step`.  Returns `true` if the deadline
    /// has NOT yet been exceeded.
    pub fn tick(&mut self, step: Duration) -> bool {
        self.elapsed += step;
        self.elapsed < self.timeout
    }

    #[must_use]
    pub fn is_expired(&self) -> bool {
        self.elapsed >= self.timeout
    }

    #[must_use]
    pub fn remaining(&self) -> Duration {
        self.timeout.saturating_sub(self.elapsed)
    }
}

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

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

    #[test]
    fn test_cancellation_token_default_not_cancelled() {
        let t = CancellationToken::new();
        assert!(!t.is_cancelled());
    }

    #[test]
    fn test_cancellation_token_cancel() {
        let t = CancellationToken::new();
        t.cancel();
        assert!(t.is_cancelled());
    }

    #[test]
    fn test_cancellation_token_child_shares_state() {
        let parent = CancellationToken::new();
        let child = parent.child();
        parent.cancel();
        assert!(child.is_cancelled());
    }

    #[test]
    fn test_io_op_initial_state() {
        let op = IoOp::new_read(1, 0, 4096);
        assert!(op.is_pending());
        assert!(!op.is_complete());
        assert_eq!(op.kind, OpKind::Read);
    }

    #[test]
    fn test_io_op_cancel() {
        let mut op = IoOp::new_write(2, 0, 1024);
        op.cancel();
        assert_eq!(op.status, OpStatus::Cancelled);
    }

    #[test]
    fn test_completion_queue_submit_and_drain() {
        let mut cq = CompletionQueue::new();
        let id = cq.next_id();
        cq.submit(IoOp::new_read(id, 0, 512));
        assert_eq!(cq.pending_count(), 1);
        cq.drain_pending(512);
        assert_eq!(cq.pending_count(), 0);
        assert_eq!(cq.completed_count(), 1);
    }

    #[test]
    fn test_completion_queue_cancelled_op_stays_cancelled() {
        let mut cq = CompletionQueue::new();
        let mut op = IoOp::new_read(10, 0, 256);
        op.cancel();
        cq.submit(op);
        cq.drain_pending(256);
        let done = cq
            .pop_completed()
            .expect("pop_completed should return item");
        assert_eq!(done.status, OpStatus::Cancelled);
    }

    #[test]
    fn test_completion_queue_pop_order_fifo() {
        let mut cq = CompletionQueue::new();
        cq.submit(IoOp::new_read(1, 0, 1));
        cq.submit(IoOp::new_read(2, 1, 1));
        cq.drain_pending(1);
        assert_eq!(
            cq.pop_completed()
                .expect("pop_completed should return item")
                .id,
            1
        );
        assert_eq!(
            cq.pop_completed()
                .expect("pop_completed should return item")
                .id,
            2
        );
    }

    #[test]
    fn test_byte_poller_reaches_ready_state() {
        let mut p = BytePoller::new(vec![1, 2, 3], 3);
        p.poll(); // Idle → Waiting
        p.poll(); // stays Waiting (count=2 < 3)
        p.poll(); // count==3 → Ready
        assert_eq!(*p.state(), PollState::Ready);
    }

    #[test]
    fn test_byte_poller_read_chunk() {
        let mut p = BytePoller::new(vec![10, 20, 30], 1);
        p.poll(); // Idle → Waiting
        p.poll(); // Waiting → Ready (count=2 >= 1)
        let chunk = p.read_chunk(2).expect("read_chunk should succeed");
        assert_eq!(chunk, &[10, 20]);
    }

    #[test]
    fn test_byte_poller_no_read_when_waiting() {
        let mut p = BytePoller::new(vec![1], 99);
        p.poll();
        assert!(p.read_chunk(1).is_none());
    }

    #[test]
    fn test_deadline_not_expired_initially() {
        let d = Deadline::new(Duration::from_secs(5));
        assert!(!d.is_expired());
    }

    #[test]
    fn test_deadline_expires_after_ticks() {
        let mut d = Deadline::new(Duration::from_secs(2));
        assert!(d.tick(Duration::from_secs(1)));
        assert!(!d.tick(Duration::from_secs(1))); // now elapsed == timeout
        assert!(d.is_expired());
    }

    #[test]
    fn test_deadline_remaining() {
        let mut d = Deadline::new(Duration::from_secs(10));
        d.tick(Duration::from_secs(3));
        assert_eq!(d.remaining(), Duration::from_secs(7));
    }
}