Cleanup through additional impls for DirectIOBuffer.
This commit is contained in:
38
src/io.rs
38
src/io.rs
@@ -1,9 +1,11 @@
|
|||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::{self, Seek, SeekFrom};
|
use std::io::{self, Seek, SeekFrom};
|
||||||
|
use std::ops::{Index, Range, RangeFrom};
|
||||||
|
use std::slice::SliceIndex;
|
||||||
|
|
||||||
use crate::cli::CONFIG;
|
use crate::cli::CONFIG;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::{Context, anyhow};
|
||||||
|
|
||||||
/// Get length of data stream.
|
/// Get length of data stream.
|
||||||
/// Physical length of data stream in bytes
|
/// Physical length of data stream in bytes
|
||||||
@@ -91,3 +93,37 @@ impl From<[u8; crate::MAX_BUFFER_SIZE]> for DirectIOBuffer {
|
|||||||
Self(value)
|
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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ impl Recover {
|
|||||||
stage: Stage::Intact,
|
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
|
// 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
|
// 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
|
// 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 {
|
if cluster.stage == Stage::Intact {
|
||||||
self.output
|
self.output
|
||||||
.write_all(&buf.0[0..buf_capacity])
|
.write_all(&buf[0..buf_capacity])
|
||||||
.context("Failed to write data to output file")?;
|
.context("Failed to write data to output file")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user