Skip to main content

newtype_array_deref

Macro newtype_array_deref 

Source
macro_rules! newtype_array_deref {
    ($name:ident, i8, $n:expr) => { ... };
    ($name:ident, u8, $n:expr) => { ... };
    ($name:ident, i16, $n:expr) => { ... };
    ($name:ident, u16, $n:expr) => { ... };
    ($name:ident, i32, $n:expr) => { ... };
    ($name:ident, u32, $n:expr) => { ... };
    ($name:ident, i64, $n:expr) => { ... };
    ($name:ident, u64, $n:expr) => { ... };
    ($name:ident, i128, $n:expr) => { ... };
    ($name:ident, u128, $n:expr) => { ... };
    ($name:ident, isize, $n:expr) => { ... };
    ($name:ident, usize, $n:expr) => { ... };
    ($name:ident, i8, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, u8, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, i16, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, u16, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, i32, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, u32, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, i64, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, u64, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, i128, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, u128, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, isize, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, usize, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, f32, $n:expr) => { ... };
    ($name:ident, f64, $n:expr) => { ... };
    ($name:ident, f32, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, f64, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, String, $n:expr) => { ... };
    ($name:ident, String, $n:expr; $($derive:path),+) => { ... };
    ($name:ident, $t:ty, $n:expr) => { ... };
    ($name:ident, $t:ty, $n:expr; $($derive:path),+) => { ... };
}
Expand description

The newtype_array_deref! macro behaves exactly like newtype_array! but additionally implements Deref<Target = [T]> and DerefMut.

This exposes every slice method (first, last, contains, fill, chunks, windows, split, …) on the newtype directly, as well as triggering implicit &Name -> &[T] coercion. That convenience comes at the cost of partially erasing the wrapper at the call site, which can defeat the type-safety motivation of the newtype pattern. Prefer newtype_array! and access the slice through .as_ref() / .iter() / indexing unless you specifically want the full slice surface.

Example:

use newtypes::*;

newtype_array_deref!(Buf, u8, 4);

let b = Buf([1, 2, 3, 4]);
assert_eq!(Some(&1u8), b.first());
assert_eq!(Some(&4u8), b.last());
assert!(b.contains(&3));