| Crates.io | async-reciprocals |
| lib.rs | async-reciprocals |
| version | 0.1.0 |
| created_at | 2025-04-01 22:11:21.838389+00 |
| updated_at | 2025-04-01 22:11:21.838389+00 |
| description | A utility library for asynchronous fallible conversion and reciprocals in Rust. |
| homepage | |
| repository | https://github.com/jfaleiro/async-reciprocals |
| max_upload_size | |
| id | 1615675 |
| size | 52,749 |
A utility library for asynchronous fallible conversion and reciprocals in Rust.
async-reciprocals provides tools for handling bidirectional asynchronous conversions between different types in Rust. It leverages Tokio for asynchronous operations, making it suitable for high-performance applications that need type conversions in non-blocking contexts.
Add this to your Cargo.toml:
[dependencies]
async-reciprocals = "0.1.0"
Basic example:
use async_reciprocals::{AsyncTryFrom, AsyncTryInto};
use tokio;
use std::error::Error;
// Define a simple source type
struct UserInput {
data: String
}
// Define a target type
struct ProcessedData {
value: String
}
// Implement async conversion from UserInput to ProcessedData
#[async_trait::async_trait]
impl AsyncTryFrom<UserInput> for ProcessedData {
type Error = Box<dyn Error + Send + Sync>;
async fn async_try_from(input: UserInput) -> Result<Self, Self::Error> {
// Simulate some async processing
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
// Convert the input to processed data
Ok(ProcessedData {
value: input.data.to_uppercase()
})
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a user input
let user_input = UserInput {
data: "hello world".to_string()
};
// Convert it to processed data using AsyncTryInto (available through blanket impl)
let processed: ProcessedData = user_input.async_try_into().await?;
// Verify the conversion worked
println!("Converted '{}' to '{}'", "hello world", processed.value);
assert_eq!(processed.value, "HELLO WORLD");
println!("Async conversion successful!");
Ok(())
}
To contribute to this project:
cargo buildcargo testThis project is licensed under the GNU Lesser General Public License v2.1 (LGPL-2.1). See the LICENSE file for details.
Contributions are welcome. Feel free to submit a Pull Request.