Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / array_view / src / lib.rs
lib.rs133 lines · 3.8 KiBRaw
| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! A no_std friendly array wrapper to expose a variable length prefix of the array. |
| 16 | #![no_std] |
| 17 | |
| 18 | #[cfg(feature = "std")] |
| 19 | extern crate std; |
| 20 | |
| 21 | use core::{borrow, fmt}; |
| 22 | |
| 23 | /// A view into the first `len` elements of an array. |
| 24 | /// |
| 25 | /// Useful when you have a fixed size array but variable length data that fits in it. |
| 26 | #[derive(PartialEq, Eq, Clone)] |
| 27 | pub struct ArrayView<T, const N: usize> { |
| 28 | array: [T; N], |
| 29 | len: usize, |
| 30 | } |
| 31 | |
| 32 | // manual impl to avoid showing parts of the buffer beyond len |
| 33 | impl<T: fmt::Debug, const N: usize> fmt::Debug for ArrayView<T, N> { |
| 34 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 35 | self.as_slice().fmt(f) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<T, const N: usize> ArrayView<T, N> { |
| 40 | /// Destructures this ArrayView into a (length, payload) pair. |
| 41 | pub fn into_raw_parts(self) -> (usize, [T; N]) { |
| 42 | (self.len, self.array) |
| 43 | } |
| 44 | /// A version of [`ArrayView#try_from_array`] which panics if `len > buffer.len()`, |
| 45 | /// suitable for usage in `const` contexts. |
| 46 | #[allow(clippy::panic)] |
| 47 | pub const fn const_from_array(array: [T; N], len: usize) -> ArrayView<T, N> { |
| 48 | if N < len { |
| 49 | panic!("Invalid const ArrayView"); |
| 50 | } else { |
| 51 | ArrayView { array, len } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// Create an [ArrayView] of the first `len` elements of `buffer`. |
| 56 | /// |
| 57 | /// Returns `None` if `len > buffer.len()`. |
| 58 | pub fn try_from_array(array: [T; N], len: usize) -> Option<ArrayView<T, N>> { |
| 59 | if N < len { |
| 60 | None |
| 61 | } else { |
| 62 | Some(ArrayView { array, len }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// Returns the occupied portion of the array as a slice. |
| 67 | pub fn as_slice(&self) -> &[T] { |
| 68 | &self.array[..self.len] |
| 69 | } |
| 70 | |
| 71 | /// The length of the data in the view |
| 72 | pub fn len(&self) -> usize { |
| 73 | self.len |
| 74 | } |
| 75 | |
| 76 | /// Returns true if the length is 0 |
| 77 | pub fn is_empty(&self) -> bool { |
| 78 | self.len == 0 |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl<T: Default + Copy, const N: usize> ArrayView<T, N> { |
| 83 | /// Create an `ArrayView` containing the data from the provided slice, assuming the slice can |
| 84 | /// fit in the array size. |
| 85 | pub fn try_from_slice(slice: &[T]) -> Option<ArrayView<T, N>> { |
| 86 | if N < slice.len() { |
| 87 | None |
| 88 | } else { |
| 89 | let mut array = [T::default(); N]; |
| 90 | array[..slice.len()].copy_from_slice(slice); |
| 91 | Some(ArrayView { array, len: slice.len() }) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl<T, const N: usize> AsRef<[T]> for ArrayView<T, N> { |
| 97 | fn as_ref(&self) -> &[T] { |
| 98 | self.as_slice() |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl<T, const N: usize> borrow::Borrow<[T]> for ArrayView<T, N> { |
| 103 | fn borrow(&self) -> &[T] { |
| 104 | self.as_slice() |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | #[allow(clippy::unwrap_used)] |
| 110 | mod tests { |
| 111 | extern crate std; |
| 112 | use crate::ArrayView; |
| 113 | use std::format; |
| 114 | |
| 115 | #[test] |
| 116 | fn debug_only_shows_len_elements() { |
| 117 | assert_eq!( |
| 118 | "[1, 2]", |
| 119 | &format!("{:?}", ArrayView::try_from_array([1, 2, 3, 4, 5], 2).unwrap()) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn try_from_slice_too_long() { |
| 125 | assert_eq!(None, ArrayView::<u8, 3>::try_from_slice(&[1, 2, 3, 4, 5])); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn try_from_slice_ok() { |
| 130 | let view = ArrayView::<u8, 10>::try_from_slice(&[1, 2, 3, 4, 5]).unwrap(); |
| 131 | assert_eq!(&[1, 2, 3, 4, 5], view.as_slice()) |
| 132 | } |
| 133 | } |