macro_rules! call_numeric_buffer_fn {
    ($fn:ident ::<_,$($params:ident),*>( $data:expr, $($args:expr),* ) or $err:block ) => { ... };
    ($fn:ident ::<_>( $($args:expr),* ) or $err:block ) => { ... };
    ($fn:ident ::<_,$($params:ident),*>( $data:expr ) or $err:block ) => { ... };
    ($data:ident . $fn:ident ::<_,$($params:ident),*>( $($args:expr),* ) or $err:block ) => { ... };
    ($data:ident . $fn:ident ::<_>( $($args:expr),* ) or $err:block ) => { ... };
}
Expand description

Applies $fn to an VecCopy mapping valid numeric data types by corresponding generic parameters. For example, passing an VecCopy containing data of type u8 will cause this macro to call $fn with type parameter u8 like $fn::<u8>(buffer).

Note

Since the introduction o virtual tables to this crate, this mechanism is not as useful since the behaviours of internal elements can now be passed through traits. For instance, the Display example below could be replaced by using a virtual table for the Display trait.

Examples

use std::{fmt, any::Any};
use dync::{VecCopy, call_numeric_buffer_fn};
// Implement pretty printing of a `VecCopy` derivative for numeric buffers.
struct MyBuffer(VecCopy);
impl fmt::Display for MyBuffer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        unsafe fn display_buf<T: Copy + Any + fmt::Display>(buf: &VecCopy, f: &mut fmt::Formatter) {
            for item in buf.iter_as::<T>().unwrap() {
                write!(f, "{} ", item)
                    .expect("Error occurred while writing MyBuffer.");
            }
        }
        call_numeric_buffer_fn!( display_buf::<_>(&self.0, f) or {});
        write!(f, "")
    }
}