Skip to content

Instantly share code, notes, and snippets.

@alerdenisov
Created October 28, 2020 17:08
Show Gist options
  • Save alerdenisov/c1e24ca8400693a9827ee31179a83cad to your computer and use it in GitHub Desktop.
Save alerdenisov/c1e24ca8400693a9827ee31179a83cad to your computer and use it in GitHub Desktop.
Arrays
macro_rules! define_array {
($name: ident, $size: expr) => {
#[derive(Copy, Clone)]
#[repr(C)]
pub struct $name<T: Sized + Clone + Copy + std::fmt::Debug + PartialEq + Eq>([T; $size]);
impl<T: Sized + Clone + Copy + std::fmt::Debug + PartialEq + Eq> std::fmt::Debug for $name<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0[..].fmt(f)
}
}
// impl<T: Sized + Clone + Copy + std::fmt::Debug + PartialEq + Eq> std::ops::Deref for $name<T> {
impl<T: Sized + Clone + Copy + std::fmt::Debug + PartialEq + Eq> std::ops::Deref for $name<T> {
type Target = [T; $size];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: Sized + Clone + Copy + std::fmt::Debug + PartialEq + Eq> std::cmp::PartialEq for $name<T> {
fn eq(&self, other: &Self) -> bool {
self[..].eq(&other[..])
}
}
impl<T: Sized + Clone + Copy + std::fmt::Debug + PartialEq + Eq> std::cmp::Eq for $name<T> {}
};
}
define_array!(Array64, 64);
define_array!(Array128, 128);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment