Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / array_ref / src / lib.rs
lib.rs45 lines · 1.6 KiBRaw
| 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 | |
| 16 | //! Crate exposing macros to take array references of slices |
| 17 | |
| 18 | #[cfg(feature = "std")] |
| 19 | extern crate std; |
| 20 | |
| 21 | /// Generate an array reference to a subset of a slice-able bit of data |
| 22 | /// panics if the provided offset and len are out of range of the array |
| 23 | #[macro_export] |
| 24 | macro_rules! array_ref { |
| 25 | ($arr:expr, $offset:expr, $len:expr) => {{ |
| 26 | let offset = $offset; |
| 27 | let slice = &$arr[offset..offset + $len]; |
| 28 | let result: &[u8; $len] = |
| 29 | slice.try_into().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] = |
| 42 | slice.try_into().expect("array ref len and offset should be valid for provided array"); |
| 43 | result |
| 44 | }}; |
| 45 | } |