#options #array #default-init

no-std default-option-arr

Macros for simple default initialization of arrays of option types

3 unstable releases

0.2.0 Jul 6, 2024
0.1.1 Jul 8, 2023
0.1.0 May 27, 2023

#75 in No standard library

Download history 31/week @ 2024-04-01 112/week @ 2024-07-01 25/week @ 2024-07-08

137 downloads per month
Used in arraysetcell

EUPL-1.2

12KB
105 lines

Default Array-of-Option<T> Macros

Macros to make your life easier when dealing with default-initialized arrays of Option<T> or Cell<Option<T>> for non-Copy types of T to [None, ..].

You may need it if ...

  • You need an array of [Option<T>; N] initialized to [None; N], or
  • You need an array of [Cell<Option<T>>; N] initialized to [Cell::new(None); N], or
  • You need an array of [RefCell<Option<T>>; N] initialized to [RefCell::new(None); N].

You will not need it if ...

  • Your types already implement Copy or Clone and you don't need cells.
  • You require #![forbid(unsafe_code)].

Examples

use core::cell::Cell;
use arraysetcell::ArraySetCell;

// This type does not implement Copy.
struct Complicated;

fn it_works() {
    // This doesn't compile:
    let arr: [Option<Complicated>; 10] = [None; 10];

    // This does:
    let arr = none_arr![Complicated; 10];

    // [None, None, None, ...]
    assert_eq!(arr.len(), 10);
    for item in arr.into_iter() {
        assert!(item.is_none());
    }

    // The created type is an array.
    let arr: [Option<Complicated>; 10] = arr;
    assert_eq!(arr.len(), 10);
}

Likewise, arrays of Cell<Option<T>> can be created.

fn cell_works() {
    let arr: [Cell<Option<Complicated>>; 10] = none_cell_arr![Complicated; 10];
    let arr: [RefCell<Option<Complicated>>; 10] = none_refcell_arr![Complicated; 10];
}

I cannot have unsafe code

If you cannot have unsafe code in your project, something like the following can be used:

fn not_fun() {
    let arr: [Option<Complicated>; 10] = (0..10)
        .into_iter()
        .map(|_| None)
        .collect::<Vec<_>>()
        .try_into()
        .map_err(|_| "try_into failed") // Debug required otherwise
        .expect("initialization failed");
}

No runtime deps

Features