#![no_std]
#![deny(clippy::missing_safety_doc)]
#![deny(missing_docs)]
#![deny(unsafe_op_in_unsafe_fn)]
#[cfg(feature = "alloc")]
extern crate alloc;
pub mod array;
pub mod int;
pub mod mem;
pub mod tag;
pub use array::ArrayPrefix;
pub use int::SliceIntExt;
#[allow(non_snake_case)]
pub fn Intex<T>(idx: T) -> int::Intex<T> {
int::Intex(idx)
}
#[cfg(doc)]
macro_rules! doctest_readme {
{ $content:expr } => {
#[doc = $content] pub mod readme {}
}
}
#[cfg(doc)]
doctest_readme!(include_str!("../Readme.md"));
#[cfg(test)]
mod test {
use super::{Intex, SliceIntExt};
#[test]
#[should_panic = "100"]
fn panics_with_length_u32() {
[0u8; 0][Intex(100u32)];
}
#[test]
#[should_panic = "100"]
fn panics_with_length_u8() {
[0u8; 0][Intex(100u8)];
}
#[test]
#[should_panic = "-1"]
fn panics_with_length_i8() {
[0u8; 0][Intex(-1i8)];
}
#[test]
#[should_panic = "100000000000000000000000000000000000000"]
fn panics_with_length_u128() {
[0u8; 0][Intex(100_000_000_000_000_000_000_000_000_000_000_000_000u128)];
}
#[test]
fn index_with_all() {
let slice = [0u8; 10];
macro_rules! assert_slice_success {
(@$slice:path, $exp:expr) => {
assert!($slice.get_int($exp).is_some());
};
($slice:path: $($exp:expr),*) => {
$(assert_slice_success!(@$slice, $exp);)*
}
}
macro_rules! assert_slice_fail {
(@$slice:path, $exp:expr) => {
assert_eq!($slice.get_int($exp), None);
};
($slice:path: $($exp:expr),*) => {
$(assert_slice_fail!(@$slice, $exp);)*
}
}
assert_slice_success!(slice: 0u8, 0i8, 0u16, 0i16, 0u32, 0i32, 0u64, 0i64);
assert_slice_success!(slice: ..10u8, ..10i8, ..10u16, ..10i16, ..10u32, ..10i32, ..10u64, ..10i64);
assert_slice_success!(slice: 0..10u8, 0..10i8, 0..10u16, 0..10i16, 0..10u32, 0..10i32, 0..10u64, 0..10i64);
assert_slice_success!(slice: 10u8.., 10i8.., 10u16.., 10i16.., 10u32.., 10i32.., 10u64.., 10i64..);
assert_slice_fail!(slice: -1i8, -1i16, -1i32, -1i64);
}
#[test]
fn unchecked() {
let mut slice = [0u8, 1, 2, 3];
macro_rules! assert_slice_eq {
(@$slice:path, $idx:expr, $exp:expr) => {
assert_eq!($slice[Intex($idx)], $exp);
assert_eq!(&mut $slice[Intex($idx)], $exp);
unsafe {
assert_eq!($slice.get_int_unchecked($idx), $exp);
assert_eq!($slice.get_int_unchecked_mut($idx), $exp);
}
};
($slice:path[idx], $result:expr, for idx in [$($idx:expr),*]) => {
$(assert_slice_eq!(@$slice, $idx, $result);)*
}
}
assert_slice_eq!(slice[idx], [1, 2],
for idx in [1u8..3, 1i8..3, 1u16..3, 1i16..3, 1u32..3, 1i32..3, 1u64..3, 1i64..3]);
}
#[test]
fn str_indices() {
let text = "What if ascii still has it?";
assert_eq!(text.get_int(8u8..13), Some("ascii"));
}
}