removed generic from FatFs, made inner a dyn now
This commit is contained in:
parent
b3d87687dd
commit
a08a84c3e4
7 changed files with 270 additions and 78 deletions
|
|
@ -1,17 +1,17 @@
|
|||
use std::io::Read;
|
||||
|
||||
use crate::FatFs;
|
||||
use crate::subslice::SubSlice;
|
||||
use crate::utils::replace;
|
||||
use crate::{FatFs, SliceLike};
|
||||
|
||||
pub struct ClusterChainReader<'a, S: SliceLike> {
|
||||
sub_slice: SubSlice<'a, S>,
|
||||
pub struct ClusterChainReader<'a> {
|
||||
sub_slice: SubSlice<'a>,
|
||||
|
||||
next_cluster: Option<u32>,
|
||||
}
|
||||
|
||||
impl<'a, S: SliceLike> ClusterChainReader<'a, S> {
|
||||
pub fn new(fat_fs: &'a FatFs<S>, first_cluster: u32) -> ClusterChainReader<'a, S> {
|
||||
impl<'a> ClusterChainReader<'a> {
|
||||
pub fn new(fat_fs: &'a FatFs, first_cluster: u32) -> ClusterChainReader<'a> {
|
||||
let next_cluster = fat_fs.next_cluster(first_cluster).unwrap_or(None);
|
||||
|
||||
let sub_slice = fat_fs.cluster_as_subslice(first_cluster);
|
||||
|
|
@ -39,7 +39,7 @@ impl<'a, S: SliceLike> ClusterChainReader<'a, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, S: SliceLike> Read for ClusterChainReader<'a, S> {
|
||||
impl<'a> Read for ClusterChainReader<'a> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
if self.sub_slice.is_empty() {
|
||||
if !self.next_cluster() {
|
||||
|
|
|
|||
|
|
@ -26,14 +26,6 @@ pub trait SliceLike {
|
|||
fn read_at_offset(&mut self, offset: u64, buf: &mut [u8]) -> std::io::Result<()>;
|
||||
|
||||
fn write_at_offset(&mut self, offset: u64, bytes: &[u8]) -> std::io::Result<()>;
|
||||
|
||||
fn read_array_at_offset<const N: usize>(&mut self, offset: u64) -> std::io::Result<[u8; N]> {
|
||||
let mut buf = [0; N];
|
||||
|
||||
self.read_at_offset(offset, &mut buf)?;
|
||||
|
||||
Ok(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl SliceLike for &mut [u8] {
|
||||
|
|
@ -87,8 +79,8 @@ impl SliceLike for std::fs::File {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct FatFs<S: SliceLike> {
|
||||
inner: Rc<RefCell<S>>,
|
||||
pub struct FatFs {
|
||||
inner: Rc<RefCell<dyn SliceLike>>,
|
||||
|
||||
fat_offset: u64,
|
||||
fat_size: usize,
|
||||
|
|
@ -106,15 +98,18 @@ pub struct FatFs<S: SliceLike> {
|
|||
fat: fat::Fat,
|
||||
}
|
||||
|
||||
impl<S: SliceLike> FatFs<S> {
|
||||
pub fn load(mut data: S) -> anyhow::Result<FatFs<S>> {
|
||||
let bpb_bytes: [u8; 512] = data.read_array_at_offset(0)?;
|
||||
impl FatFs {
|
||||
pub fn load(data: Rc<RefCell<dyn SliceLike>>) -> anyhow::Result<FatFs> {
|
||||
let mut bpb_bytes = [0; 512];
|
||||
|
||||
data.borrow_mut().read_at_offset(0, &mut bpb_bytes)?;
|
||||
|
||||
let bpb = bpb::Bpb::load(&bpb_bytes)?;
|
||||
|
||||
let mut fat_buf = vec![0; bpb.fat_len_bytes()];
|
||||
|
||||
data.read_at_offset(bpb.fat_offset(), &mut fat_buf)?;
|
||||
data.borrow_mut()
|
||||
.read_at_offset(bpb.fat_offset(), &mut fat_buf)?;
|
||||
|
||||
let fat = fat::Fat::new(bpb.fat_type(), &fat_buf, bpb.count_of_clusters());
|
||||
|
||||
|
|
@ -144,8 +139,6 @@ impl<S: SliceLike> FatFs<S> {
|
|||
|
||||
let bytes_per_cluster = bpb.bytes_per_cluster();
|
||||
|
||||
let data = Rc::new(RefCell::new(data));
|
||||
|
||||
Ok(FatFs {
|
||||
inner: data,
|
||||
fat_offset,
|
||||
|
|
@ -184,13 +177,13 @@ impl<S: SliceLike> FatFs<S> {
|
|||
self.fat().get_next_cluster(cluster)
|
||||
}
|
||||
|
||||
pub fn cluster_as_subslice_mut(&mut self, cluster: u32) -> SubSliceMut<'_, S> {
|
||||
pub fn cluster_as_subslice_mut(&mut self, cluster: u32) -> SubSliceMut<'_> {
|
||||
let offset = self.data_cluster_to_offset(cluster);
|
||||
|
||||
SubSliceMut::new(self, offset, self.bytes_per_cluster)
|
||||
}
|
||||
|
||||
pub fn cluster_as_subslice(&self, cluster: u32) -> SubSlice<'_, S> {
|
||||
pub fn cluster_as_subslice(&self, cluster: u32) -> SubSlice<'_> {
|
||||
let offset = self.data_cluster_to_offset(cluster);
|
||||
|
||||
SubSlice::new(self, offset, self.bytes_per_cluster)
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
use std::fmt::Debug;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use crate::{FatFs, SliceLike};
|
||||
use crate::FatFs;
|
||||
|
||||
pub struct SubSliceMut<'a, S: SliceLike> {
|
||||
fat_fs: &'a mut FatFs<S>,
|
||||
pub struct SubSliceMut<'a> {
|
||||
fat_fs: &'a mut FatFs,
|
||||
|
||||
offset: u64,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<S: SliceLike> Debug for SubSliceMut<'_, S> {
|
||||
impl Debug for SubSliceMut<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("SubSliceMut")
|
||||
.field("offset", &self.offset)
|
||||
|
|
@ -19,8 +19,8 @@ impl<S: SliceLike> Debug for SubSliceMut<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: SliceLike> SubSliceMut<'_, S> {
|
||||
pub fn new(fat_fs: &mut FatFs<S>, offset: u64, len: usize) -> SubSliceMut<'_, S> {
|
||||
impl SubSliceMut<'_> {
|
||||
pub fn new(fat_fs: &mut FatFs, offset: u64, len: usize) -> SubSliceMut<'_> {
|
||||
SubSliceMut {
|
||||
fat_fs,
|
||||
offset,
|
||||
|
|
@ -29,7 +29,7 @@ impl<S: SliceLike> SubSliceMut<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: SliceLike> SubSliceMut<'_, S> {
|
||||
impl SubSliceMut<'_> {
|
||||
pub fn len(&self) -> usize {
|
||||
self.len
|
||||
}
|
||||
|
|
@ -39,7 +39,7 @@ impl<S: SliceLike> SubSliceMut<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: SliceLike> Read for SubSliceMut<'_, S> {
|
||||
impl Read for SubSliceMut<'_> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let bytes_to_read = self.len.min(buf.len());
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ impl<S: SliceLike> Read for SubSliceMut<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: SliceLike> Write for SubSliceMut<'_, S> {
|
||||
impl Write for SubSliceMut<'_> {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
let bytes_to_write = self.len.min(buf.len());
|
||||
|
||||
|
|
@ -75,14 +75,14 @@ impl<S: SliceLike> Write for SubSliceMut<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct SubSlice<'a, S: SliceLike> {
|
||||
fat_fs: &'a FatFs<S>,
|
||||
pub struct SubSlice<'a> {
|
||||
fat_fs: &'a FatFs,
|
||||
|
||||
offset: u64,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<S: SliceLike> Debug for SubSlice<'_, S> {
|
||||
impl Debug for SubSlice<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("SubSliceMut")
|
||||
.field("offset", &self.offset)
|
||||
|
|
@ -91,8 +91,8 @@ impl<S: SliceLike> Debug for SubSlice<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: SliceLike> SubSlice<'_, S> {
|
||||
pub fn new(fat_fs: &FatFs<S>, offset: u64, len: usize) -> SubSlice<'_, S> {
|
||||
impl SubSlice<'_> {
|
||||
pub fn new(fat_fs: &FatFs, offset: u64, len: usize) -> SubSlice<'_> {
|
||||
SubSlice {
|
||||
fat_fs,
|
||||
offset,
|
||||
|
|
@ -100,11 +100,11 @@ impl<S: SliceLike> SubSlice<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn fat_fs(&self) -> &FatFs<S> {
|
||||
pub fn fat_fs(&self) -> &FatFs {
|
||||
self.fat_fs
|
||||
}
|
||||
|
||||
pub fn fat_fs_mut(&self) -> &FatFs<S> {
|
||||
pub fn fat_fs_mut(&self) -> &FatFs {
|
||||
self.fat_fs
|
||||
}
|
||||
|
||||
|
|
@ -116,14 +116,14 @@ impl<S: SliceLike> SubSlice<'_, S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, S: SliceLike> SubSlice<'a, S> {
|
||||
impl<'a> SubSlice<'a> {
|
||||
/// releases the inner &FatFs, consuming self in the process
|
||||
pub fn release(self) -> &'a FatFs<S> {
|
||||
pub fn release(self) -> &'a FatFs {
|
||||
self.fat_fs
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: SliceLike> Read for SubSlice<'_, S> {
|
||||
impl Read for SubSlice<'_> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let bytes_to_read = self.len.min(buf.len());
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue