# Isolator
<div align="center">
[](https://crates.io/crates/isolator)
[](https://docs.rs/isolator)
[](LICENSE)
[](https://www.rust-lang.org)
[](https://github.com/username/isolator/actions)
A Rust library for running functions in isolation, with panic catching and error handling.
[Installation](#installation) •
[Usage](#usage) •
[Examples](#examples) •
[API Reference](#api-reference)
</div>
## Features
- ✨ Run synchronous and async functions in isolation
- 🛡️ Catch panics and convert them to proper errors
- 🔄 Support for async/await
- 🧪 Comprehensive test coverage
- 📦 Zero dependencies (except for async support)
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
isolator = "0.1.0"
```
## Usage
### Basic Example
```rust
use isolator::{isolate, IsolationResult};
fn main() -> IsolationResult<()> {
// Run a function in isolation
let result = isolate(|| {
// This code is isolated
println!("Hello from isolation!");
42
})?;
println!("Result: {}", result); // prints: Result: 42
Ok(())
}
```
### Async Example
```rust
use isolator::{isolate_async, IsolationResult};
async fn example() -> IsolationResult<()> {
let result = isolate_async(|| async {
// This async code is isolated
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
42
}).await?;
println!("Async result: {}", result);
Ok(())
}
```
### Error Handling
The library provides the `IsolationError` enum for handling both panics and regular errors:
```rust
#[derive(Error, Debug)]
pub enum IsolationError {
#[error("Function panicked: {0}")]
Panic(String),
#[error("Function failed: {0}")]
Error(Box<dyn std::error::Error + Send + Sync>),
}
```
Example with error handling:
```rust
use isolator::{isolate, IsolationError};
fn main() {
match isolate(|| {
if true {
panic!("Something went wrong!");
}
42
}) {
Ok(value) => println!("Success: {}", value),
Err(IsolationError::Panic(msg)) => println!("Panic caught: {}", msg),
Err(IsolationError::Error(e)) => println!("Error caught: {}", e),
}
}
```
## Advanced Usage
### Custom Error Types
You can use your own error types as long as they implement `std::error::Error`:
```rust
use thiserror::Error;
use isolator::isolate;
#[derive(Error, Debug)]
enum MyError {
#[error("Custom error: {0}")]
Custom(String),
}
fn fallible_function() -> Result<(), MyError> {
Err(MyError::Custom("oops".to_string()))
}
fn main() {
let result = isolate(|| fallible_function());
// Handle the result...
}
```
### Integration with Async Runtimes
The library works seamlessly with async runtimes like Tokio:
```rust
use isolator::isolate_async;
use tokio;
#[tokio::main]
async fn main() {
let result = isolate_async(|| async {
// Your async code here
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
"Done!"
}).await;
println!("Result: {:?}", result);
}
```
## API Reference
For detailed API documentation, visit [docs.rs/isolator](https://docs.rs/isolator).
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
<div align="center">
## License
This project is licensed under the GPL-3.0 License - see the [LICENSE](LICENSE) file for details.
---
Made by Voltaged.
</div>