Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slice syntax #20

Open
alan-signal opened this issue Jul 14, 2021 · 1 comment
Open

Slice syntax #20

alan-signal opened this issue Jul 14, 2021 · 1 comment

Comments

@alan-signal
Copy link

I wonder if you would be open to a PR that allowed for most slice syntax, or have any thoughts on whether this is a good or bad idea.

A typical usage, calling a method that needs an array:

u16::from_be_bytes(*array_ref!(bytes[1..3]));
Implementation
macro_rules! array_ref {
    ($slice:expr, $offset:expr, $len:expr) => {
        ::arrayref::array_ref!($slice, $offset, $len) // the current macro
    };
    ($slice:tt[$from:tt..$to:expr]) => {
        array_ref!($slice, $from, $to - $from)
    };
    ($slice:tt[..$to:expr]) => {
        array_ref!($slice, 0, $to)
    };
    ($slice:tt[$from:tt..=$to:expr]) => {
        array_ref!($slice, $from, $to - $from + 1)
    };
}

#[cfg(test)]
mod tests {
    use hex_literal::hex;

    #[test]
    fn from_zero_len_2() {
        let bytes: &[u8] = &hex!("01 02 03");
        let a = array_ref!(bytes, 0, 2);
        let b = array_ref!(bytes[..2]);
        let c = array_ref!(bytes[0..2]);
        let d = array_ref!(bytes[0..=1]);
        assert_eq!(a, b);
        assert_eq!(a, c);
        assert_eq!(a, d);
        assert_eq!(vec![1, 2], a.to_vec());
    }

    #[test]
    fn from_one_len_2() {
        let bytes: &[u8] = &hex!("01 02 03");
        let a = array_ref!(bytes, 1, 2);
        let b = array_ref!(bytes[1..3]);
        let c = array_ref!(bytes[1..=2]);
        assert_eq!(a, b);
        assert_eq!(a, c);
        assert_eq!(vec![2, 3], a.to_vec());
    }

    #[test]
    fn from_two_len_1() {
        let bytes: &[u8] = &hex!("07 08 09");
        let a = array_ref!(bytes, 2, 1);
        let b = array_ref!(bytes[2..3]);
        let c = array_ref!(bytes[2..=2]);
        assert_eq!(a, b);
        assert_eq!(a, c);
        assert_eq!(vec![9], a.to_vec());
    }

    #[test]
    #[allow(clippy::many_single_char_names)]
    fn expressions_and_non_literals() {
        let bytes: &[u8] = &hex!("05 06 07 08");
        let a = array_ref!(bytes, 2, 1 + 1);
        let b = array_ref!((bytes[2..])[..1 + 1]);
        let c = array_ref!(bytes[2..1 + 3]);
        let d = array_ref!(bytes[2..=2 + 1]);
        const START: usize = 1 + 1;
        const END: usize = 1 + 2;
        let e = array_ref!(bytes[START..=END]);
        assert_eq!(a, b);
        assert_eq!(a, c);
        assert_eq!(a, d);
        assert_eq!(a, e);
        assert_eq!(vec![7, 8], a.to_vec());
    }
}
@droundy
Copy link
Owner

droundy commented Oct 6, 2021

I don't think so. My understanding is that there is now a slice pattern matching f that should make arrayref obsolete, and would like to see it remain as is. Correct me if I'm wrong...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants