#convert #tokio #async #operation #reciprocal

async-reciprocals

A utility library for asynchronous fallible conversion and reciprocals in Rust

1 unstable release

Uses new Rust 2024

new 0.1.0 Apr 1, 2025

#564 in Asynchronous

Download history 78/week @ 2025-03-28

78 downloads per month

Custom license

18KB
136 lines

async-reciprocals

A utility library for asynchronous fallible conversion and reciprocals in Rust.

Overview

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.

Features

  • Asynchronous type conversion utilities
  • Built on Tokio runtime for efficient async operations
  • Simple API for bidirectional conversions
  • Designed for Rust's async/await ecosystem

Installation

Add this to your Cargo.toml:

[dependencies]
async-reciprocals = "0.1.0"

Usage

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(())
}

Requirements

  • Rust 2024 edition
  • Tokio runtime

Development

To contribute to this project:

  1. Clone the repository
  2. Install dependencies with cargo build
  3. Run tests with cargo test

License

This project is licensed under the GNU Lesser General Public License v2.1 (LGPL-2.1). See the LICENSE file for details.

Contributing

Contributions are welcome. Feel free to submit a Pull Request.

Dependencies

~180–610KB
~14K SLoC