Skip to content

Commit c8cc20f

Browse files
committed
LfnBuffer now borrows its contents.
Removes a bunch of const generics from the API.
1 parent 1ed2c1a commit c8cc20f

File tree

6 files changed

+27
-29
lines changed

6 files changed

+27
-29
lines changed

examples/shell.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ impl Context {
231231
fn dir(&self, path: &Path) -> Result<(), Error> {
232232
println!("Directory listing of {:?}", path);
233233
let dir = self.resolve_existing_directory(path)?;
234-
let mut lfn_buffer = LfnBuffer::<256>::new();
234+
let mut storage = [0u8; 128];
235+
let mut lfn_buffer = LfnBuffer::new(&mut storage);
235236
dir.iterate_dir_lfn(&mut lfn_buffer, |entry, lfn| {
236237
if !entry.attributes.is_volume() {
237238
print!(

src/fat/volume.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,10 @@ impl FatVolume {
540540
/// including the Long File Name.
541541
///
542542
/// Useful for performing directory listings.
543-
pub(crate) fn iterate_dir_lfn<D, F, const N: usize>(
543+
pub(crate) fn iterate_dir_lfn<D, F>(
544544
&self,
545545
block_cache: &mut BlockCache<D>,
546-
lfn_buffer: &mut LfnBuffer<N>,
546+
lfn_buffer: &mut LfnBuffer<'_>,
547547
dir_info: &DirectoryInfo,
548548
mut func: F,
549549
) -> Result<(), Error<D::Error>>
@@ -559,9 +559,9 @@ impl FatVolume {
559559
}
560560

561561
impl SeqState {
562-
fn update<const N: usize>(
562+
fn update(
563563
self,
564-
lfn_buffer: &mut LfnBuffer<N>,
564+
lfn_buffer: &mut LfnBuffer<'_>,
565565
start: bool,
566566
sequence: u8,
567567
csum: u8,

src/filesystem/directory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ where
176176
/// object is already locked in order to do the iteration.
177177
///
178178
/// </div>
179-
pub fn iterate_dir_lfn<F, const N: usize>(
179+
pub fn iterate_dir_lfn<F>(
180180
&self,
181-
lfn_buffer: &mut LfnBuffer<N>,
181+
lfn_buffer: &mut LfnBuffer<'_>,
182182
func: F,
183183
) -> Result<(), Error<D::Error>>
184184
where

src/filesystem/filename.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,9 @@ impl core::fmt::Debug for ShortFileName {
221221
}
222222

223223
/// Used to store a Long File Name
224-
///
225-
/// The const generic specifies the maximum capacity in bytes.
226-
pub struct LfnBuffer<const N: usize> {
224+
pub struct LfnBuffer<'a> {
227225
/// We fill this buffer in from the back
228-
inner: [u8; N],
226+
inner: &'a mut [u8],
229227
/// How many bytes are free.
230228
///
231229
/// This is also the byte index the string starts from.
@@ -234,19 +232,20 @@ pub struct LfnBuffer<const N: usize> {
234232
overflow: bool,
235233
}
236234

237-
impl<const N: usize> LfnBuffer<N> {
238-
/// Create a new, empty, LFN Buffer
239-
pub fn new() -> LfnBuffer<N> {
235+
impl<'a> LfnBuffer<'a> {
236+
/// Create a new, empty, LFN Buffer using the given mutable slice as its storage.
237+
pub fn new(storage: &'a mut [u8]) -> LfnBuffer<'a> {
238+
let len = storage.len();
240239
LfnBuffer {
241-
inner: [0u8; N],
242-
free: N,
240+
inner: storage,
241+
free: len,
243242
overflow: false,
244243
}
245244
}
246245

247246
/// Empty out this buffer
248247
pub fn clear(&mut self) {
249-
self.free = N;
248+
self.free = self.inner.len();
250249
self.overflow = false;
251250
}
252251

@@ -291,12 +290,6 @@ impl<const N: usize> LfnBuffer<N> {
291290
}
292291
}
293292

294-
impl<const N: usize> core::default::Default for LfnBuffer<N> {
295-
fn default() -> Self {
296-
LfnBuffer::new()
297-
}
298-
}
299-
300293
// ****************************************************************************
301294
//
302295
// Unit Tests
@@ -402,7 +395,8 @@ mod test {
402395

403396
#[test]
404397
fn one_piece() {
405-
let mut buf: LfnBuffer<64> = LfnBuffer::new();
398+
let mut storage = [0u8; 64];
399+
let mut buf: LfnBuffer = LfnBuffer::new(&mut storage);
406400
buf.push(&[
407401
0x0030, 0x0031, 0x0032, 0x0033, 0x2202, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
408402
0xFFFF, 0xFFFF,
@@ -412,7 +406,8 @@ mod test {
412406

413407
#[test]
414408
fn two_piece() {
415-
let mut buf: LfnBuffer<64> = LfnBuffer::new();
409+
let mut storage = [0u8; 64];
410+
let mut buf: LfnBuffer = LfnBuffer::new(&mut storage);
416411
buf.push(&[
417412
0x0030, 0x0031, 0x0032, 0x0033, 0x2202, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
418413
0xFFFF, 0xFFFF,

src/volume_mgr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ where
435435
/// object is already locked in order to do the iteration.
436436
///
437437
/// </div>
438-
pub fn iterate_dir_lfn<F, const N: usize>(
438+
pub fn iterate_dir_lfn<F>(
439439
&self,
440440
directory: RawDirectory,
441-
lfn_buffer: &mut LfnBuffer<N>,
441+
lfn_buffer: &mut LfnBuffer<'_>,
442442
func: F,
443443
) -> Result<(), Error<D::Error>>
444444
where

tests/directories.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ fn fat16_root_directory_listing() {
101101
];
102102

103103
let mut listing = Vec::new();
104-
let mut lfn_buffer: LfnBuffer<128> = LfnBuffer::new();
104+
let mut storage = [0u8; 128];
105+
let mut lfn_buffer: LfnBuffer = LfnBuffer::new(&mut storage);
105106

106107
volume_mgr
107108
.iterate_dir_lfn(root_dir, &mut lfn_buffer, |d, opt_lfn| {
@@ -288,7 +289,8 @@ fn fat32_root_directory_listing() {
288289
];
289290

290291
let mut listing = Vec::new();
291-
let mut lfn_buffer: LfnBuffer<128> = LfnBuffer::new();
292+
let mut storage = [0u8; 128];
293+
let mut lfn_buffer: LfnBuffer = LfnBuffer::new(&mut storage);
292294

293295
volume_mgr
294296
.iterate_dir_lfn(root_dir, &mut lfn_buffer, |d, opt_lfn| {

0 commit comments

Comments
 (0)