fs4 1.1.0

No libc, pure Rust cross-platform file locks. Original fs2, now supports async and replace libc by rustix.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
// The `cfg_<feature>!` macros below are only invoked inside
// feature-gated modules -- every call site is itself behind
// `#[cfg(feature = "...")]` or inside the Unix/Windows backend
// trees. With `--no-default-features` (or on targets where neither
// `cfg(unix)` nor `cfg(windows)` matches, e.g. `wasm32-wasi*`), all
// call sites compile out, so the macros appear unused. Silence the
// lint at the crate level rather than shadowing each definition.
#![allow(unexpected_cfgs, unstable_name_collisions, unused_macros)]

#[cfg(windows)]
extern crate windows_sys;

macro_rules! cfg_async_std {
    ($($item:item)*) => {
        $(
            #[cfg(feature = "async-std")]
            #[cfg_attr(docsrs, doc(cfg(feature = "async-std")))]
            $item
        )*
    }
}

macro_rules! cfg_fs_err2 {
    ($($item:item)*) => {
        $(
            #[cfg(feature = "fs-err2")]
            #[cfg_attr(docsrs, doc(cfg(feature = "fs-err2")))]
            $item
        )*
    }
}

macro_rules! cfg_fs_err2_tokio {
    ($($item:item)*) => {
        $(
            #[cfg(feature = "fs-err2-tokio")]
            #[cfg_attr(docsrs, doc(cfg(feature = "fs-err2-tokio")))]
            $item
        )*
    }
}

macro_rules! cfg_fs_err3 {
    ($($item:item)*) => {
        $(
            #[cfg(feature = "fs-err3")]
            #[cfg_attr(docsrs, doc(cfg(feature = "fs-err3")))]
            $item
        )*
    }
}

macro_rules! cfg_fs_err3_tokio {
    ($($item:item)*) => {
        $(
            #[cfg(feature = "fs-err3-tokio")]
            #[cfg_attr(docsrs, doc(cfg(feature = "fs-err3-tokio")))]
            $item
        )*
    }
}

macro_rules! cfg_smol {
    ($($item:item)*) => {
        $(
            #[cfg(feature = "smol")]
            #[cfg_attr(docsrs, doc(cfg(feature = "smol")))]
            $item
        )*
    }
}

macro_rules! cfg_tokio {
    ($($item:item)*) => {
        $(
            #[cfg(feature = "tokio")]
            #[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
            $item
        )*
    }
}

macro_rules! cfg_sync {
  ($($item:item)*) => {
      $(
          #[cfg(feature = "sync")]
          #[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
          $item
      )*
  }
}

macro_rules! cfg_async {
    ($($item:item)*) => {
        $(
            #[cfg(any(
                feature = "smol",
                feature = "async-std",
                feature = "tokio",
                feature = "fs-err2-tokio",
                feature = "fs-err3-tokio",
            ))]
            #[cfg_attr(docsrs, doc(cfg(any(
                feature = "smol",
                feature = "async-std",
                feature = "tokio",
                feature = "fs-err2-tokio",
                feature = "fs-err3-tokio",
            ))))]
            $item
        )*
    }
}

#[cfg(unix)]
mod unix;
#[cfg(unix)]
use unix as sys;

#[cfg(windows)]
mod windows;

#[cfg(windows)]
use windows as sys;

// The file-extension traits (`FileExt`, `AsyncFileExt`) and the stats
// API are only implementable on targets with a real `sys` backend.
// Anywhere else (notably `wasm32-wasi*`, where `target_family = "wasm"`
// so neither `cfg(unix)` nor `cfg(windows)` matches and rustix does
// not expose `statvfs` / `flock` / `fallocate`) the crate compiles
// down to just the shared data types below.
#[cfg(any(unix, windows))]
mod file_ext;

#[cfg(all(feature = "fs-err2", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs-err2")))]
pub mod fs_err2 {
  pub use crate::FileExt;
}

#[cfg(all(feature = "fs-err3", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs-err3")))]
pub mod fs_err3 {
  pub use crate::FileExt;
}

#[cfg(all(feature = "async-std", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std")))]
pub mod async_std {
  pub use crate::{AsyncFileExt, DynAsyncFileExt};
}

#[cfg(all(feature = "fs-err2-tokio", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs-err2-tokio")))]
pub mod fs_err2_tokio {
  pub use crate::{AsyncFileExt, DynAsyncFileExt};
}

