process-fun-rs
A Rust library for easily running functions in separate processes with minimal boilerplate on Nix systems.
Overview
process-fun-rs
provides a simple macro-based approach to execute Rust functions in separate processes. It handles all the complexity of process spawning, argument serialization, and result communication, allowing you to focus on your business logic.
⚠️ Important Notes:
- This library currently only supports Nix-based systems
- When passing mutable data to a process function, modifications made within the process will not be reflected in the original data structure in the parent process.
Features
- Simple
#[process]
attribute macro for marking functions to create an additional version that runs in separate processes - Automatic serialization/deserialization of function return values
- Type-safe process communication
- Error handling with custom error types
- Debug mode for troubleshooting process execution
- Process timeout support with automatic cleanup
Usage
Add this to your Cargo.toml
:
[]
= "0.1.0"
Basic example:
use process;
use ;
use Duration;
Example demonstrating timeout behavior:
use process;
use ;
use Duration;
use thread;
Example demonstrating mutable data behavior:
use process;
use ;
How It Works
- The
#[process]
attribute macro generates a wrapper function with_process
suffix - When called, the wrapper function:
- Forks the process
- Returns a
ProcessWrapper
object
- The ProcessWrapper provides timeout functionality:
- Set a maximum duration for process execution
- Automatically kills the process if it exceeds the timeout
- Cleans up resources properly whether the process completes or times out
- Deserializes the result from the process
Crate Structure
process-fun
: Main crate providing the public APIprocess-fun-core
: Core functionality and typesprocess-fun-macro
: Implementation of the#[process]
attribute macro
Requirements
- Nix-based operating system
- Functions marked with
#[process]
must:- Have arguments and return types that implement
Serialize
andDeserialize
- Not take
self
parameters
- Have arguments and return types that implement
License
This project is licensed under the Apache 2 License - see the LICENSE file for details.