Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/acovaci/expand-array-rust-macro
Arrr! macro for easy fixed-size array creation from slices and strings. Fill gaps with any value or zero.
https://github.com/acovaci/expand-array-rust-macro
array c-ffi crate crates-io developer-tools ffi macro proc-macro proc-macro2 rust rust-ffi static
Last synced: about 1 month ago
JSON representation
Arrr! macro for easy fixed-size array creation from slices and strings. Fill gaps with any value or zero.
- Host: GitHub
- URL: https://github.com/acovaci/expand-array-rust-macro
- Owner: acovaci
- License: mpl-2.0
- Created: 2024-05-17T09:37:22.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-05-18T02:30:16.000Z (7 months ago)
- Last Synced: 2024-10-28T22:27:07.132Z (about 2 months ago)
- Topics: array, c-ffi, crate, crates-io, developer-tools, ffi, macro, proc-macro, proc-macro2, rust, rust-ffi, static
- Language: Rust
- Homepage: https://crates.io/crates/expand_array
- Size: 43.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Expand Array Rust Macro
![](https://img.shields.io/github/actions/workflow/status/acovaci/expand-array-rust-macro/rust.yml)
[![](https://img.shields.io/crates/v/expand-array)
](https://crates.io/crates/expand-array)
[![](https://img.shields.io/crates/d/expand-array)
](https://crates.io/crates/expand-array)
[![](https://img.shields.io/crates/l/expand-array)
](https://crates.io/crates/expand-array)
[![](https://docs.rs/expand_array/badge.svg)
](https://docs.rs/expand-array)This crate provides a macro called `arr!` to convert a static array to a
fixed-size array. Limited type conversion is supported. This is useful when you
need to make available global constants in a dynamic library, for example.As a bonus, the crate also provides a `bitify!` macro to convert a string
literal to a fixed-size array of bytes.## Usage
The `arr!` macro can take any two primitive types. On top of that, it can take
string literals (`&str`, byte strings and C strings). Using the `bitify!` macro,
it can convert it to a fixed-size array of any primitive type.```rust
use expand_array::arrr;let var: [; target_len] = arrr!( as [; target_len]);
let var_with_default: [; target_len] = arrr!( as [; target_len] with );
```## Example
```rust
use expand_array::arrr;let arr: [u8; 10] = arrr!([1, 2, 3, 4, 5] as [u8; 10]);
assert_eq!(arr, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]);let arr: [u8; 10] = arrr!([1, 2, 3, 4, 5] as [u8; 10] with 2);
assert_eq!(arr, [1, 2, 3, 4, 5, 2, 2, 2, 2, 2]);let arr: [u8; 10] = arrr!(b"Hello" as [u8; 10]);
assert_eq!(arr, [72, 101, 108, 108, 111, 0, 0, 0, 0, 0]);let arr: [u8; 10] = arrr!("Hello" as [u8; 10]);
assert_eq!(arr, [72, 101, 108, 108, 111, 0, 0, 0, 0, 0]);let arr: [char; 10] = arrr!(['H', 'e', 'l', 'l', 'o'] as [char; 10]);
assert_eq!(arr, ['H', 'e', 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0']);let arr: [&str; 10] = arrr!(["Hello", "world"] as [&str; 10] with '!');
assert_eq!(arr, ["Hello", "world", "!", "!", "!", "!", "!", "!", "!", "!"]);use ::std::ffi::c_char;
let arr: [c_char; 10] = arrr!([72i8, 101, 108, 108, 111] as [c_char; 10]);
assert_eq!(arr, [72i8, 101, 108, 108, 111, 0, 0, 0, 0, 0]);let arr: [c_char; 10] = arrr!(c"Hello" as [c_char; 10]);
assert_eq!(arr, [72i8, 101, 108, 108, 111, 0, 0, 0, 0, 0]);let arr: [c_char; 10] = arrr!([72u8, 101, 108, 108, 111] as [c_char; 10]);
assert_eq!(arr, [72i8, 101, 108, 108, 111, 0, 0, 0, 0, 0]);let arr: [c_char; 10] = arrr!([b'H', b'e', b'l', b'l', b'o'] as [c_char; 10]);
assert_eq!(arr, [72i8, 101, 108, 108, 111, 0, 0, 0, 0, 0]);let arr: [c_char; 10] = arrr!(b"Hello" as [c_char; 10]);
assert_eq!(arr, [72i8, 101, 108, 108, 111, 0, 0, 0, 0, 0]);let arr: [c_char; 10] = arrr!("Hello" as [c_char; 10] with "!");
assert_eq!(arr, [72i8, 101, 108, 108, 111, 33, 33, 33, 33, 33]);
```