io_stream/io.rs
1//! Filesystem I/O requests and responses.
2
3use std::fmt;
4
5/// The stream I/O request and response enum, emitted by [coroutines]
6/// and processed by [runtimes].
7///
8/// Represents all the possible I/O requests that a stream coroutine
9/// can emit. Runtimes should be able to handle all variants.
10///
11/// [coroutines]: crate::coroutines
12/// [runtimes]: crate::runtimes
13#[derive(Clone, Eq, PartialEq)]
14pub enum StreamIo {
15 /// I/O request to read bytes.
16 ///
17 /// Input: read buffer as vec
18 ///
19 /// Output: [`StreamOutput`]
20 Read(Result<StreamOutput, Vec<u8>>),
21
22 /// I/O request to write bytes.
23 ///
24 /// Input: write buffer as vec
25 ///
26 /// Output: [`StreamOutput`]
27 Write(Result<StreamOutput, Vec<u8>>),
28}
29
30impl fmt::Debug for StreamIo {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Self::Read(Ok(_)) => f.write_str("read output"),
34 Self::Read(Err(_)) => f.write_str("read input"),
35
36 Self::Write(Ok(_)) => f.write_str("write output"),
37 Self::Write(Err(_)) => f.write_str("write input"),
38 }
39 }
40}
41
42/// Output returned by both read and write coroutines.
43#[derive(Clone, Debug, Eq, PartialEq)]
44pub struct StreamOutput {
45 /// The inner buffer.
46 pub buffer: Vec<u8>,
47
48 /// The amount of bytes that have been read/written.
49 pub bytes_count: usize,
50}
51
52impl StreamOutput {
53 /// Returns the exact read/written bytes as slice.
54 pub fn bytes(&self) -> &[u8] {
55 &self.buffer[..self.bytes_count]
56 }
57}