From e31ff3327789c67289f8beeb1c7c068c189ec466 Mon Sep 17 00:00:00 2001 From: Cutieguwu Date: Sun, 18 Jan 2026 12:03:23 -0500 Subject: [PATCH] Cleanup through additional impls for DirectIOBuffer. --- src/io.rs | 38 +++++++++++++++++++++++++++++++++++++- src/recovery.rs | 4 ++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/io.rs b/src/io.rs index 0e103a2..5713256 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,9 +1,11 @@ use std::fs::{File, OpenOptions}; use std::io::{self, Seek, SeekFrom}; +use std::ops::{Index, Range, RangeFrom}; +use std::slice::SliceIndex; use crate::cli::CONFIG; -use anyhow::Context; +use anyhow::{Context, anyhow}; /// Get length of data stream. /// Physical length of data stream in bytes @@ -91,3 +93,37 @@ impl From<[u8; crate::MAX_BUFFER_SIZE]> for DirectIOBuffer { Self(value) } } + +impl TryFrom<&[u8]> for DirectIOBuffer { + type Error = anyhow::Error; + + fn try_from(value: &[u8]) -> Result { + if value.len() > crate::MAX_BUFFER_SIZE { + return Err(anyhow!("Provided slice is larger than MAX_BUFFER_SIZE.")); + } + + Ok(Self(value.try_into()?)) + } +} + +impl AsRef<[u8]> for DirectIOBuffer { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +impl AsMut<[u8]> for DirectIOBuffer { + fn as_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl Index for DirectIOBuffer +where + Idx: std::slice::SliceIndex<[u8], Output = [u8]>, +{ + type Output = Idx::Output; + fn index(&self, index: Idx) -> &Self::Output { + &self.0.as_slice()[index] + } +} diff --git a/src/recovery.rs b/src/recovery.rs index f28f8be..46a7262 100644 --- a/src/recovery.rs +++ b/src/recovery.rs @@ -109,7 +109,7 @@ impl Recover { stage: Stage::Intact, }; - if let Err(err) = self.input.read_exact(&mut buf.0) { + if let Err(err) = self.input.read_exact(&mut buf.as_mut()) { // If buf were zeroed out before every read, one could theoretically recover // part of that read given the assumption that all null values from the end to // the first non-null value are unread, and some further padding from the last @@ -136,7 +136,7 @@ impl Recover { if cluster.stage == Stage::Intact { self.output - .write_all(&buf.0[0..buf_capacity]) + .write_all(&buf[0..buf_capacity]) .context("Failed to write data to output file")?; }