sys_traits
Trait per function for system related functionality.
Write functions that specify only the system functions they need.
use FsWriteFile;
use SystemRandom;
Now a caller only needs to provide a type that implements those two functions.
Implementations
Comes with two implementations that implement all the traits.
sys_traits::impl::RealSys
- A real implementation of the current system.- Automatically works with Wasm in Deno
- Will implement Node.js support once I need it (https://github.com/dsherret/sys_traits/issues/4)
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
:
;
The sys_traits::FsWrite
trait gets automatically implemented for this as its
definition is:
There's two reasons for this:
- You can't box traits with
impl ...
. - By design it limits code generation of multiple kinds of
impl AsRef<Path>
andimpl AsRef<[u8]>
to only being a single statement.