Skip to content

Slice syntax #20

Open
Open

Description

@alan-signal

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());
    }
}

Activity

  1. droundy commented on Oct 6, 2021

    @droundy
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions