Crates.io | process-fun-macro |
lib.rs | process-fun-macro |
version | 0.1.1 |
source | src |
created_at | 2024-12-12 11:37:52.082079 |
updated_at | 2024-12-12 16:18:07.696655 |
description | Procedural macros for process-fun, a Rust library for process management and IPC |
homepage | |
repository | https://github.com/eytans/process-fun-rs |
max_upload_size | |
id | 1481189 |
size | 12,945 |
A Rust library for easily running functions in separate processes with minimal boilerplate on Nix systems.
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:
#[process]
attribute macro for marking functions to create an additional version that runs in separate processesAdd this to your Cargo.toml
:
[dependencies]
process-fun = "0.1.0"
Basic example:
use process_fun::process;
use serde::{Serialize, Deserialize};
use std::time::Duration;
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}
#[process]
pub fn add_points(p1: Point, p2: Point) -> Point {
Point {
x: p1.x + p2.x,
y: p1.y + p2.y,
}
}
fn main() {
let p1 = Point { x: 1, y: 2 };
let p2 = Point { x: 3, y: 4 };
// Execute in a separate process with a timeout
let mut process = add_points_process(p1, p2).unwrap();
let result = process.timeout(Duration::from_secs(5)).unwrap();
assert_eq!(result.x, 4);
assert_eq!(result.y, 6);
}
Example demonstrating timeout behavior:
use process_fun::process;
use serde::{Serialize, Deserialize};
use std::time::Duration;
use std::thread;
#[process]
fn long_running_task() -> i32 {
thread::sleep(Duration::from_secs(10));
42
}
fn main() {
let mut process = long_running_task_process().unwrap();
// Process will be killed if it doesn't complete within 1 second
match process.timeout(Duration::from_secs(1)) {
Ok(result) => println!("Task completed with result: {}", result),
Err(e) => println!("Task timed out: {}", e)
}
}
Example demonstrating mutable data behavior:
use process_fun::process;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Counter {
value: i32,
}
#[process]
pub fn increment_counter(mut counter: Counter) -> Counter {
counter.value += 1;
counter
}
fn main() {
let counter = Counter { value: 0 };
// The counter is passed by value to the process
let mut process = increment_counter_process(counter).unwrap();
let result = process.timeout(Duration::from_secs(1)).unwrap();
// Original counter is unchanged
println!("Original counter value: {}", counter.value); // Prints: 0
println!("Result counter value: {}", result.value); // Prints: 1
}
#[process]
attribute macro generates a wrapper function with _process
suffixProcessWrapper
objectprocess-fun
: Main crate providing the public APIprocess-fun-core
: Core functionality and typesprocess-fun-macro
: Implementation of the #[process]
attribute macro#[process]
must:
Serialize
and Deserialize
self
parametersThis project is licensed under the Apache 2 License - see the LICENSE file for details.