impl_memoryview

Macro impl_memoryview 

Source
macro_rules! impl_memoryview {
    ($v:ident<
        $($a: lifetime,)?
        $($param:ident$(: $bound:path)?),*
    > where
        $inner:ty: ViewMut,
        $($where_type:ty: $where_bound:path),*
    {
        |$self:ident, $index:ident| ($collection_expr:expr)[$index_expr:expr]
    }) => { ... };
}
Expand description

A helper macro for implementing ViewRef and ViewMut.

The macro defines a private method inner_index() and implements std::ops::Index, std::ops::IndexMut, ViewRef and ViewMut

use multidimension::{Index, View, ViewRef, ViewMut, impl_memoryview};

struct Reversed<V: View>(V);

impl_memoryview!(Reversed<V: View<I=usize>> where
    V: ViewMut,
{
    |self_, index| (self_.0)[self_.size() - 1 - index]
});

impl<V: View<I=usize>> View for Reversed<V> {
    type I = usize;
    type T = V::T;
    fn size(&self) -> <Self::I as Index>::Size { self.0.size() }
    fn at(&self, index: Self::I) -> Self::T { self.0.at(self.inner_index(index)) }
}