#[cfg(all(feature = "fs-err3-tokio", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs-err3-tokio")))]
pub mod fs_err3_tokio {
  pub use crate::{AsyncFileExt, DynAsyncFileExt};
}

#[cfg(all(feature = "smol", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(feature = "smol")))]
pub mod smol {
  pub use crate::{AsyncFileExt, DynAsyncFileExt};
}

#[cfg(all(feature = "tokio", any(unix, windows)))]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub mod tokio {
  pub use crate::{AsyncFileExt, DynAsyncFileExt};
}

mod fs_stats;
pub use fs_stats::FsStats;

mod try_lock_error;
pub use try_lock_error::TryLockError;

use std::io::Result;
#[cfg(any(unix, windows))]
use std::path::Path;

/// Get the stats of the file system containing the provided path.
#[cfg(any(unix, windows))]
pub fn statvfs<P>(path: P) -> Result<FsStats>
where
  P: AsRef<Path>,
{
  sys::statvfs(path.as_ref())
}

/// Returns the number of free bytes in the file system containing the provided
/// path.
#[cfg(any(unix, windows))]
pub fn free_space<P>(path: P) -> Result<u64>
where
  P: AsRef<Path>,
{
  statvfs(path).map(|stat| stat.free_space)
}

/// Returns the available space in bytes to non-privileged users in the file
/// system containing the provided path.
#[cfg(any(unix, windows))]
pub fn available_space<P>(path: P) -> Result<u64>
where
  P: AsRef<Path>,
{
  statvfs(path).map(|stat| stat.available_space)
}

/// Returns the total space in bytes in the file system containing the provided
/// path.
#[cfg(any(unix, windows))]
pub fn total_space<P>(path: P) -> Result<u64>
where
  P: AsRef<Path>,
{
  statvfs(path).map(|stat| stat.total_space)
}

/// Returns the filesystem's disk space allocation granularity in bytes.
/// The provided path may be for any file in the filesystem.
///
/// On Posix, this is equivalent to the filesystem's block size.
/// On Windows, this is equivalent to the filesystem's cluster size.
#[cfg(any(unix, windows))]
pub fn allocation_granularity<P>(path: P) -> Result<u64>
where
  P: AsRef<Path>,
{
  statvfs(path).map(|stat| stat.allocation_granularity)
}

mod sealed {
  pub trait Sealed {}

  impl<F: Sealed + ?Sized> Sealed for &F {}
}

/// Extension trait for file which provides allocation and locking methods.
///
/// This trait is sealed and cannot be implemented for types outside of `fs4`.
///
/// ## Notes on File Locks
///
/// This library provides whole-file locks in both shared (read) and exclusive
/// (read-write) varieties.
///
/// File locks are a cross-platform hazard since the file lock APIs exposed by
/// operating system kernels vary in subtle and not-so-subtle ways.
///
/// The API exposed by this library can be safely used across platforms as long
/// as the following rules are followed:
///
///   * Multiple locks should not be created on an individual `File` instance
///     concurrently.
///   * Duplicated files should not be locked without great care.
///   * Files to be locked should be opened with at least read or write
///     permissions.
///   * File locks may only be relied upon to be advisory.
///
/// File locks are released automatically when the file handle is closed (for
/// example when the owning `File` is dropped), so calling [`FileExt::unlock`]
/// explicitly is optional.
///
/// File locks are implemented with
/// [`flock(2)`](http://man7.org/linux/man-pages/man2/flock.2.html) on Unix and
/// [`LockFileEx`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex)
/// on Windows.
pub trait FileExt: sealed::Sealed {
  /// Returns the amount of physical space allocated for a file.
  fn allocated_size(&self) -> Result<u64>;

  /// Ensures that at least `len` bytes of disk space are allocated for the
  /// file. After a successful call to `allocate`, subsequent writes to the
  /// file within the specified length are guaranteed not to fail because of
  /// lack of disk space.
  ///
  /// On most platforms the file's logical size is also extended to `len`
  /// bytes. On Windows, if the file's existing cluster-aligned allocation
  /// already covers `len`, the logical size is left unchanged to work around
  /// buffered-I/O quirks observed when the end-of-file pointer is moved
  /// inside an already-allocated cluster.
  fn allocate(&self, len: u64) -> Result<()>;

  /// Acquires a shared lock on the file, blocking until the lock can be
  /// acquired.
  fn lock_shared(&self) -> Result<()>;

  /// Acquires an exclusive lock on the file, blocking until the lock can be
  /// acquired.
  ///
  /// This is the blocking counterpart of [`FileExt::try_lock`]. It mirrors
  /// [`std::fs::File::lock`].
  fn lock(&self) -> Result<()>;

