counted-array-0.1.2/.gitignore01006440000765000002400000000161130404636560014503 0ustar0000000000000000# Compiled files *.o *.so *.rlib *.dll # Executables *.exe # Generated by Cargo target Cargo.lock # vim *.sw* counted-array-0.1.2/Cargo.toml.orig01006440000765000002400000001104131357335250015400 0ustar0000000000000000[package] name = "counted-array" version = "0.1.2" authors = ["Alex Burka "] description = "Macro for declaring fixed-size arrays without counting elements by hand. Supports lazy_static." categories = ["rust-patterns"] homepage = "https://github.com/durka/counted-array" repository = "https://github.com/durka/counted-array" readme = "README.md" license = "MIT" keywords = ["array", "length", "macro"] [dependencies] lazy_static = { version = "0.2", optional = true } [dev-dependencies] lazy_static = "0.2" [features] nightly = ["lazy_static/nightly"] counted-array-0.1.2/Cargo.toml0000644000000021150010625 0ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "counted-array" version = "0.1.2" authors = ["Alex Burka "] description = "Macro for declaring fixed-size arrays without counting elements by hand. Supports lazy_static." homepage = "https://github.com/durka/counted-array" readme = "README.md" keywords = ["array", "length", "macro"] categories = ["rust-patterns"] license = "MIT" repository = "https://github.com/durka/counted-array" [dependencies.lazy_static] version = "0.2" optional = true [dev-dependencies.lazy_static] version = "0.2" [features] nightly = ["lazy_static/nightly"] counted-array-0.1.2/LICENSE.md01006440000765000002400000002067127035343730014126 0ustar0000000000000000The MIT License (MIT) Copyright (c) 2016 Alex Burka Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. counted-array-0.1.2/README.md01006440000765000002400000000636127035371630014001 0ustar0000000000000000What it is ========== This crate exports a macro which can be used to declare a fixed-size array without counting the elements by hand. This is achieved by using macro recursion to make rustc count the elements at compile time. The macro can declare local variables, consts, statics, and lazy statics (if you import that crate, or enable this crate's "nightly" feature). For examples, see the tests directory. counted-array-0.1.2/src/lib.rs01006440000765000002400000003735131357332710014426 0ustar0000000000000000#![cfg_attr(feature = "nightly", feature(allow_internal_unstable, macro_reexport))] #[cfg(feature = "nightly")] #[macro_reexport(lazy_static, __lazy_static_create)] #[macro_use] extern crate lazy_static; #[cfg(feature = "nightly")] pub use lazy_static::lazy; /// Declare a fixed-size array with an autogenerated length. /// /// ``` /// # #[macro_use] extern crate counted_array; /// # fn main() { /// counted_array!(let arr: [i32; _] = [1, 2, 3]); /// assert_eq!(arr.len(), 3); /// # } /// ``` #[macro_export] macro_rules! counted_array { // INTERNAL // last element (proceed to output) (@parse $size:expr, ($val:expr) -> [$($accs:expr),*] $thru:tt) => { counted_array!(@output $size + 1usize, [$($accs,)* $val] $thru); }; // more elements (keep parsing) (@parse $size:expr, ($val:expr, $($vals:expr),*) -> [$($accs:expr),*] $thru:tt) => { counted_array!(@parse $size + 1usize, ($($vals),*) -> [$($accs,)* $val] $thru); }; // output a local variable (@output $size:expr, $acc:tt (() let $n:ident $t:ty)) => { let $n: [$t; $size] = $acc; }; // output a lazy static (@output $size:expr, $acc:tt (($($p:tt)*) lazy_static $n:ident $t:ty)) => { lazy_static!{ $($p)* static ref $n: [$t; $size] = $acc; } }; // output a static or const item (@output $size:expr, $acc:tt (($($p:tt)*) $s:ident $n:ident $t:ty)) => { $($p)* $s $n: [$t; $size] = $acc; }; // EXTERNAL // entry point (pub $storage:ident $n:ident: [$t:ty; _] = [$($vals:expr),* $(,)*]) => { counted_array!(@parse 0usize, ($($vals),*) -> [] ((pub) $storage $n $t)); }; (pub $restr:tt $storage:ident $n:ident: [$t:ty; _] = [$($vals:expr),* $(,)*]) => { counted_array!(@parse 0usize, ($($vals),*) -> [] ((pub $restr) $storage $n $t)); }; ($storage:ident $n:ident: [$t:ty; _] = [$($vals:expr),* $(,)*]) => { counted_array!(@parse 0usize, ($($vals),*) -> [] (() $storage $n $t)); }; } counted-array-0.1.2/tests/nightly.rs01006440000765000002400000001320130404635130015667 0ustar0000000000000000#![cfg(feature = "nightly")] #[macro_use] extern crate counted_array; use std::time::{SystemTime, UNIX_EPOCH}; counted_array!(pub lazy_static PLAZY: [u64; _] = [3, 2, SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()]); #[test] fn lazy_static_reexport() { // notice lazy_static was not imported separately in this file counted_array!(lazy_static LAZY: [u64; _] = [3, 2, SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()]); assert_eq!(LAZY[0], 3); assert_eq!(LAZY[1], 2); assert!(LAZY[2] > 1400000000); assert_eq!(PLAZY[0], 3); assert_eq!(PLAZY[1], 2); assert!(PLAZY[2] > 1400000000); } #[test] fn test_lazy_static_reexport() { lazy_static_reexport(); } counted-array-0.1.2/tests/stable.rs01006440000765000002400000002651131357333250015501 0ustar0000000000000000#[cfg(test)] #[macro_use] extern crate lazy_static; #[macro_use] extern crate counted_array; use std::time::{SystemTime, UNIX_EPOCH}; #[test] fn private_arrays() { counted_array!(const LOCAL_CONST_ARR: [i32; _] = [1, 2, 3]); counted_array!(let local_arr: [i32; _] = [4, 5, 6]); counted_array!(static LOCAL_STATIC_ARR: [i32; _] = [7, 8, 9, 10]); assert_eq!(LOCAL_CONST_ARR, [1, 2, 3]); assert_eq!(local_arr, [4, 5, 6]); assert_eq!(LOCAL_STATIC_ARR, [7, 8, 9, 10]); } counted_array!(pub const CONST_ARR: [i32; _] = [1, 2, 3]); counted_array!(pub static STATIC_ARR: [i32; _] = [7, 8, 9, 10]); counted_array!(pub(crate) static CRATE_ARR: [i32; _] = [11, 12, 13, 14, 15]); #[test] fn public_arrays() { assert_eq!(CONST_ARR, [1, 2, 3]); assert_eq!(STATIC_ARR, [7, 8, 9, 10]); } #[cfg(test)] counted_array!(pub lazy_static PLAZY: [u64; _] = [3, 2, SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()]); #[cfg(test)] fn lazy_static() { counted_array!(lazy_static LAZY: [u64; _] = [3, 2, SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()]); assert_eq!(LAZY[0], 3); assert_eq!(LAZY[1], 2); assert!(LAZY[2] > 1400000000); assert_eq!(PLAZY[0], 3); assert_eq!(PLAZY[1], 2); assert!(PLAZY[2] > 1400000000); } #[test] fn test_private_arrays() { private_arrays(); } #[test] fn test_public_arrays() { public_arrays(); } #[test] fn test_lazy_static() { lazy_static(); }