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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2023-2025 Jarkko Sakkinen
//! Traits for I/O non-blocking operations.
use crate::Error;
/// Write operations.
pub trait Write {
/// Writes bytes from a buffer to the I/O port.
///
/// # Errors
///
/// [`Data`](crate::Error::Data) when corrupted data has been detected.
/// [`Read`](crate::Error::Read) when the read I/O fails with the serial
/// port.
/// [`Write`](crate::Error::Write) when the write I/O fails with the
/// serial port.
fn write(&mut self, buf: &[u8]) -> Result<Option<u32>, Error> {
if self.write_all(buf)?.is_none() {
return Ok(None);
}
u32::try_from(buf.len())
.map(Some)
.map_err(|_| Error::OutOfMemory)
}
/// Writes the entire buffer to the I/O port.
///
/// Returns `Ok(None)` only if no bytes were written.
///
/// # Errors
///
/// [`Data`](crate::Error::Data) when corrupted data has been detected.
/// [`Read`](crate::Error::Read) when the read I/O fails with the serial
/// port.
/// [`Write`](crate::Error::Write) when the write I/O fails with the
/// serial port.
fn write_all(&mut self, buf: &[u8]) -> Result<Option<()>, Error>;
/// Writes a single byte to the I/O port.
///
/// Returns `Ok(None)` only if no bytes were written.
///
/// # Errors
///
/// [`Data`](crate::Error::Data) when corrupted data has been detected.
/// [`Read`](crate::Error::Read) when the read I/O fails with the serial
/// port.
/// [`Write`](crate::Error::Write) when the write I/O fails with the
/// serial port.
fn write_byte(&mut self, value: u8) -> Result<Option<()>, Error> {
self.write_all(&[value])
}
}
/// Read operations.
pub trait Read {
/// Read bytes from the I/O port.
///
/// Returns `Ok(None)` only if no bytes were written.
///
/// # Errors
///
/// * [`Data`](crate::Error::Data) when corrupted data has been detected.
/// * [`Read`](crate::Error::Read) when the read I/O fails with the serial port.
/// * [`Write`](crate::Error::Write) when the write I/O fails with the serial port.
fn read(&mut self, buf: &mut [u8]) -> Result<Option<u32>, Error>;
/// Read a byte from the I/O port.
///
/// Returns `Ok(None)` only if no bytes were written.
///
/// # Errors
///
/// [`Data`](crate::Error::Data) when corrupted data has been detected.
/// [`Read`](crate::Error::Read) when the read I/O fails with the serial port.
/// [`Write`](crate::Error::Write) when the write I/O fails with the serial port.
fn read_byte(&mut self) -> Result<Option<u8>, Error>;
}
/// Seek operations
pub trait Seek {
/// Seeks I/O port to an offset.
///
/// # Errors
///
/// [`Data`](crate::Error::Data) when corrupted data has been detected.
/// [`Read`](crate::Error::Read) when the read I/O fails with the serial port.
/// [`Write`](crate::Error::Write) when the write I/O fails with the serial port.
fn seek(&mut self, offset: u32) -> Result<Option<u32>, Error>;
}