refactor(io): make accurate reader names primary (#4060)

This commit is contained in:
Zhengchao An
2026-06-29 20:20:22 +08:00
committed by GitHub
parent 14e9fa2c8d
commit 9567e7b3be
4 changed files with 114 additions and 58 deletions
+37 -15
View File
@@ -101,7 +101,7 @@ impl From<io::Error> for DirectIoError {
/// let reader = AlignedPreadReader::new(file, offset, size)?;
/// ```
#[cfg(target_os = "linux")]
pub struct DirectIoReader {
pub struct AlignedPreadReader {
/// Underlying file handle used for aligned pread I/O
file: std::fs::File,
/// Current read position
@@ -117,7 +117,7 @@ pub struct DirectIoReader {
}
#[cfg(target_os = "linux")]
impl DirectIoReader {
impl AlignedPreadReader {
/// Alignment requirement for reads (512 bytes for most systems)
pub const ALIGNMENT: usize = 512;
@@ -131,7 +131,7 @@ impl DirectIoReader {
///
/// # Returns
///
/// A `DirectIoReader` that reads the file at the given offset.
/// An `AlignedPreadReader` that reads the file at the given offset.
///
/// # Errors
///
@@ -197,7 +197,7 @@ impl DirectIoReader {
}
#[cfg(target_os = "linux")]
impl AsyncRead for DirectIoReader {
impl AsyncRead for AlignedPreadReader {
fn poll_read(mut self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
let filled = buf.filled().len();
let mut remaining = buf.initialize_unfilled();
@@ -222,12 +222,12 @@ impl AsyncRead for DirectIoReader {
/// On non-Linux platforms, `read_at`-based I/O is not available through this
/// type. This stub exists to provide a consistent API across platforms.
#[cfg(not(target_os = "linux"))]
pub struct DirectIoReader {
pub struct AlignedPreadReader {
_priv: (),
}
#[cfg(not(target_os = "linux"))]
impl DirectIoReader {
impl AlignedPreadReader {
/// Create a new aligned pread reader (not supported on this platform).
///
/// Always returns an error on non-Linux platforms.
@@ -237,7 +237,7 @@ impl DirectIoReader {
}
#[cfg(not(target_os = "linux"))]
impl AsyncRead for DirectIoReader {
impl AsyncRead for AlignedPreadReader {
fn poll_read(self: Pin<&mut Self>, _cx: &mut Context<'_>, _buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Err(io::Error::new(
io::ErrorKind::Unsupported,
@@ -246,11 +246,11 @@ impl AsyncRead for DirectIoReader {
}
}
impl std::fmt::Debug for DirectIoReader {
impl std::fmt::Debug for AlignedPreadReader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
#[cfg(target_os = "linux")]
{
f.debug_struct("DirectIoReader")
f.debug_struct("AlignedPreadReader")
.field("pos", &self.pos)
.field("remaining", &self.remaining)
.field("buffer_len", &self.buffer_len)
@@ -258,7 +258,9 @@ impl std::fmt::Debug for DirectIoReader {
}
#[cfg(not(target_os = "linux"))]
{
f.debug_struct("DirectIoReader").field("platform", &"unsupported").finish()
f.debug_struct("AlignedPreadReader")
.field("platform", &"unsupported")
.finish()
}
}
}
@@ -266,8 +268,9 @@ impl std::fmt::Debug for DirectIoReader {
/// Preferred name for aligned pread errors.
pub type AlignedPreadError = DirectIoError;
/// Preferred name for the aligned pread-based reader.
pub type AlignedPreadReader = DirectIoReader;
/// Historical name for the aligned pread-based reader.
#[deprecated(since = "1.0.0-beta.8", note = "use AlignedPreadReader; this reader does not set O_DIRECT")]
pub type DirectIoReader = AlignedPreadReader;
#[cfg(test)]
mod tests {
@@ -279,7 +282,10 @@ mod tests {
{
// Valid alignment
let file = std::fs::File::open("/dev/zero").unwrap();
assert!(DirectIoReader::new(file, 0, 512).is_ok(), "Should succeed with aligned offset and size");
assert!(
AlignedPreadReader::new(file, 0, 512).is_ok(),
"Should succeed with aligned offset and size"
);
let file = std::fs::File::open("/dev/zero").expect("open /dev/zero for alias");
assert!(
@@ -289,16 +295,32 @@ mod tests {
// Invalid offset
let file = std::fs::File::open("/dev/zero").unwrap();
assert!(DirectIoReader::new(file, 1, 512).is_err(), "Should fail with unaligned offset");
assert!(AlignedPreadReader::new(file, 1, 512).is_err(), "Should fail with unaligned offset");
// Invalid size
let file = std::fs::File::open("/dev/zero").unwrap();
assert!(DirectIoReader::new(file, 0, 511).is_err(), "Should fail with unaligned size");
assert!(AlignedPreadReader::new(file, 0, 511).is_err(), "Should fail with unaligned size");
}
#[cfg(not(target_os = "linux"))]
{
// Non-Linux should return UnsupportedPlatform
let file = std::fs::File::open(std::env::current_exe().unwrap()).unwrap();
assert!(matches!(AlignedPreadReader::new(file, 0, 512), Err(DirectIoError::UnsupportedPlatform)));
}
}
#[test]
#[allow(deprecated)]
fn test_legacy_direct_io_alias() {
#[cfg(target_os = "linux")]
{
let file = std::fs::File::open("/dev/zero").unwrap();
assert!(DirectIoReader::new(file, 0, 512).is_ok());
}
#[cfg(not(target_os = "linux"))]
{
let file = std::fs::File::open(std::env::current_exe().unwrap()).unwrap();
assert!(matches!(DirectIoReader::new(file, 0, 512), Err(DirectIoError::UnsupportedPlatform)));
}