| Crates.io | as-what |
| lib.rs | as-what |
| version | 0.1.1 |
| created_at | 2024-12-11 12:21:27.077629+00 |
| updated_at | 2025-05-03 12:53:40.504422+00 |
| description | Provide a bunch of `as` traits. |
| homepage | |
| repository | https://github.com/jmjoy/as-what |
| max_upload_size | |
| id | 1480045 |
| size | 9,225 |
as-what provides a set of custom as traits to improve type conversions in Rust, enhancing clarity and readability in expressions.
The as keyword was part of Rust’s early syntax, before the introduction of postfix operators like .await. Consider the following example using as:
fn calc_index(x: u64, y: u64, width: std::num::NonZeroI64) -> usize {
y as usize * width.get() as usize + x as usize
}
In this case, the precedence of the as keyword is mixed with other operators, making the expression harder to read. Additionally, as does not support chained operations, limiting its flexibility in complex expressions.
The purpose of this library is to provide custom as traits that solve these issues, improving the readability of code and allowing more intuitive type conversion expressions. The library helps avoid problems related to operator precedence and enables chaining of operations.
as traits for type conversion.cargo add as-what
Here is a simple example of how to use the library:
use as_what::AsUsize;
fn calc_index(x: u64, y: u64, width: std::num::NonZeroI64) -> usize {
y.as_usize() * width.get().as_usize() + x.as_usize()
}
If you have any suggestions or find issues, feel free to submit an Issue or Pull Request!
This project is licensed under the MIT License.