Skip to content

Commit 7b15cf5

Browse files
committed
impl Drop for async File & Volume with embassy_futures::block_on
1 parent e623266 commit 7b15cf5

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rust-version = "1.76"
1717
bisync = "0.3.0"
1818
byteorder = {version = "1", default-features = false}
1919
defmt = {version = "0.3", optional = true}
20+
embassy-futures = "0.1.1"
2021
embedded-hal = "1.0.0"
2122
embedded-hal-async = "1.0.0"
2223
embedded-io = "0.6.1"

src/inner/filesystem/files.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::super::super::{bisync, only_sync};
1+
use super::super::super::{bisync, only_sync, only_async};
22
use super::super::super::{ErrorType, Read, Seek, SeekFrom, Write};
33
use super::super::{
44
filesystem::{ClusterId, DirEntry, Handle},
@@ -49,8 +49,10 @@ impl RawFile {
4949
/// error that may occur will be ignored. To handle potential errors, use
5050
/// the [`File::close`] method.
5151
///
52-
/// For async Files, async drop does not exist in Rust, so you *must* call
53-
/// [`File::close`] when you are done with a File.
52+
/// For async `Files`, the implementation of [`Drop`] blocks with [`embassy_futures::block_on`]
53+
/// because there is no way to `.await` inside [`Drop::drop`]. If you would prefer
54+
/// to rely on the async executor you are already using, call [`File::close`]
55+
/// manually instead of letting the `File` drop.
5456
pub struct File<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize>
5557
where
5658
D: BlockDevice,
@@ -150,7 +152,6 @@ where
150152
}
151153
}
152154

153-
// async drop does not yet exist :(
154155
#[only_sync]
155156
impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize> Drop
156157
for File<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
@@ -163,6 +164,18 @@ where
163164
}
164165
}
165166

167+
#[only_async]
168+
impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize> Drop
169+
for File<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
170+
where
171+
D: BlockDevice,
172+
T: TimeSource,
173+
{
174+
fn drop(&mut self) {
175+
_ = embassy_futures::block_on(self.volume_mgr.close_file(self.raw_file));
176+
}
177+
}
178+
166179
impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize>
167180
core::fmt::Debug for File<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
168181
where

src/inner/mod.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::fmt::Debug;
1919
use embedded_io::ErrorKind;
2020
use filesystem::Handle;
2121

22-
use super::{bisync, only_sync};
22+
use super::{bisync, only_sync, only_async};
2323

2424
#[doc(inline)]
2525
pub use blockdevice::{Block, BlockCache, BlockCount, BlockDevice, BlockIdx};
@@ -210,8 +210,10 @@ impl RawVolume {
210210
/// any error that may occur will be ignored. To handle potential errors, use
211211
/// the [`Volume::close`] method.
212212
///
213-
/// For async Volumes, async drop does not exist in Rust, so you *must* call
214-
/// [`Volume::close`] when you are done with a Volume.
213+
/// For async `Volumes`, the implementation of [`Drop`] blocks with [`embassy_futures::block_on`]
214+
/// because there is no way to `.await` inside [`Drop::drop`]. If you would prefer
215+
/// to rely on the async executor you are already using, call [`Volume::close`]
216+
/// manually instead of letting the `Volume` drop.
215217
pub struct Volume<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize>
216218
where
217219
D: BlockDevice,
@@ -268,7 +270,6 @@ where
268270
}
269271
}
270272

271-
// async drop does not yet exist :(
272273
#[only_sync]
273274
impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize> Drop
274275
for Volume<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
@@ -281,6 +282,18 @@ where
281282
}
282283
}
283284

285+
#[only_async]
286+
impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize> Drop
287+
for Volume<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
288+
where
289+
D: BlockDevice,
290+
T: TimeSource,
291+
{
292+
fn drop(&mut self) {
293+
_ = embassy_futures::block_on(self.volume_mgr.close_volume(self.raw_volume));
294+
}
295+
}
296+
284297
impl<'a, D, T, const MAX_DIRS: usize, const MAX_FILES: usize, const MAX_VOLUMES: usize>
285298
core::fmt::Debug for Volume<'a, D, T, MAX_DIRS, MAX_FILES, MAX_VOLUMES>
286299
where

0 commit comments

Comments
 (0)