io_stream/io.rs
1use std::fmt;
2
3/// The streams I/O request enum, emitted by [coroutines] and
4/// processed by [runtimes].
5///
6/// Represents all the possible I/O requests that a stream coroutine
7/// can emit. Runtimes should be able to handle all variants.
8///
9/// [coroutines]: crate::coroutines
10/// [runtimes]: crate::runtimes
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub enum Io {
13 /// Generic error related to coroutine progression.
14 Error(String),
15
16 /// I/O for reading bytes.
17 Read(Result<Output, Vec<u8>>),
18
19 /// I/O for writing bytes.
20 Write(Result<Output, Vec<u8>>),
21}
22
23impl Io {
24 pub fn err(msg: impl fmt::Display) -> Io {
25 let msg = format!("Stream error: {msg}");
26 Io::Error(msg)
27 }
28}
29
30/// Output returned by both read and write coroutines.
31#[derive(Clone, Debug, Eq, PartialEq)]
32pub struct Output {
33 /// The inner buffer.
34 pub buffer: Vec<u8>,
35
36 /// The amount of bytes that have been read/written.
37 pub bytes_count: usize,
38}
39
40impl Output {
41 pub fn bytes(&self) -> &[u8] {
42 &self.buffer[..self.bytes_count]
43 }
44}