Expand description
Cross-platform I/O redirection.
This crate provides a Redirectable<T> trait, platform-specific implementations of this trait,
and convenience methods for handling stdout and stderr redirection. This is most useful if you
can’t print to stdout or stderr directly (e.g., a systemd generator) or need to hijack file
access without changing user code.
§Platform Support
| Platform | Required Features | File to File | Stdout/Stderr to File | Any FD to Any FD |
|---|---|---|---|---|
| Unix-like | libc_on_unix | Yes | Yes | Yes |
| Windows | windows-sys | No | Yes | No |
| Windows | libc_on_windows | Yes | Yes | Yes |
On Unix-like systems such as Linux and macOS, the default is libc_on_unix.
On Windows, the default is windows-sys.
`libc_on_windows` feature is experimental! It should not be relied on.
§Usage
For a more detailed example, see the selftest executable.
§File to File
ⓘ
use io_redirect::Redirectable;
use std::fs::File;
let mut file_src = File::create("src.txt").unwrap();
let mut file_dst = File::create("dst.txt").unwrap();
file_src.redirect(&file_dst).unwrap();§Redirect Standard Streams to a File
ⓘ
let some_path = PathBuf::new("/dev/kmsg".into());
redirect_std_to_path(some_path.as_path(), true).unwrap();
// or just one stream
stdout().redirect(some_path.as_path()).unwrap();§Notes and Caveats
- Resource Management: Avoid using
Redirectable<Path>::redirect(...)multiple times on the same entity as each call will leak a file descriptor.Redirectable<File>does not suffer from the same. - OS-Specific Behavior: Not all features may function identically across platforms; ensure feature flags match the intended target for compilation.
Traits§
- Descriptable
- Redirectable
- A trait to represent entities that can have their I/O redirected to a specified target.