foyer_common/buf.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
// Copyright 2024 foyer Project Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use bytes::{Buf, BufMut};
/// Extend [`Buf`] with `get_isize()` and `get_usize()`.
pub trait BufExt: Buf {
// TODO(MrCroxx): Use `cfg_match` after stable.
// cfg_match! {
// cfg(target_pointer_width = "16") => {
// fn get_usize(&mut self) -> usize {
// self.get_u16() as usize
// }
// fn get_isize(&mut self) -> isize {
// self.get_i16() as isize
// }
// }
// cfg(target_pointer_width = "32") => {
// fn get_usize(&mut self) -> usize {
// self.get_u32() as usize
// }
// fn get_isize(&mut self) -> isize {
// self.get_i32() as isize
// }
// }
// cfg(target_pointer_width = "64") => {
// fn get_usize(&mut self) -> usize {
// self.get_u64() as usize
// }
// fn get_isize(&mut self) -> isize {
// self.get_i64() as isize
// }
// }
// }
cfg_if::cfg_if! {
if #[cfg(target_pointer_width = "16")] {
/// Gets an usize from self in big-endian byte order and advance the current position.
fn get_usize(&mut self) -> usize {
self.get_u16() as usize
}
/// Gets an isize from self in big-endian byte order and advance the current position.
fn get_isize(&mut self) -> isize {
self.get_i16() as isize
}
}
else if #[cfg(target_pointer_width = "32")] {
/// Gets an usize from self in big-endian byte order and advance the current position.
fn get_usize(&mut self) -> usize {
self.get_u32() as usize
}
/// Gets an isize from self in big-endian byte order and advance the current position.
fn get_isize(&mut self) -> isize {
self.get_i32() as isize
}
}
else if #[cfg(target_pointer_width = "64")] {
/// Gets an usize from self in big-endian byte order and advance the current position.
fn get_usize(&mut self) -> usize {
self.get_u64() as usize
}
/// Gets an isize from self in big-endian byte order and advance the current position.
fn get_isize(&mut self) -> isize {
self.get_i64() as isize
}
}
}
}
impl<T: Buf> BufExt for T {}
/// Extend [`BufMut`] with `put_isize()` and `put_usize()`.
pub trait BufMutExt: BufMut {
// TODO(MrCroxx): Use `cfg_match` after stable.
// cfg_match! {
// cfg(target_pointer_width = "16") => {
// fn put_usize(&mut self, v: usize) {
// self.put_u16(v as u16);
// }
// fn put_isize(&mut self, v: isize) {
// self.put_i16(v as i16);
// }
// }
// cfg(target_pointer_width = "32") => {
// fn put_usize(&mut self, v: usize) {
// self.put_u32(v as u32);
// }
// fn put_isize(&mut self, v: isize) {
// self.put_i32(v as i32);
// }
// }
// cfg(target_pointer_width = "64") => {
// fn put_usize(&mut self, v: usize) {
// self.put_u64(v as u64);
// }
// fn put_isize(&mut self, v: isize) {
// self.put_i64(v as i64);
// }
// }
// }
cfg_if::cfg_if! {
if #[cfg(target_pointer_width = "16")] {
/// Writes an usize to self in the big-endian byte order and advance the current position.
fn put_usize(&mut self, v: usize) {
self.put_u16(v as u16);
}
/// Writes an usize to self in the big-endian byte order and advance the current position.
fn put_isize(&mut self, v: isize) {
self.put_i16(v as i16);
}
}
else if #[cfg(target_pointer_width = "32")] {
/// Writes an usize to self in the big-endian byte order and advance the current position.
fn put_usize(&mut self, v: usize) {
self.put_u32(v as u32);
}
/// Writes an usize to self in the big-endian byte order and advance the current position.
fn put_isize(&mut self, v: isize) {
self.put_i32(v as i32);
}
}
else if #[cfg(target_pointer_width = "64")] {
/// Writes an usize to self in the big-endian byte order and advance the current position.
fn put_usize(&mut self, v: usize) {
self.put_u64(v as u64);
}
/// Writes an usize to self in the big-endian byte order and advance the current position.
fn put_isize(&mut self, v: isize) {
self.put_i64(v as i64);
}
}
}
}
impl<T: BufMut> BufMutExt for T {}