Cleanup through additional impls for DirectIOBuffer.

This commit is contained in:
Cutieguwu
2026-01-18 12:03:23 -05:00
parent 6ecc43dedf
commit e31ff33277
2 changed files with 39 additions and 3 deletions

View File

@@ -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<Self, Self::Error> {
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<Idx> Index<Idx> 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]
}
}

View File

@@ -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")?;
}