Skip to content

Commit 39be699

Browse files
committed
async_from
1 parent d4d6cc1 commit 39be699

File tree

14 files changed

+645
-108
lines changed

14 files changed

+645
-108
lines changed

Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,16 @@ version = "~0.4.0"
411411
path = "module/alias/wtest_basic"
412412

413413

414+
## async
415+
416+
[workspace.dependencies.async_from]
417+
version = "~0.1.0"
418+
path = "module/core/async_from"
419+
420+
# [workspace.dependencies.async_tools]
421+
# version = "~0.1.0"
422+
# path = "module/core/async_tools"
423+
414424
## graphs tools
415425

416426
[workspace.dependencies.graphs_tools]
@@ -517,3 +527,12 @@ default-features = true
517527
version = "~0.3.0"
518528
path = "module/test/c"
519529
default-features = true
530+
531+
## External
532+
533+
[workspace.dependencies.async-trait]
534+
version = "0.1.83"
535+
536+
[workspace.dependencies.tokio]
537+
version = "1.41.0"
538+
features = []

module/core/async_from/Cargo.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "async_from"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = [
6+
"Kostiantyn Wandalen <[email protected]>",
7+
]
8+
license = "MIT"
9+
readme = "Readme.md"
10+
documentation = "https://docs.rs/async_from"
11+
repository = "https://github.com/Wandalen/wTools/tree/master/module/core/async_from"
12+
homepage = "https://github.com/Wandalen/wTools/tree/master/module/core/async_from"
13+
description = """
14+
Async version of From, Into, TryFrom, TryInto.
15+
"""
16+
categories = [ "algorithms", "development-tools" ]
17+
keywords = [ "fundamental", "general-purpose" ]
18+
19+
[lints]
20+
workspace = true
21+
22+
[package.metadata.docs.rs]
23+
features = [ "full" ]
24+
all-features = false
25+
26+
[features]
27+
default = [ "enabled", "async_from", "async_try_from" ]
28+
full = [ "default" ]
29+
enabled = []
30+
async_from = []
31+
async_try_from = []
32+
33+
[dependencies]
34+
async-trait = { workspace = true }
35+
36+
[dev-dependencies]
37+
# test_tools = { workspace = true }
38+
tokio = { workspace = true, default-features = false, features = [ "rt-multi-thread", "time", "macros" ] }

module/core/async_from/License

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright Kostiantyn Mysnyk and Out of the Box Systems (c) 2021-2024
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

module/core/async_from/Readme.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<!-- {{# generate.module_header{} #}} -->
2+
3+
# Module :: async_from
4+
[![experimental](https://raster.shields.io/static/v1?label=stability&message=experimental&color=orange&logoColor=eee)](https://github.com/emersion/stability-badges#experimental) [![rust-status](https://github.com/Wandalen/wTools/actions/workflows/Moduleasync_fromPush.yml/badge.svg)](https://github.com/Wandalen/wTools/actions/workflows/Moduleasync_fromPush.yml) [![docs.rs](https://img.shields.io/docsrs/async_from?color=e3e8f0&logo=docs.rs)](https://docs.rs/async_from) [![discord](https://img.shields.io/discord/872391416519737405?color=eee&logo=discord&logoColor=eee&label=ask)](https://discord.gg/m3YfbXpUUY)
5+
6+
Async version of From, Into, TryFrom, TryInto.
7+
8+
The `async_from` crate provides asynchronous versions of the well-known `From`, `Into`, `TryFrom`, and `TryInto` traits. These traits are essential for handling conversions in Rust, and their asynchronous counterparts, allowing for conversions that involve asynchronous operations.
9+
10+
## Why Asynchronous Conversion Traits?
11+
12+
In Rust, the `From`, `Into`, `TryFrom`, and `TryInto` traits provide a standardized way to handle type conversions. The `async_from` module extends this functionality to asynchronous contexts with `AsyncFrom`, `AsyncInto`, `AsyncTryFrom`, and `AsyncTryInto` traits, offering several key benefits:
13+
14+
- **Simplicity**: Allow straightforward conversions without boilerplate, even in asynchronous contexts.
15+
- **Consistency**: Provide a uniform interface for conversions across different types, aiding in writing predictable and maintainable code.
16+
- **Error Handling**: Enable safe and explicit handling of conversion failures, essential for robust error management in commercial applications.
17+
- **Asynchronous Contexts**: Facilitate conversions involving asynchronous operations, such as network requests or database queries, which are common in modern applications.
18+
19+
The `async_from` provides developers with the tools needed to handle complex conversions in an async context efficiently, which is particularly important for commercial applications requiring reliable and efficient handling of asynchronous operations.
20+
21+
### `AsyncFrom` and `AsyncInto`
22+
23+
Trait for asynchronous conversions from a type T.
24+
25+
These traits are designed for infallible asynchronous conversions. They allow you to convert types asynchronously, returning the result directly.
26+
27+
```rust
28+
use async_from::{ async_trait, AsyncFrom, AsyncInto };
29+
30+
struct MyNumber( u32 );
31+
32+
#[ async_trait ]
33+
impl AsyncFrom< String > for MyNumber
34+
{
35+
async fn async_from( value : String ) -> Self
36+
{
37+
let num = value.parse::< u32 >().unwrap_or( 0 );
38+
MyNumber( num )
39+
}
40+
}
41+
42+
#[ tokio::main ]
43+
async fn main()
44+
{
45+
let num = MyNumber::async_from( "42".to_string() ).await;
46+
println!( "Converted: {}", num.0 );
47+
let num : MyNumber = "42".to_string().async_into().await;
48+
println!( "Converted: {}", num.0 );
49+
}
50+
```
51+
52+
### `AsyncTryFrom` and `AsyncTryInto`
53+
54+
Trait for asynchronous fallible conversions from a type T.
55+
56+
These traits are for fallible asynchronous conversions, where the conversion might fail. They return a `Result` wrapped in a `Future`, allowing you to handle errors gracefully.
57+
58+
```rust
59+
use async_from::{ async_trait, AsyncTryFrom, AsyncTryInto };
60+
use std::num::ParseIntError;
61+
62+
struct MyNumber( u32 );
63+
64+
#[ async_trait ]
65+
impl AsyncTryFrom< String > for MyNumber
66+
{
67+
type Error = ParseIntError;
68+
69+
async fn async_try_from( value : String ) -> Result< Self, Self::Error >
70+
{
71+
let num = value.parse::< u32 >()?;
72+
Ok( MyNumber( num ) )
73+
}
74+
}
75+
76+
#[ tokio::main ]
77+
async fn main()
78+
{
79+
match MyNumber::async_try_from( "42".to_string() ).await
80+
{
81+
Ok( my_num ) => println!( "Converted successfully: {}", my_num.0 ),
82+
Err( e ) => println!( "Conversion failed: {:?}", e ),
83+
}
84+
let result : Result< MyNumber, _ > = "42".to_string().async_try_into().await;
85+
match result
86+
{
87+
Ok( my_num ) => println!( "Converted successfully using AsyncTryInto: {}", my_num.0 ),
88+
Err( e ) => println!( "Conversion failed using AsyncTryInto: {:?}", e ),
89+
}
90+
}
91+
```

0 commit comments

Comments
 (0)