foyer_common/
buf.rs

1// Copyright 2025 foyer Project Authors
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
15use bytes::{Buf, BufMut};
16
17/// Extend [`Buf`] with `get_isize()` and `get_usize()`.
18pub trait BufExt: Buf {
19    // TODO(MrCroxx): Use `cfg_match` after stable.
20    // cfg_match! {
21    //     cfg(target_pointer_width = "16") => {
22    //         fn get_usize(&mut self) -> usize {
23    //             self.get_u16() as usize
24    //         }
25
26    //         fn get_isize(&mut self) -> isize {
27    //             self.get_i16() as isize
28    //         }
29    //     }
30    //     cfg(target_pointer_width = "32") => {
31    //         fn get_usize(&mut self) -> usize {
32    //             self.get_u32() as usize
33    //         }
34
35    //         fn get_isize(&mut self) -> isize {
36    //             self.get_i32() as isize
37    //         }
38    //     }
39    //     cfg(target_pointer_width = "64") => {
40    //         fn get_usize(&mut self) -> usize {
41    //             self.get_u64() as usize
42    //         }
43
44    //         fn get_isize(&mut self) -> isize {
45    //             self.get_i64() as isize
46    //         }
47    //     }
48    // }
49    cfg_if::cfg_if! {
50        if #[cfg(target_pointer_width = "16")] {
51            /// Gets an usize from self in big-endian byte order and advance the current position.
52            fn get_usize(&mut self) -> usize {
53                self.get_u16() as usize
54            }
55            /// Gets an isize from self in big-endian byte order and advance the current position.
56            fn get_isize(&mut self) -> isize {
57                self.get_i16() as isize
58            }
59        }
60        else if #[cfg(target_pointer_width = "32")] {
61            /// Gets an usize from self in big-endian byte order and advance the current position.
62            fn get_usize(&mut self) -> usize {
63                self.get_u32() as usize
64            }
65            /// Gets an isize from self in big-endian byte order and advance the current position.
66            fn get_isize(&mut self) -> isize {
67                self.get_i32() as isize
68            }
69        }
70        else if #[cfg(target_pointer_width = "64")] {
71            /// Gets an usize from self in big-endian byte order and advance the current position.
72            fn get_usize(&mut self) -> usize {
73                self.get_u64() as usize
74            }
75            /// Gets an isize from self in big-endian byte order and advance the current position.
76            fn get_isize(&mut self) -> isize {
77                self.get_i64() as isize
78            }
79        }
80    }
81}
82
83impl<T: Buf> BufExt for T {}
84
85/// Extend [`BufMut`] with `put_isize()` and `put_usize()`.
86pub trait BufMutExt: BufMut {
87    // TODO(MrCroxx): Use `cfg_match` after stable.
88    // cfg_match! {
89    //     cfg(target_pointer_width = "16") => {
90    //         fn put_usize(&mut self, v: usize) {
91    //             self.put_u16(v as u16);
92    //         }
93
94    //         fn put_isize(&mut self, v: isize) {
95    //             self.put_i16(v as i16);
96    //         }
97    //     }
98    //     cfg(target_pointer_width = "32") => {
99    //         fn put_usize(&mut self, v: usize) {
100    //             self.put_u32(v as u32);
101    //         }
102
103    //         fn put_isize(&mut self, v: isize) {
104    //             self.put_i32(v as i32);
105    //         }
106    //     }
107    //     cfg(target_pointer_width = "64") => {
108    //         fn put_usize(&mut self, v: usize) {
109    //             self.put_u64(v as u64);
110    //         }
111
112    //         fn put_isize(&mut self, v: isize) {
113    //             self.put_i64(v as i64);
114    //         }
115    //     }
116    // }
117    cfg_if::cfg_if! {
118        if #[cfg(target_pointer_width = "16")] {
119            /// Writes an usize to self in the big-endian byte order and advance the current position.
120            fn put_usize(&mut self, v: usize) {
121                self.put_u16(v as u16);
122            }
123            /// Writes an usize to self in the big-endian byte order and advance the current position.
124            fn put_isize(&mut self, v: isize) {
125                self.put_i16(v as i16);
126            }
127        }
128        else if #[cfg(target_pointer_width = "32")] {
129            /// Writes an usize to self in the big-endian byte order and advance the current position.
130            fn put_usize(&mut self, v: usize) {
131                self.put_u32(v as u32);
132            }
133            /// Writes an usize to self in the big-endian byte order and advance the current position.
134            fn put_isize(&mut self, v: isize) {
135                self.put_i32(v as i32);
136            }
137        }
138        else if #[cfg(target_pointer_width = "64")] {
139            /// Writes an usize to self in the big-endian byte order and advance the current position.
140            fn put_usize(&mut self, v: usize) {
141                self.put_u64(v as u64);
142            }
143            /// Writes an usize to self in the big-endian byte order and advance the current position.
144            fn put_isize(&mut self, v: isize) {
145                self.put_i64(v as i64);
146            }
147        }
148    }
149}
150
151impl<T: BufMut> BufMutExt for T {}