Crate as_what

Source
Expand description

§as-what

as-what provides a set of custom as traits to improve type conversions in Rust, enhancing clarity and readability in expressions.

§Background

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.

§Purpose

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.

§Features

  • Provides various as traits for type conversion.
  • Improves expression readability by addressing operator precedence issues.
  • Supports chaining of operations for more flexible and concise code.

§Installation

cargo add as-what

§Example Usage

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

§Contributing

If you have any suggestions or find issues, feel free to submit an Issue or Pull Request!

§License

This project is licensed under the MIT License.

Traits§

AsChar
A trait for converting to char.
AsF32
A trait for converting to f32.
AsF64
A trait for converting to f64.
AsI8
A trait for converting to i8.
AsI16
A trait for converting to i16.
AsI32
A trait for converting to i32.
AsI64
A trait for converting to i64.
AsI128
A trait for converting to i128.
AsIsize
A trait for converting to isize.
AsU8
A trait for converting to u8.
AsU16
A trait for converting to u16.
AsU32
A trait for converting to u32.
AsU64
A trait for converting to u64.
AsU128
A trait for converting to u128.
AsUsize
A trait for converting to usize.