Skip to content

Commit 2e030d3

Browse files
feat(core): impl Drop for BlockingWrapper (#6036)
* feat(blocking): implement Drop trait for BlockingWrapper to ensure proper resource cleanup * remove a drop * rm white * add blocking test * fix(blocking): restrict Drop implementation for BlockingWrapper to specific trait bounds * try drop in async * Option<I> * fix(blocking): change Drop implementation to use block_on for inner cleanup * fix(blocking): relax trait bounds for Drop implementation in BlockingWrapper * test(blocking): add test_check to the blocking trials
1 parent 2d9c412 commit 2e030d3

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

core/src/layers/blocking.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,44 +267,57 @@ impl<A: Access> LayeredAccess for BlockingAccessor<A> {
267267

268268
pub struct BlockingWrapper<I> {
269269
handle: Handle,
270-
inner: I,
270+
inner: Option<I>,
271271
}
272272

273273
impl<I> BlockingWrapper<I> {
274274
fn new(handle: Handle, inner: I) -> Self {
275-
Self { handle, inner }
275+
Self {
276+
handle,
277+
inner: Some(inner),
278+
}
276279
}
277280
}
278281

279282
impl<I: oio::Read + 'static> oio::BlockingRead for BlockingWrapper<I> {
280283
fn read(&mut self) -> Result<Buffer> {
281-
self.handle.block_on(self.inner.read())
284+
self.handle.block_on(self.inner.as_mut().unwrap().read())
282285
}
283286
}
284287

285288
impl<I: oio::Write + 'static> oio::BlockingWrite for BlockingWrapper<I> {
286289
fn write(&mut self, bs: Buffer) -> Result<()> {
287-
self.handle.block_on(self.inner.write(bs))
290+
self.handle.block_on(self.inner.as_mut().unwrap().write(bs))
288291
}
289292

290293
fn close(&mut self) -> Result<Metadata> {
291-
self.handle.block_on(self.inner.close())
294+
self.handle.block_on(self.inner.as_mut().unwrap().close())
295+
}
296+
}
297+
298+
impl<I> Drop for BlockingWrapper<I> {
299+
fn drop(&mut self) {
300+
if let Some(inner) = self.inner.take() {
301+
self.handle.block_on(async move {
302+
drop(inner);
303+
});
304+
}
292305
}
293306
}
294307

295308
impl<I: oio::List> oio::BlockingList for BlockingWrapper<I> {
296309
fn next(&mut self) -> Result<Option<oio::Entry>> {
297-
self.handle.block_on(self.inner.next())
310+
self.handle.block_on(self.inner.as_mut().unwrap().next())
298311
}
299312
}
300313

301314
impl<I: oio::Delete + 'static> oio::BlockingDelete for BlockingWrapper<I> {
302315
fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
303-
self.inner.delete(path, args)
316+
self.inner.as_mut().unwrap().delete(path, args)
304317
}
305318

306319
fn flush(&mut self) -> Result<usize> {
307-
self.handle.block_on(self.inner.flush())
320+
self.handle.block_on(self.inner.as_mut().unwrap().flush())
308321
}
309322
}
310323

core/tests/behavior/blocking_list.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn tests(op: &Operator, tests: &mut Vec<Trial>) {
2828
if cap.read && cap.write && cap.copy && cap.blocking && cap.list {
2929
tests.extend(blocking_trials!(
3030
op,
31+
test_check,
3132
test_blocking_list_dir,
3233
test_blocking_list_non_exist_dir,
3334
test_blocking_list_not_exist_dir_with_recursive,
@@ -39,6 +40,13 @@ pub fn tests(op: &Operator, tests: &mut Vec<Trial>) {
3940
}
4041
}
4142

43+
/// Check should be OK.
44+
pub fn test_check(op: BlockingOperator) -> Result<()> {
45+
op.check().expect("operator check is ok");
46+
47+
Ok(())
48+
}
49+
4250
/// List dir should return newly created file.
4351
pub fn test_blocking_list_dir(op: BlockingOperator) -> Result<()> {
4452
let parent = uuid::Uuid::new_v4().to_string();

0 commit comments

Comments
 (0)