bp3d_proto/message/macros.rs
1// Copyright (c) 2024, BlockProject 3D
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of BlockProject 3D nor the names of its contributors
14// may be used to endorse or promote products derived from this software
15// without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#[macro_export]
30macro_rules! generate_array_wrapper {
31 ($name: ident, $inner: ident, $t: ty) => {
32 pub struct $name<'a, B>($crate::message::util::Array<B, $t, $inner<&'a [u8]>>);
33
34 impl<'a, B> $name<'a, B> {
35 pub unsafe fn from_raw_parts(data: B, len: usize) -> $name<'a, B> {
36 Self($crate::message::util::Array::<B, $t, $inner<&'a [u8]>>::from_raw_parts(data, len))
37 }
38
39 pub fn from_array(inner: $crate::message::util::Array<B, $t, $inner<&'a [u8]>>) -> Self {
40 Self(inner)
41 }
42
43 pub fn len(&self) -> usize {
44 self.0.len()
45 }
46
47 pub fn is_empty(&self) -> bool {
48 self.0.is_empty()
49 }
50 }
51
52 impl<'a, B: AsRef<[u8]>> $name<'a, B> {
53 pub fn new(data: B) -> $name<'a, B> {
54 Self($crate::message::util::Array::<B, $t, $inner<&'a [u8]>>::new(data))
55 }
56
57 pub fn to_ref(&self) -> $crate::message::util::Array<&[u8], $t, $inner<&[u8]>> {
58 self.0.to_ref()
59 }
60
61 pub fn from_parts(data: B, len: usize) -> Option<Self> {
62 $crate::message::util::Array::<B, $t, $inner<&'a [u8]>>::from_parts(data, len).map(Self)
63 }
64
65 pub fn get(&self, index: usize) -> $inner<&[u8]> {
66 use bp3d_proto::util::Wrap;
67 unsafe { $inner::wrap_unchecked(&self.0[index]) }
68 }
69
70 pub fn try_get(&self, index: usize) -> Option<$inner<&[u8]>> {
71 use bp3d_proto::util::Wrap;
72 self.0.get(index).map(|v| unsafe { $inner::wrap_unchecked(v) })
73 }
74
75 pub fn iter<'b>(&'b self) -> $crate::message::util::array::Iter<'b, $inner<&[u8]>> {
76 $crate::message::util::array::Iter::new(self.0.iter())
77 }
78 }
79
80 impl<'a, B: AsRef<[u8]> + AsMut<[u8]>> $name<'a, B> {
81 pub fn get_mut(&mut self, index: usize) -> $inner<&mut [u8]> {
82 use bp3d_proto::util::Wrap;
83 unsafe { $inner::wrap_unchecked(&mut self.0[index]) }
84 }
85
86 pub fn try_get_mut(&mut self, index: usize) -> Option<$inner<&mut [u8]>> {
87 use bp3d_proto::util::Wrap;
88 self.0.get_mut(index).map(|v| unsafe { $inner::wrap_unchecked(v) })
89 }
90
91 pub fn iter_mut<'b>(&'b mut self) -> $crate::message::util::array::IterMut<'b, $inner<&mut [u8]>> {
92 $crate::message::util::array::IterMut::new(self.0.iter_mut())
93 }
94 }
95 };
96}