0.2.2 |
|
---|---|
0.2.1 |
|
0.1.2 |
|
#12 in #infer
8KB
56 lines
This project is no longer maintained.
lib.rs
:
"Infer" the lengths of arrays in array static
/const
items.
Crate feature
-
no-core
: Make this crate#![no_core]
.#![no_core]
example#![feature(no_core, lang_items, start)] #![no_core]
#[cfg_attr(not(windows), link(name = "c"))] #[cfg_attr(windows, link(name = "msvcrt"))] extern "C" {}
use infer_len::infer_len;
#[lang = "sized"] trait Sized {}
#[lang = "eh_personality"] fn eh_personality() -> ! { loop {} }
#[lang = "receiver"] trait Receiver {}
impl<T: ?Sized> Receiver for &T {}
#[lang = "copy"] trait Copy {}
impl Copy for i32 {}
#[lang = "array"] impl [i32; 5] { const fn len(self) -> usize { 5 } }
infer_len! { const FOO: [i32; _] = [1, 2, 3, 4, 5]; pub(crate) const BAR: &[i32; _] = &FOO; pub(self) const BAZ: [i32; _] = [6, 7, 8, 9, 10]; }
#[start] fn main(_: isize, _: *const *const u8) -> isize { let _: [i32; 5] = FOO; let _: &[i32; 5] = BAR; let _: [i32; 5] = BAZ; 0 }
</details>
Example
mod foo {
# use infer_len::infer_len;
use std::convert;
infer_len! {
pub(self) const FOO: &[i128; _] = &[-1, -2, -3, -4];
const BAR: [i128; _] = *FOO;
pub static BAZ: [i128; _] = BAR;
pub(super) static HELLO_WORLD: &[u8; _] = b"Hello World";
#[allow(non_upper_case_globals)] // attribute works
static qux: [i128; _] = convert::identity(BAR); // arbitrary expression works
#[allow(non_upper_case_globals)]
#[used] // multiple attributes works
static used: [i128; _] = convert::identity(*FOO);
pub(crate) const QUZ: &[i128; _] = FOO;
}
fn _foo() {
let _: &[i128; 4] = FOO;
let _: [i128; 4] = BAR;
let _: [i128; 4] = qux;
}
}
let _: &[u8; 11] = foo::HELLO_WORLD;
let _: [i128; 4] = foo::BAZ;
let _: &[i128; 4] = foo::QUZ;