  /// Attempts to acquire a shared lock on the file, without blocking.
  ///
  /// Returns `Ok(())` if the lock was acquired, or
  /// `Err(`[`TryLockError::WouldBlock`](crate::TryLockError::WouldBlock)`)`
  /// if the file is currently locked. Mirrors
  /// [`std::fs::File::try_lock_shared`].
  fn try_lock_shared(&self) -> std::result::Result<(), TryLockError>;

  /// Attempts to acquire an exclusive lock on the file, without blocking.
  ///
  /// Returns `Ok(())` if the lock was acquired, or
  /// `Err(`[`TryLockError::WouldBlock`](crate::TryLockError::WouldBlock)`)`
  /// if the file is currently locked. Mirrors [`std::fs::File::try_lock`].
  fn try_lock(&self) -> std::result::Result<(), TryLockError>;

  /// Releases any lock held on the file. The lock is also released
  /// automatically when the file handle is closed.
  fn unlock(&self) -> Result<()>;
}

impl<F: FileExt + ?Sized> FileExt for &F {
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn allocated_size(&self) -> Result<u64> {
    <F as FileExt>::allocated_size(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn allocate(&self, len: u64) -> Result<()> {
    <F as FileExt>::allocate(*self, len)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn lock_shared(&self) -> Result<()> {
    <F as FileExt>::lock_shared(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn lock(&self) -> Result<()> {
    <F as FileExt>::lock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn try_lock_shared(&self) -> std::result::Result<(), TryLockError> {
    <F as FileExt>::try_lock_shared(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn try_lock(&self) -> std::result::Result<(), TryLockError> {
    <F as FileExt>::try_lock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn unlock(&self) -> Result<()> {
    <F as FileExt>::unlock(*self)
  }
}

/// Extension trait for file which provides allocation and locking methods.
///
/// ## Notes on File Locks
///
/// This library provides whole-file locks in both shared (read) and exclusive
/// (read-write) varieties.
///
/// File locks are a cross-platform hazard since the file lock APIs exposed by
/// operating system kernels vary in subtle and not-so-subtle ways.
///
/// The API exposed by this library can be safely used across platforms as long
/// as the following rules are followed:
///
///   * Multiple locks should not be created on an individual `File` instance
///     concurrently.
///   * Duplicated files should not be locked without great care.
///   * Files to be locked should be opened with at least read or write
///     permissions.
///   * File locks may only be relied upon to be advisory.
///
/// File locks are released automatically when the file handle is closed (for
/// example when the owning `File` is dropped), so calling [`AsyncFileExt::unlock`]
/// explicitly is optional.
///
/// File locks are implemented with
/// [`flock(2)`](http://man7.org/linux/man-pages/man2/flock.2.html) on Unix and
/// [`LockFileEx`](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex)
/// on Windows. The `lock_*` and `try_lock_*` methods are synchronous because
/// the underlying system calls are blocking. The separate
/// [`AsyncFileExt::unlock_async`] method is provided for convenience inside
/// async code, but the underlying `unlock` syscall is still blocking.
///
/// This trait is sealed and cannot be implemented for types outside of `fs4`.
pub trait AsyncFileExt: sealed::Sealed {
  /// Returns the amount of physical space allocated for a file.
  fn allocated_size(&self) -> impl core::future::Future<Output = Result<u64>>;

  /// Ensures that at least `len` bytes of disk space are allocated for the
  /// file. After a successful call to `allocate`, subsequent writes to the
  /// file within the specified length are guaranteed not to fail because of
  /// lack of disk space.
  ///
  /// On most platforms the file's logical size is also extended to `len`
  /// bytes. On Windows, if the file's existing cluster-aligned allocation
  /// already covers `len`, the logical size is left unchanged to work around
  /// buffered-I/O quirks observed when the end-of-file pointer is moved
  /// inside an already-allocated cluster.
  fn allocate(&self, len: u64) -> impl core::future::Future<Output = Result<()>>;

  /// Acquires a shared lock on the file, blocking until the lock can be
  /// acquired.
  fn lock_shared(&self) -> Result<()>;

  /// Acquires an exclusive lock on the file, blocking until the lock can be
  /// acquired. Mirrors [`std::fs::File::lock`].
  fn lock(&self) -> Result<()>;

  /// Attempts to acquire a shared lock on the file, without blocking.
  ///
  /// Returns `Ok(())` if the lock was acquired, or
  /// `Err(`[`TryLockError::WouldBlock`](crate::TryLockError::WouldBlock)`)`
  /// if the file is currently locked.
  fn try_lock_shared(&self) -> std::result::Result<(), crate::TryLockError>;

  /// Attempts to acquire an exclusive lock on the file, without blocking.
  ///
  /// Returns `Ok(())` if the lock was acquired, or
  /// `Err(`[`TryLockError::WouldBlock`](crate::TryLockError::WouldBlock)`)`
  /// if the file is currently locked.
  fn try_lock(&self) -> std::result::Result<(), crate::TryLockError>;

  /// Releases any lock held on the file. The lock is also released
  /// automatically when the file handle is closed.
  fn unlock(&self) -> Result<()>;

  /// Releases any lock held on the file.
  ///
  /// **Note:** This method is not truly async; the underlying system call is
  /// still blocking. It exists for convenience when used from an async
  /// context.
  fn unlock_async(&self) -> impl core::future::Future<Output = Result<()>>;
}

impl<F: AsyncFileExt + ?Sized> AsyncFileExt for &F {
  #[cfg_attr(not(tarpaulin), inline(always))]
  async fn allocated_size(&self) -> Result<u64> {
    <F as AsyncFileExt>::allocated_size(*self).await
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  async fn allocate(&self, len: u64) -> Result<()> {
    <F as AsyncFileExt>::allocate(*self, len).await
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn lock_shared(&self) -> Result<()> {
    <F as AsyncFileExt>::lock_shared(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn lock(&self) -> Result<()> {
    <F as AsyncFileExt>::lock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn try_lock_shared(&self) -> std::result::Result<(), crate::TryLockError> {
    <F as AsyncFileExt>::try_lock_shared(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn try_lock(&self) -> std::result::Result<(), crate::TryLockError> {
    <F as AsyncFileExt>::try_lock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn unlock(&self) -> Result<()> {
    <F as AsyncFileExt>::unlock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  async fn unlock_async(&self) -> Result<()> {
    <F as AsyncFileExt>::unlock_async(*self).await
  }
}

/// A heap-allocated, dynamically-typed `Send` future used by
/// [`DynAsyncFileExt`] to keep its methods object-safe.
pub type BoxFuture<'a, T> = core::pin::Pin<Box<dyn core::future::Future<Output = T> + Send + 'a>>;

/// Object-safe variant of [`AsyncFileExt`] returning boxed `Send` futures, so
/// it can be used behind a trait object (e.g. `Box<dyn DynAsyncFileExt>` or
/// `&dyn DynAsyncFileExt`).
///
/// [`AsyncFileExt`] uses return-position `impl Future`, which is not
/// object-safe; this trait wraps the same operations behind
/// [`BoxFuture`]s so the trait *can* be used as a trait object. Every type
/// that implements [`AsyncFileExt`] also implements `DynAsyncFileExt`.
///
/// Prefer [`AsyncFileExt`] for generic code (no allocation, no dynamic
/// dispatch); reach for `DynAsyncFileExt` only when type erasure is
/// required.
///
/// This trait is sealed and cannot be implemented for types outside of `fs4`.
pub trait DynAsyncFileExt: sealed::Sealed {
  /// Returns the amount of physical space allocated for a file.
  fn allocated_size(&self) -> BoxFuture<'_, Result<u64>>;

  /// Ensures that at least `len` bytes of disk space are allocated for the
  /// file. After a successful call to `allocate`, subsequent writes to the
  /// file within the specified length are guaranteed not to fail because of
  /// lack of disk space.
  ///
  /// On most platforms the file's logical size is also extended to `len`
  /// bytes. On Windows, if the file's existing cluster-aligned allocation
  /// already covers `len`, the logical size is left unchanged to work around
  /// buffered-I/O quirks observed when the end-of-file pointer is moved
  /// inside an already-allocated cluster.
  fn allocate(&self, len: u64) -> BoxFuture<'_, Result<()>>;

  /// Acquires a shared lock on the file, blocking until the lock can be
  /// acquired.
  fn lock_shared(&self) -> Result<()>;

  /// Acquires an exclusive lock on the file, blocking until the lock can be
  /// acquired. Mirrors [`std::fs::File::lock`].
  fn lock(&self) -> Result<()>;

  /// Attempts to acquire a shared lock on the file, without blocking.
  ///
  /// Returns `Ok(())` if the lock was acquired, or
  /// `Err(`[`TryLockError::WouldBlock`](crate::TryLockError::WouldBlock)`)`
  /// if the file is currently locked.
  fn try_lock_shared(&self) -> std::result::Result<(), crate::TryLockError>;

  /// Attempts to acquire an exclusive lock on the file, without blocking.
  ///
  /// Returns `Ok(())` if the lock was acquired, or
  /// `Err(`[`TryLockError::WouldBlock`](crate::TryLockError::WouldBlock)`)`
  /// if the file is currently locked.
  fn try_lock(&self) -> std::result::Result<(), crate::TryLockError>;

  /// Releases any lock held on the file. The lock is also released
  /// automatically when the file handle is closed.
  fn unlock(&self) -> Result<()>;

  /// Releases any lock held on the file.
  ///
  /// **Note:** This method is not truly async; the underlying system call is
  /// still blocking. It exists for convenience when used from an async
  /// context.
  fn unlock_async(&self) -> BoxFuture<'_, Result<()>>;
}

impl<F: DynAsyncFileExt + ?Sized> DynAsyncFileExt for &F {
  #[cfg_attr(not(tarpaulin), inline(always))]
  fn allocated_size(&self) -> BoxFuture<'_, Result<u64>> {
    <F as DynAsyncFileExt>::allocated_size(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn allocate(&self, len: u64) -> BoxFuture<'_, Result<()>> {
    <F as DynAsyncFileExt>::allocate(*self, len)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn lock_shared(&self) -> Result<()> {
    <F as DynAsyncFileExt>::lock_shared(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn lock(&self) -> Result<()> {
    <F as DynAsyncFileExt>::lock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn try_lock_shared(&self) -> std::result::Result<(), crate::TryLockError> {
    <F as DynAsyncFileExt>::try_lock_shared(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn try_lock(&self) -> std::result::Result<(), crate::TryLockError> {
    <F as DynAsyncFileExt>::try_lock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn unlock(&self) -> Result<()> {
    <F as DynAsyncFileExt>::unlock(*self)
  }

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn unlock_async(&self) -> BoxFuture<'_, Result<()>> {
    <F as DynAsyncFileExt>::unlock_async(*self)
  }
}

#[cfg(all(test, any(unix, windows)))]
mod tests {
  //! The `free_space` / `available_space` / `total_space` helpers
  //! each forward to `statvfs(...).map(|s| s.<field>)`. The
  //! `FsStats` getter tests in `fs_stats.rs` cover the field
  //! accessors; these tests cover the top-level forwarders (which
  //! were previously uncovered in CI per Codecov).
  //!
  //! Assertions are intentionally loose: we don't compare the three
  //! numbers across separate `statvfs` calls because that races
  //! with concurrent filesystem activity (other tests, the OS,
  //! etc.). Proving the call returned `Ok` with a plausible value
  //! is enough to exercise the forwarding path.
  extern crate tempfile;

  use super::*;

  fn tempdir() -> tempfile::TempDir {
    tempfile::TempDir::with_prefix("fs4").unwrap()
  }

  #[test]
  fn free_space_returns_ok() {
    let dir = tempdir();
    let free = free_space(dir.path()).unwrap();
    let total = total_space(dir.path()).unwrap();
    assert!(
      free <= total,
      "free_space ({free}) must not exceed total_space ({total})",
    );
  }

  #[test]
  fn available_space_returns_ok() {
    let dir = tempdir();
    let available = available_space(dir.path()).unwrap();
    let total = total_space(dir.path()).unwrap();
    assert!(
      available <= total,
      "available_space ({available}) must not exceed total_space ({total})",
    );
  }

  #[test]
  fn total_space_is_non_zero() {
    let dir = tempdir();
    assert!(
      total_space(dir.path()).unwrap() > 0,
      "total_space on a tempdir's volume should be non-zero",
    );
  }

  /// POSIX `statvfs` returns `ENOENT` for a path that doesn't
  /// exist, which is how we exercise the error-propagation branch
  /// of the three forwarders. Windows has different semantics:
  /// `GetVolumePathNameW` resolves any syntactically valid path to
  /// its volume root regardless of whether the path itself exists,
  /// so `statvfs(missing)` returns `Ok` on that platform.
  #[cfg(unix)]
  #[test]
  fn missing_path_errors() {
    let dir = tempdir();
    let missing = dir.path().join("definitely-does-not-exist");
    assert!(free_space(&missing).is_err());
    assert!(available_space(&missing).is_err());
    assert!(total_space(&missing).is_err());
  }
}