Crates.io | sys_traits |
lib.rs | sys_traits |
version | 0.1.17 |
created_at | 2024-12-23 15:20:20.698171+00 |
updated_at | 2025-07-14 18:28:06.297946+00 |
description | Trait per function for system related functionality. |
homepage | |
repository | https://github.com/dsherret/sys_traits |
max_upload_size | |
id | 1493145 |
size | 177,654 |
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
-{
-}
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.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:
impl ...
.impl AsRef<Path>
and impl AsRef<[u8]>
to only being a single statement.