D:\a\tools.proto\tools.proto\runtime\src\message\macros.rs
Line | Count | Source |
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] |
30 | | macro_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 | 0 | pub unsafe fn from_raw_parts(data: B, len: usize) -> $name<'a, B> { |
36 | 0 | Self($crate::message::util::Array::<B, $t, $inner<&'a [u8]>>::from_raw_parts(data, len)) |
37 | 0 | } |
38 | | |
39 | 2 | pub fn from_array(inner: $crate::message::util::Array<B, $t, $inner<&'a [u8]>>) -> Self { |
40 | 2 | Self(inner) |
41 | 2 | } |
42 | | |
43 | 2 | pub fn len(&self) -> usize { |
44 | 2 | self.0.len() |
45 | 2 | } |
46 | | |
47 | 0 | pub fn is_empty(&self) -> bool { |
48 | 0 | self.0.is_empty() |
49 | 0 | } |
50 | | } |
51 | | |
52 | | impl<'a, B: AsRef<[u8]>> $name<'a, B> { |
53 | 0 | pub fn new(data: B) -> $name<'a, B> { |
54 | 0 | Self($crate::message::util::Array::<B, $t, $inner<&'a [u8]>>::new(data)) |
55 | 0 | } |
56 | | |
57 | 2 | pub fn to_ref(&self) -> $crate::message::util::Array<&[u8], $t, $inner<&[u8]>> { |
58 | 2 | self.0.to_ref() |
59 | 2 | } |
60 | | |
61 | 2 | pub fn from_parts(data: B, len: usize) -> Option<Self> { |
62 | 2 | $crate::message::util::Array::<B, $t, $inner<&'a [u8]>>::from_parts(data, len).map(Self) |
63 | 2 | } |
64 | | |
65 | 24 | pub fn get(&self, index: usize) -> $inner<&[u8]> { |
66 | | use bp3d_proto::util::Wrap; |
67 | 24 | unsafe { $inner::wrap_unchecked(&self.0[index]) } |
68 | 24 | } |
69 | | |
70 | 0 | pub fn try_get(&self, index: usize) -> Option<$inner<&[u8]>> { |
71 | | use bp3d_proto::util::Wrap; |
72 | 0 | self.0.get(index).map(|v| unsafe { $inner::wrap_unchecked(v) }) |
73 | 0 | } |
74 | | |
75 | 1 | pub fn iter<'b>(&'b self) -> $crate::message::util::array::Iter<'b, $inner<&[u8]>> { |
76 | 1 | $crate::message::util::array::Iter::new(self.0.iter()) |
77 | 1 | } |
78 | | } |
79 | | |
80 | | impl<'a, B: AsRef<[u8]> + AsMut<[u8]>> $name<'a, B> { |
81 | 8 | pub fn get_mut(&mut self, index: usize) -> $inner<&mut [u8]> { |
82 | | use bp3d_proto::util::Wrap; |
83 | 8 | unsafe { $inner::wrap_unchecked(&mut self.0[index]) } |
84 | 8 | } |
85 | | |
86 | 0 | pub fn try_get_mut(&mut self, index: usize) -> Option<$inner<&mut [u8]>> { |
87 | | use bp3d_proto::util::Wrap; |
88 | 0 | self.0.get_mut(index).map(|v| unsafe { $inner::wrap_unchecked(v) }) |
89 | 0 | } |
90 | | |
91 | 1 | pub fn iter_mut<'b>(&'b mut self) -> $crate::message::util::array::IterMut<'b, $inner<&mut [u8]>> { |
92 | 1 | $crate::message::util::array::IterMut::new(self.0.iter_mut()) |
93 | 1 | } |
94 | | } |
95 | | }; |
96 | | } |