use process_fun::process;
use serde::{Deserialize, Serialize};
use std::thread;
use std::time::Duration;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Point {
x: i32,
y: i32,
}
#[process]
pub fn calculate_distance(p1: Point, p2: Point) -> f64 {
let dx = (p2.x - p1.x) as f64;
let dy = (p2.y - p1.y) as f64;
(dx * dx + dy * dy).sqrt()
}
#[process]
#[allow(unused_mut)]
pub fn move_point(mut point: Point, dx: i32, dy: i32) -> Point {
point.x += dx;
point.y += dy;
point }
#[process]
pub fn slow_calculation(iterations: u64) -> u64 {
let mut sum: u64 = 0;
for i in 0..iterations {
sum = sum.wrapping_add(i as u64);
if i % 1000 == 0 {
thread::sleep(Duration::from_micros(1));
}
}
sum
}
fn main() {
println!("Running examples with debug prints enabled...\n");
let p1 = Point { x: 0, y: 0 };
let p2 = Point { x: 3, y: 4 };
println!("Calculating distance between {:?} and {:?}...", &p1, &p2);
let mut process = calculate_distance_process(p1.clone(), p2.clone()).unwrap();
match process.timeout(Duration::from_secs(1)) {
Ok(distance) => println!("Distance: {}", distance), Err(e) => panic!("Error: {}", e),
}
println!();
let original = Point { x: 10, y: 20 };
println!("Moving point with mutable data example:");
println!("Original point: {:?}", &original);
let mut process = move_point_process(original.clone(), 5, 5).unwrap();
match process.timeout(Duration::from_secs(1)) {
Ok(moved) => {
println!("Original point (unchanged): {:?}", original);
println!("Moved point (new): {:?}", moved);
}
Err(e) => panic!("Error: {}", e),
}
println!();
println!("Running slow calculation with timeout...");
let mut process = slow_calculation_process(10_000_000).unwrap();
match process.timeout(Duration::from_millis(100)) {
Ok(result) => println!("Calculation completed with result: {}", result),
Err(e) => println!("Calculation timed out as expected: {}", e),
}
}