#random-data #path #auto-impl #per-for-system #system-random

sys_traits

Trait per function for system related functionality

29 releases

new 0.1.22 Jan 15, 2026
0.1.21 Dec 7, 2025
0.1.19 Nov 5, 2025
0.1.17 Jul 14, 2025
0.1.6 Dec 31, 2024

#107 in Filesystem

Download history 12824/week @ 2025-09-28 10908/week @ 2025-10-05 10322/week @ 2025-10-12 11835/week @ 2025-10-19 14416/week @ 2025-10-26 11122/week @ 2025-11-02 11614/week @ 2025-11-09 17698/week @ 2025-11-16 21283/week @ 2025-11-23 39847/week @ 2025-11-30 43834/week @ 2025-12-07 53105/week @ 2025-12-14 27176/week @ 2025-12-21 26366/week @ 2025-12-28 38020/week @ 2026-01-04 22396/week @ 2026-01-11

119,121 downloads per month
Used in 93 crates (27 directly)

MIT license

170KB
5.5K SLoC

sys_traits

Trait per function for system related functionality.

Write functions that specify only the system functions they need.

use sys_traits::FsWriteFile;
use sys_traits::SystemRandom;

pub fn write_random_data<TSys: FsWriteFile + SystemRandom>(
  sys: &TSys,
  file_path: &Path,
) -> std::io::Result<()> {
  let mut buf = [0u8; 16];
  sys.sys_random(&mut buf)?;
  sys.fs_write_file(file_path, buf)
}

Now a caller only needs to provide a type that implements those two functions.

#[sys_traits::auto_impl]

Use the #[sys_traits::auto_impl] macro to reduce boilerplate when wanting to automatically implement a trait for T when T implements the required traits.

This is useful for aliasing and reducing verbosity when using this crate.

+#[sys_traits::auto_impl]
pub trait WriteRandomDataSys: FsWriteFile + SystemRandom
{
}

-impl<T> DenoResolverSys for T where T: FsWriteFile + SystemRandom
-{
-}

Implementations

Comes with two implementations that implement all the traits.

  • sys_traits::impl::RealSys - A real implementation of the current system.
  • sys_traits::impl::InMemorySys - An in-memory system useful for testing.

Creating an implementation

To create an implementation you must implement the traits; however, some traits require implementing Base<TraitName> traits instead. For example, instead of implementing FsWrite, you must implement BaseFsWrite:

pub struct MyCustomFileSystem;

impl sys_traits::BaseFsWrite for MyCustomFileSystem {
  fn base_fs_write(&self, path: &Path, data: &[u8]) -> std::io::Result<()> {
    // ...
  }
}

The sys_traits::FsWrite trait gets automatically implemented for this as its definition is:

pub trait FsWrite: BaseFsWrite {
  #[inline]
  fn fs_write(
    &self,
    path: impl AsRef<Path>,
    data: impl AsRef<[u8]>,
  ) -> std::io::Result<()> {
    self.base_fs_write(path.as_ref(), data.as_ref())
  }
}

impl<T: BaseFsWrite> FsWrite for T {}

There's two reasons for this:

  1. You can't box traits with impl ....
  2. By design it limits code generation of multiple kinds of impl AsRef<Path> and impl AsRef<[u8]> to only being a single statement.

Dependencies

~0.1–13MB
~103K SLoC