FatFs: moved data/inner behind Rc+RefCell
so that non-mut ref can read
This commit is contained in:
parent
17e5bfc27c
commit
fc7a0c4616
3 changed files with 27 additions and 17 deletions
|
|
@ -1,6 +1,5 @@
|
|||
use std::io::Read;
|
||||
|
||||
use crate::fat::Fatty;
|
||||
use crate::subslice::SubSlice;
|
||||
use crate::utils::replace;
|
||||
use crate::{FatFs, SliceLike};
|
||||
|
|
@ -12,7 +11,7 @@ pub struct ClusterChainReader<'a, S: SliceLike> {
|
|||
}
|
||||
|
||||
impl<'a, S: SliceLike> ClusterChainReader<'a, S> {
|
||||
pub fn new(fat_fs: &'a mut FatFs<S>, first_cluster: u32) -> ClusterChainReader<'a, S> {
|
||||
pub fn new(fat_fs: &'a FatFs<S>, first_cluster: u32) -> ClusterChainReader<'a, S> {
|
||||
let next_cluster = fat_fs.next_cluster(first_cluster).unwrap_or(None);
|
||||
|
||||
let sub_slice = fat_fs.cluster_as_subslice(first_cluster);
|
||||
|
|
|
|||
26
src/lib.rs
26
src/lib.rs
|
|
@ -1,4 +1,6 @@
|
|||
use std::io::{Read as _, Seek as _, SeekFrom, Write as _};
|
||||
use std::cell::RefCell;
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::dir::{DirIter, RegularDirEntry};
|
||||
use crate::fat::{FatError, Fatty};
|
||||
|
|
@ -94,7 +96,7 @@ impl SliceLike for std::fs::File {
|
|||
|
||||
#[allow(dead_code)]
|
||||
pub struct FatFs<S: SliceLike> {
|
||||
data: S,
|
||||
inner: Rc<RefCell<S>>,
|
||||
|
||||
fat_offset: u64,
|
||||
fat_size: usize,
|
||||
|
|
@ -150,8 +152,10 @@ impl<S: SliceLike> FatFs<S> {
|
|||
|
||||
let bytes_per_cluster = bpb.bytes_per_cluster();
|
||||
|
||||
let data = Rc::new(RefCell::new(data));
|
||||
|
||||
Ok(FatFs {
|
||||
data,
|
||||
inner: data,
|
||||
fat_offset,
|
||||
fat_size,
|
||||
root_dir_offset,
|
||||
|
|
@ -194,7 +198,7 @@ impl<S: SliceLike> FatFs<S> {
|
|||
SubSliceMut::new(self, offset, self.bytes_per_cluster)
|
||||
}
|
||||
|
||||
pub fn cluster_as_subslice(&mut self, cluster: u32) -> SubSlice<'_, S> {
|
||||
pub fn cluster_as_subslice(&self, cluster: u32) -> SubSlice<'_, S> {
|
||||
let offset = self.data_cluster_to_offset(cluster);
|
||||
|
||||
SubSlice::new(self, offset, self.bytes_per_cluster)
|
||||
|
|
@ -215,20 +219,20 @@ impl<S: SliceLike> FatFs<S> {
|
|||
|
||||
let mut data = vec![0; self.bytes_per_cluster];
|
||||
|
||||
self.data
|
||||
.read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?;
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
|
||||
inner.read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?;
|
||||
|
||||
while let Ok(Some(next_cluster)) = self.next_cluster(cluster) {
|
||||
cluster = next_cluster;
|
||||
|
||||
self.data
|
||||
.read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?;
|
||||
inner.read_at_offset(self.data_cluster_to_offset(cluster), &mut data)?;
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub fn root_dir_iter(&mut self) -> Box<dyn Iterator<Item = RegularDirEntry> + '_> {
|
||||
pub fn root_dir_iter(&self) -> Box<dyn Iterator<Item = RegularDirEntry> + '_> {
|
||||
// TODO: maybe wrap this in another RootDirIter enum, so we don't have to Box<dyn>
|
||||
|
||||
if let Some(root_dir_offset) = self.root_dir_offset {
|
||||
|
|
@ -248,4 +252,8 @@ impl<S: SliceLike> FatFs<S> {
|
|||
|
||||
Box::new(DirIter::new(cluster_iter))
|
||||
}
|
||||
|
||||
pub fn chain_reader(&self, first_cluster: u32) -> impl Read {
|
||||
iter::ClusterChainReader::new(self, first_cluster)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,8 @@ impl<S: SliceLike> Read for SubSliceMut<'_, S> {
|
|||
let bytes_to_read = self.len.min(buf.len());
|
||||
|
||||
self.fat_fs
|
||||
.data
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.read_at_offset(self.offset, &mut buf[..bytes_to_read])?;
|
||||
|
||||
self.offset += bytes_to_read as u64;
|
||||
|
|
@ -59,7 +60,8 @@ impl<S: SliceLike> Write for SubSliceMut<'_, S> {
|
|||
let bytes_to_write = self.len.min(buf.len());
|
||||
|
||||
self.fat_fs
|
||||
.data
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.write_at_offset(self.offset, &buf[..bytes_to_write])?;
|
||||
|
||||
self.offset += bytes_to_write as u64;
|
||||
|
|
@ -74,7 +76,7 @@ impl<S: SliceLike> Write for SubSliceMut<'_, S> {
|
|||
}
|
||||
|
||||
pub struct SubSlice<'a, S: SliceLike> {
|
||||
fat_fs: &'a mut FatFs<S>,
|
||||
fat_fs: &'a FatFs<S>,
|
||||
|
||||
offset: u64,
|
||||
len: usize,
|
||||
|
|
@ -90,7 +92,7 @@ impl<S: SliceLike> Debug for SubSlice<'_, S> {
|
|||
}
|
||||
|
||||
impl<S: SliceLike> SubSlice<'_, S> {
|
||||
pub fn new(fat_fs: &mut FatFs<S>, offset: u64, len: usize) -> SubSlice<'_, S> {
|
||||
pub fn new(fat_fs: &FatFs<S>, offset: u64, len: usize) -> SubSlice<'_, S> {
|
||||
SubSlice {
|
||||
fat_fs,
|
||||
offset,
|
||||
|
|
@ -115,7 +117,7 @@ impl<S: SliceLike> SubSlice<'_, S> {
|
|||
}
|
||||
|
||||
impl<'a, S: SliceLike> SubSlice<'a, S> {
|
||||
pub fn release(self) -> &'a mut FatFs<S> {
|
||||
pub fn release(self) -> &'a FatFs<S> {
|
||||
self.fat_fs
|
||||
}
|
||||
}
|
||||
|
|
@ -125,7 +127,8 @@ impl<S: SliceLike> Read for SubSlice<'_, S> {
|
|||
let bytes_to_read = self.len.min(buf.len());
|
||||
|
||||
self.fat_fs
|
||||
.data
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.read_at_offset(self.offset, &mut buf[..bytes_to_read])?;
|
||||
|
||||
self.offset += bytes_to_read as u64;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue