bronto 0.1.0

Utilities for for code automatically translated from C to Rust.
Documentation
// Copyright 2024 BrontoSource Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![no_std]

/// Initializes a constant array. The sequence of arguments passed until the
/// first semicolon initializes the first values in the array. The remaining
/// arguments are initialzed with the value provided to the named `fill`
/// parameter.
///
/// For example,
/// ```
/// use bronto::array;
/// assert_eq!(array![1, 4, 9; fill=0; 6], [1, 4, 9, 0, 0, 0]);
/// assert_eq!(array![fill=0; 6], [0, 0, 0, 0, 0, 0]);
/// assert_eq!(array![1, 4, 9; fill=0; 3], [1, 4, 9]);
/// ```
#[macro_export]
macro_rules! array {
  (fill=$fill:expr; $width:expr)=>{
    [$fill; $width]
  };
  ($($inits:expr),*; fill=$fill:expr; $width:expr)=>{
    const {
      let mut array = [$fill; $width];
      let mut index: isize = -1;
      $({
        index += 1;
        array[index as usize] = $inits;
      })*
      array
    }
  };
}

#[cfg(test)]
mod tests {
  #[test]
  fn empty() {
    const A: [i32; 0] = array![fill=3; 0];
    assert_eq!(A, []);
  }

  #[test]
  fn fill() {
    const A: [i32; 4] = array![fill=3; 4];
    assert_eq!(A, [3, 3, 3, 3]);
  }

  #[test]
  fn init_fill() {
    const A: [i32; 4] = array![2; fill=3; 4];
    assert_eq!(A, [2, 3, 3, 3]);
  }

  #[test]
  fn multi_init_fill() {
    const A: [i32; 10] = array![1, 2, 3; fill=10; 10];
    assert_eq!(A, [1, 2, 3, 10, 10, 10, 10, 10, 10, 10]);
  }

  #[test]
  fn fill_unused() {
    const A: [i32; 3] = array![1, 2, 3; fill=10; 3];
    assert_eq!(A, [1, 2, 3]);
  }

  #[test]
  fn strs() {
    const A: [&'static str; 5] = array!["hello", "world"; fill=""; 5];
    assert_eq!(A, ["hello", "world", "", "", ""]);
  }
}