1 #![no_std] 2 // Copyright 2023 Google LLC 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 #![forbid(unsafe_code)] 16 #![deny(missing_docs)] 17 18 //! Crate exposing macros to take array references of slices 19 20 /// Generate an array reference to a subset of a slice-able bit of data 21 /// panics if the provided offset and len are out of range of the array 22 #[macro_export] 23 macro_rules! array_ref { 24 ($arr:expr, $offset:expr, $len:expr) => {{ 25 let offset = $offset; 26 let slice = &$arr[offset..offset + $len]; 27 let result: &[u8; $len] = slice 28 .try_into() 29 .expect("array ref len and offset should be valid for provided array"); 30 result 31 }}; 32 } 33 34 /// Generates a mutable array reference to a subset of a slice-able bit of data 35 /// panics if the provided offset and len are out of range of the array 36 #[macro_export] 37 macro_rules! array_mut_ref { 38 ($arr:expr, $offset:expr, $len:expr) => {{ 39 let offset = $offset; 40 let slice = &mut $arr[offset..offset + $len]; 41 let result: &mut [u8; $len] = slice 42 .try_into() 43 .expect("array ref len and offset should be valid for provided array"); 44 result 45 }}; 46 } 47