algorithm/buf/bt.rs
1// Copyright 2022 - 2023 Wenmeng See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8//
9// Author: tickbh
10// -----
11// Created Date: 2023/08/28 09:38:10
12
13use std::{
14 io::{self},
15 mem,
16};
17
18use super::panic_advance;
19use super::Binary;
20
21macro_rules! try_advance {
22 ($flag:expr) => {
23 if !$flag {
24 return Err(std::io::Error::new(
25 std::io::ErrorKind::UnexpectedEof,
26 "not enough",
27 ));
28 }
29 };
30}
31
32macro_rules! buf_get_impl {
33 ($this:ident, $typ:tt::$conv:tt) => {{
34 const SIZE: usize = mem::size_of::<$typ>();
35 // try to convert directly from the bytes
36 // this Option<ret> trick is to avoid keeping a borrow on self
37 // when advance() is called (mut borrow) and to call bytes() only once
38 let ret = $this
39 .chunk()
40 .get(..SIZE)
41 .map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });
42
43 if let Some(ret) = ret {
44 // if the direct conversion was possible, advance and return
45 $this.advance(SIZE);
46 return ret;
47 } else {
48 // if not we copy the bytes in a temp buffer then convert
49 let mut buf = [0; SIZE];
50 $this.copy_to_slice(&mut buf); // (do the advance)
51 return $typ::$conv(buf);
52 }
53 }};
54 (le => $this:ident, $typ:tt, $len_to_read:expr) => {{
55 debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
56
57 // The same trick as above does not improve the best case speed.
58 // It seems to be linked to the way the method is optimised by the compiler
59 let mut buf = [0; (mem::size_of::<$typ>())];
60 $this.copy_to_slice(&mut buf[..($len_to_read)]);
61 return $typ::from_le_bytes(buf);
62 }};
63 (be => $this:ident, $typ:tt, $len_to_read:expr) => {{
64 debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
65
66 let mut buf = [0; (mem::size_of::<$typ>())];
67 $this.copy_to_slice(&mut buf[mem::size_of::<$typ>() - ($len_to_read)..]);
68 return $typ::from_be_bytes(buf);
69 }};
70}
71
72macro_rules! buf_peek_impl {
73 ($this:ident, $typ:tt::$conv:tt) => {{
74 const SIZE: usize = mem::size_of::<$typ>();
75 // try to convert directly from the bytes
76 // this Option<ret> trick is to avoid keeping a borrow on self
77 // when advance() is called (mut borrow) and to call bytes() only once
78 let ret = $this
79 .chunk()
80 .get(..SIZE)
81 .map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });
82
83 if let Some(ret) = ret {
84 // if the direct conversion was possible, advance and return
85 $this.advance(SIZE);
86 return ret;
87 } else {
88 // if not we copy the bytes in a temp buffer then convert
89 let mut buf = [0; SIZE];
90 $this.peek_to_slice(&mut buf); // (do the advance)
91 return $typ::$conv(buf);
92 }
93 }};
94 (le => $this:ident, $typ:tt, $len_to_read:expr) => {{
95 debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
96
97 // The same trick as above does not improve the best case speed.
98 // It seems to be linked to the way the method is optimised by the compiler
99 let mut buf = [0; (mem::size_of::<$typ>())];
100 $this.peek_to_slice(&mut buf[..($len_to_read)]);
101 return $typ::from_le_bytes(buf);
102 }};
103 (be => $this:ident, $typ:tt, $len_to_read:expr) => {{
104 debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
105
106 let mut buf = [0; (mem::size_of::<$typ>())];
107 $this.peek_to_slice(&mut buf[mem::size_of::<$typ>() - ($len_to_read)..]);
108 return $typ::from_be_bytes(buf);
109 }};
110}
111pub trait Bt {
112 /// 获取剩余数量
113 fn remaining(&self) -> usize;
114
115 /// 获取当前数据的切片引用
116 fn chunk(&self) -> &[u8];
117
118 /// 消耗掉多少字节的数据, 做指针偏移
119 fn advance(&mut self, n: usize);
120
121 /// 消耗掉多少字节的数据并返回消耗的数据
122 fn advance_chunk(&mut self, n: usize) -> &[u8];
123
124 /// 将数据转成Binary
125 fn into_binary(self) -> Binary;
126
127 /// 消耗所有的字节
128 fn advance_all(&mut self) {
129 self.advance(self.remaining());
130 }
131
132 /// 获取当前的值, 但不做任何偏移
133 fn peek(&self) -> Option<u8> {
134 if self.has_remaining() {
135 let ret = self.chunk()[0] as u8;
136 Some(ret)
137 } else {
138 None
139 }
140 }
141
142 /// 是否还有数据
143 fn has_remaining(&self) -> bool {
144 self.remaining() > 0
145 }
146
147 /// 获取当前的值并将偏移值+1
148 fn get_next(&mut self) -> Option<u8> {
149 if self.has_remaining() {
150 let val = self.peek().unwrap();
151 self.advance(1);
152 Some(val)
153 } else {
154 None
155 }
156 }
157
158 /// 拷贝数据 `self` into `dst`.
159 ///
160 /// # Examples
161 /// ```
162 /// use algorithm::buf::Bt;
163 ///
164 /// let mut buf = &b"hello world"[..];
165 /// let mut dst = [0; 5];
166 ///
167 /// buf.copy_to_slice(&mut dst);
168 /// assert_eq!(&b"hello"[..], &dst);
169 /// assert_eq!(6, buf.remaining());
170 /// ```
171 ///
172 /// # Panics
173 ///
174 /// This function panics if `self.remaining() < dst.len()`
175 fn copy_to_slice(&mut self, dst: &mut [u8]) -> usize {
176 assert!(self.remaining() >= dst.len());
177 unsafe {
178 let src = self.chunk();
179 std::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), dst.len());
180 self.advance(dst.len())
181 }
182 dst.len()
183 }
184
185 fn peek_to_slice(&mut self, dst: &mut [u8]) -> usize {
186 assert!(self.remaining() >= dst.len());
187 unsafe {
188 let src = self.chunk();
189 std::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), dst.len());
190 }
191 dst.len()
192 }
193
194 fn get_bool(&mut self) -> bool {
195 assert!(self.remaining() >= 1);
196 let ret = self.chunk()[0];
197 self.advance(1);
198 ret == 1
199 }
200
201 fn peek_bool(&mut self) -> bool {
202 assert!(self.remaining() >= 1);
203 let ret = self.chunk()[0];
204 ret == 1
205 }
206
207 fn try_get_bool(&mut self) -> io::Result<bool> {
208 try_advance!(self.remaining() >= 1);
209 Ok(self.get_bool())
210 }
211
212 fn get_u8(&mut self) -> u8 {
213 assert!(self.remaining() >= 1);
214 let ret = self.chunk()[0];
215 self.advance(1);
216 ret
217 }
218
219 fn peek_u8(&mut self) -> u8 {
220 assert!(self.remaining() >= 1);
221 let ret = self.chunk()[0];
222 ret
223 }
224
225 fn try_get_u8(&mut self) -> io::Result<u8> {
226 try_advance!(self.remaining() >= 1);
227 Ok(self.get_u8())
228 }
229
230 fn get_i8(&mut self) -> i8 {
231 assert!(self.remaining() >= 1);
232 let ret = self.chunk()[0] as i8;
233 self.advance(1);
234 ret
235 }
236
237 fn peek_i8(&mut self) -> i8 {
238 assert!(self.remaining() >= 1);
239 let ret = self.chunk()[0] as i8;
240 ret
241 }
242
243 fn try_get_i8(&mut self) -> io::Result<i8> {
244 try_advance!(self.remaining() >= 1);
245 Ok(self.get_i8())
246 }
247
248 /// Gets an unsigned 16 bit integer from `self` in big-endian byte order.
249 ///
250 /// The current position is advanced by 2.
251 ///
252 /// # Examples
253 ///
254 /// ```
255 /// use algorithm::buf::Bt;
256 ///
257 /// let mut buf = &b"\x08\x09 hello"[..];
258 /// assert_eq!(0x0809, buf.get_u16());
259 /// ```
260 ///
261 /// # Panics
262 ///
263 /// This function panics if there is not enough remaining data in `self`.
264 fn get_u16(&mut self) -> u16 {
265 buf_get_impl!(self, u16::from_be_bytes);
266 }
267
268 fn peek_u16(&mut self) -> u16 {
269 buf_peek_impl!(self, u16::from_be_bytes);
270 }
271
272 fn try_get_u16(&mut self) -> io::Result<u16> {
273 try_advance!(self.remaining() >= 2);
274 Ok(self.get_u16())
275 }
276 /// Gets an unsigned 16 bit integer from `self` in little-endian byte order.
277 ///
278 /// The current position is advanced by 2.
279 ///
280 /// # Examples
281 ///
282 /// ```
283 /// use algorithm::buf::Bt;
284 ///
285 /// let mut buf = &b"\x09\x08 hello"[..];
286 /// assert_eq!(0x0809, buf.get_u16_le());
287 /// ```
288 ///
289 /// # Panics
290 ///
291 /// This function panics if there is not enough remaining data in `self`.
292 fn get_u16_le(&mut self) -> u16 {
293 buf_get_impl!(self, u16::from_le_bytes);
294 }
295
296 fn peek_u16_le(&mut self) -> u16 {
297 buf_peek_impl!(self, u16::from_le_bytes);
298 }
299
300 fn try_get_u16_le(&mut self) -> io::Result<u16> {
301 try_advance!(self.remaining() >= 2);
302 Ok(self.get_u16_le())
303 }
304
305 /// Gets an unsigned 16 bit integer from `self` in native-endian byte order.
306 ///
307 /// The current position is advanced by 2.
308 ///
309 /// # Examples
310 ///
311 /// ```
312 /// use algorithm::buf::Bt;
313 ///
314 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
315 /// true => b"\x08\x09 hello",
316 /// false => b"\x09\x08 hello",
317 /// };
318 /// assert_eq!(0x0809, buf.get_u16_ne());
319 /// ```
320 ///
321 /// # Panics
322 ///
323 /// This function panics if there is not enough remaining data in `self`.
324 fn get_u16_ne(&mut self) -> u16 {
325 buf_get_impl!(self, u16::from_ne_bytes);
326 }
327
328 fn peek_u16_ne(&mut self) -> u16 {
329 buf_peek_impl!(self, u16::from_ne_bytes);
330 }
331
332 fn try_get_u16_ne(&mut self) -> io::Result<u16> {
333 try_advance!(self.remaining() >= 2);
334 Ok(self.get_u16_ne())
335 }
336
337 /// Gets a signed 16 bit integer from `self` in big-endian byte order.
338 ///
339 /// The current position is advanced by 2.
340 ///
341 /// # Examples
342 ///
343 /// ```
344 /// use algorithm::buf::Bt;
345 ///
346 /// let mut buf = &b"\x08\x09 hello"[..];
347 /// assert_eq!(0x0809, buf.get_i16());
348 /// ```
349 ///
350 /// # Panics
351 ///
352 /// This function panics if there is not enough remaining data in `self`.
353 fn get_i16(&mut self) -> i16 {
354 buf_get_impl!(self, i16::from_be_bytes);
355 }
356
357 fn peek_i16(&mut self) -> i16 {
358 buf_peek_impl!(self, i16::from_be_bytes);
359 }
360
361 fn try_get_i16(&mut self) -> io::Result<i16> {
362 try_advance!(self.remaining() >= 2);
363 Ok(self.get_i16())
364 }
365
366 /// Gets a signed 16 bit integer from `self` in little-endian byte order.
367 ///
368 /// The current position is advanced by 2.
369 ///
370 /// # Examples
371 ///
372 /// ```
373 /// use algorithm::buf::Bt;
374 ///
375 /// let mut buf = &b"\x09\x08 hello"[..];
376 /// assert_eq!(0x0809, buf.get_i16_le());
377 /// ```
378 ///
379 /// # Panics
380 ///
381 /// This function panics if there is not enough remaining data in `self`.
382 fn get_i16_le(&mut self) -> i16 {
383 buf_get_impl!(self, i16::from_le_bytes);
384 }
385 fn peek_i16_le(&mut self) -> i16 {
386 buf_peek_impl!(self, i16::from_le_bytes);
387 }
388 fn try_get_i16_le(&mut self) -> io::Result<i16> {
389 try_advance!(self.remaining() >= 2);
390 Ok(self.get_i16_le())
391 }
392 /// Gets a signed 16 bit integer from `self` in native-endian byte order.
393 ///
394 /// The current position is advanced by 2.
395 ///
396 /// # Examples
397 ///
398 /// ```
399 /// use algorithm::buf::Bt;
400 ///
401 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
402 /// true => b"\x08\x09 hello",
403 /// false => b"\x09\x08 hello",
404 /// };
405 /// assert_eq!(0x0809, buf.get_i16_ne());
406 /// ```
407 ///
408 /// # Panics
409 ///
410 /// This function panics if there is not enough remaining data in `self`.
411 fn get_i16_ne(&mut self) -> i16 {
412 buf_get_impl!(self, i16::from_ne_bytes);
413 }
414
415 fn peek_i16_ne(&mut self) -> i16 {
416 buf_peek_impl!(self, i16::from_ne_bytes);
417 }
418
419 fn try_get_i16_ne(&mut self) -> io::Result<i16> {
420 try_advance!(self.remaining() >= 2);
421 Ok(self.get_i16_ne())
422 }
423 /// Gets an unsigned 32 bit integer from `self` in the big-endian byte order.
424 ///
425 /// The current position is advanced by 4.
426 ///
427 /// # Examples
428 ///
429 /// ```
430 /// use algorithm::buf::Bt;
431 ///
432 /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
433 /// assert_eq!(0x0809A0A1, buf.get_u32());
434 /// ```
435 ///
436 /// # Panics
437 ///
438 /// This function panics if there is not enough remaining data in `self`.
439 fn get_u32(&mut self) -> u32 {
440 buf_get_impl!(self, u32::from_be_bytes);
441 }
442
443 fn peek_u32(&mut self) -> u32 {
444 buf_peek_impl!(self, u32::from_be_bytes);
445 }
446
447 fn try_get_u32(&mut self) -> io::Result<u32> {
448 try_advance!(self.remaining() >= 4);
449 Ok(self.get_u32())
450 }
451 /// Gets an unsigned 32 bit integer from `self` in the little-endian byte order.
452 ///
453 /// The current position is advanced by 4.
454 ///
455 /// # Examples
456 ///
457 /// ```
458 /// use algorithm::buf::Bt;
459 ///
460 /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
461 /// assert_eq!(0x0809A0A1, buf.get_u32_le());
462 /// ```
463 ///
464 /// # Panics
465 ///
466 /// This function panics if there is not enough remaining data in `self`.
467 fn get_u32_le(&mut self) -> u32 {
468 buf_get_impl!(self, u32::from_le_bytes);
469 }
470
471 fn peek_u32_le(&mut self) -> u32 {
472 buf_peek_impl!(self, u32::from_le_bytes);
473 }
474
475 fn try_get_u32_le(&mut self) -> io::Result<u32> {
476 try_advance!(self.remaining() >= 4);
477 Ok(self.get_u32_le())
478 }
479 /// Gets an unsigned 32 bit integer from `self` in native-endian byte order.
480 ///
481 /// The current position is advanced by 4.
482 ///
483 /// # Examples
484 ///
485 /// ```
486 /// use algorithm::buf::Bt;
487 ///
488 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
489 /// true => b"\x08\x09\xA0\xA1 hello",
490 /// false => b"\xA1\xA0\x09\x08 hello",
491 /// };
492 /// assert_eq!(0x0809A0A1, buf.get_u32_ne());
493 /// ```
494 ///
495 /// # Panics
496 ///
497 /// This function panics if there is not enough remaining data in `self`.
498 fn get_u32_ne(&mut self) -> u32 {
499 buf_get_impl!(self, u32::from_ne_bytes);
500 }
501
502 fn peek_u32_ne(&mut self) -> u32 {
503 buf_peek_impl!(self, u32::from_ne_bytes);
504 }
505
506 fn try_get_u32_ne(&mut self) -> io::Result<u32> {
507 try_advance!(self.remaining() >= 4);
508 Ok(self.get_u32_ne())
509 }
510 /// Gets a signed 32 bit integer from `self` in big-endian byte order.
511 ///
512 /// The current position is advanced by 4.
513 ///
514 /// # Examples
515 ///
516 /// ```
517 /// use algorithm::buf::Bt;
518 ///
519 /// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..];
520 /// assert_eq!(0x0809A0A1, buf.get_i32());
521 /// ```
522 ///
523 /// # Panics
524 ///
525 /// This function panics if there is not enough remaining data in `self`.
526 fn get_i32(&mut self) -> i32 {
527 buf_get_impl!(self, i32::from_be_bytes);
528 }
529
530 fn peek_i32(&mut self) -> i32 {
531 buf_peek_impl!(self, i32::from_be_bytes);
532 }
533
534 fn try_get_i32(&mut self) -> io::Result<i32> {
535 try_advance!(self.remaining() >= 4);
536 Ok(self.get_i32())
537 }
538
539 /// Gets a signed 32 bit integer from `self` in little-endian byte order.
540 ///
541 /// The current position is advanced by 4.
542 ///
543 /// # Examples
544 ///
545 /// ```
546 /// use algorithm::buf::Bt;
547 ///
548 /// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..];
549 /// assert_eq!(0x0809A0A1, buf.get_i32_le());
550 /// ```
551 ///
552 /// # Panics
553 ///
554 /// This function panics if there is not enough remaining data in `self`.
555 fn get_i32_le(&mut self) -> i32 {
556 buf_get_impl!(self, i32::from_le_bytes);
557 }
558
559 fn peek_i32_le(&mut self) -> i32 {
560 buf_peek_impl!(self, i32::from_le_bytes);
561 }
562
563 fn try_get_i32_le(&mut self) -> io::Result<i32> {
564 try_advance!(self.remaining() >= 4);
565 Ok(self.get_i32_le())
566 }
567
568 /// Gets a signed 32 bit integer from `self` in native-endian byte order.
569 ///
570 /// The current position is advanced by 4.
571 ///
572 /// # Examples
573 ///
574 /// ```
575 /// use algorithm::buf::Bt;
576 ///
577 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
578 /// true => b"\x08\x09\xA0\xA1 hello",
579 /// false => b"\xA1\xA0\x09\x08 hello",
580 /// };
581 /// assert_eq!(0x0809A0A1, buf.get_i32_ne());
582 /// ```
583 ///
584 /// # Panics
585 ///
586 /// This function panics if there is not enough remaining data in `self`.
587 fn get_i32_ne(&mut self) -> i32 {
588 buf_get_impl!(self, i32::from_ne_bytes);
589 }
590
591 fn peek_i32_ne(&mut self) -> i32 {
592 buf_peek_impl!(self, i32::from_ne_bytes);
593 }
594
595 fn try_get_i32_ne(&mut self) -> io::Result<i32> {
596 try_advance!(self.remaining() >= 4);
597 Ok(self.get_i32_ne())
598 }
599 /// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
600 ///
601 /// The current position is advanced by 8.
602 ///
603 /// # Examples
604 ///
605 /// ```
606 /// use algorithm::buf::Bt;
607 ///
608 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
609 /// assert_eq!(0x0102030405060708, buf.get_u64());
610 /// ```
611 ///
612 /// # Panics
613 ///
614 /// This function panics if there is not enough remaining data in `self`.
615 fn get_u64(&mut self) -> u64 {
616 buf_get_impl!(self, u64::from_be_bytes);
617 }
618
619 fn peek_u64(&mut self) -> u64 {
620 buf_peek_impl!(self, u64::from_be_bytes);
621 }
622
623 fn try_get_u64(&mut self) -> io::Result<u64> {
624 try_advance!(self.remaining() >= 8);
625 Ok(self.get_u64())
626 }
627
628 /// Gets an unsigned 64 bit integer from `self` in little-endian byte order.
629 ///
630 /// The current position is advanced by 8.
631 ///
632 /// # Examples
633 ///
634 /// ```
635 /// use algorithm::buf::Bt;
636 ///
637 /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
638 /// assert_eq!(0x0102030405060708, buf.get_u64_le());
639 /// ```
640 ///
641 /// # Panics
642 ///
643 /// This function panics if there is not enough remaining data in `self`.
644 fn get_u64_le(&mut self) -> u64 {
645 buf_get_impl!(self, u64::from_le_bytes);
646 }
647
648 fn peek_u64_le(&mut self) -> u64 {
649 buf_peek_impl!(self, u64::from_le_bytes);
650 }
651
652 fn try_get_u64_le(&mut self) -> io::Result<u64> {
653 try_advance!(self.remaining() >= 8);
654 Ok(self.get_u64_le())
655 }
656
657 /// Gets an unsigned 64 bit integer from `self` in native-endian byte order.
658 ///
659 /// The current position is advanced by 8.
660 ///
661 /// # Examples
662 ///
663 /// ```
664 /// use algorithm::buf::Bt;
665 ///
666 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
667 /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
668 /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
669 /// };
670 /// assert_eq!(0x0102030405060708, buf.get_u64_ne());
671 /// ```
672 ///
673 /// # Panics
674 ///
675 /// This function panics if there is not enough remaining data in `self`.
676 fn get_u64_ne(&mut self) -> u64 {
677 buf_get_impl!(self, u64::from_ne_bytes);
678 }
679
680 fn peek_u64_ne(&mut self) -> u64 {
681 buf_peek_impl!(self, u64::from_ne_bytes);
682 }
683
684 fn try_get_u64_ne(&mut self) -> io::Result<u64> {
685 try_advance!(self.remaining() >= 8);
686 Ok(self.get_u64_ne())
687 }
688 /// Gets a signed 64 bit integer from `self` in big-endian byte order.
689 ///
690 /// The current position is advanced by 8.
691 ///
692 /// # Examples
693 ///
694 /// ```
695 /// use algorithm::buf::Bt;
696 ///
697 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..];
698 /// assert_eq!(0x0102030405060708, buf.get_i64());
699 /// ```
700 ///
701 /// # Panics
702 ///
703 /// This function panics if there is not enough remaining data in `self`.
704 fn get_i64(&mut self) -> i64 {
705 buf_get_impl!(self, i64::from_be_bytes);
706 }
707
708 fn peek_i64(&mut self) -> i64 {
709 buf_peek_impl!(self, i64::from_be_bytes);
710 }
711
712 fn try_get_i64(&mut self) -> io::Result<i64> {
713 try_advance!(self.remaining() >= 8);
714 Ok(self.get_i64())
715 }
716 /// Gets a signed 64 bit integer from `self` in little-endian byte order.
717 ///
718 /// The current position is advanced by 8.
719 ///
720 /// # Examples
721 ///
722 /// ```
723 /// use algorithm::buf::Bt;
724 ///
725 /// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
726 /// assert_eq!(0x0102030405060708, buf.get_i64_le());
727 /// ```
728 ///
729 /// # Panics
730 ///
731 /// This function panics if there is not enough remaining data in `self`.
732 fn get_i64_le(&mut self) -> i64 {
733 buf_get_impl!(self, i64::from_le_bytes);
734 }
735
736 fn peek_i64_le(&mut self) -> i64 {
737 buf_peek_impl!(self, i64::from_le_bytes);
738 }
739
740 fn try_get_i64_le(&mut self) -> io::Result<i64> {
741 try_advance!(self.remaining() >= 8);
742 Ok(self.get_i64_le())
743 }
744 /// Gets a signed 64 bit integer from `self` in native-endian byte order.
745 ///
746 /// The current position is advanced by 8.
747 ///
748 /// # Examples
749 ///
750 /// ```
751 /// use algorithm::buf::Bt;
752 ///
753 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
754 /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello",
755 /// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello",
756 /// };
757 /// assert_eq!(0x0102030405060708, buf.get_i64_ne());
758 /// ```
759 ///
760 /// # Panics
761 ///
762 /// This function panics if there is not enough remaining data in `self`.
763 fn get_i64_ne(&mut self) -> i64 {
764 buf_get_impl!(self, i64::from_ne_bytes);
765 }
766
767 fn peek_i64_ne(&mut self) -> i64 {
768 buf_peek_impl!(self, i64::from_ne_bytes);
769 }
770
771 fn try_get_i64_ne(&mut self) -> io::Result<i64> {
772 try_advance!(self.remaining() >= 8);
773 Ok(self.get_i64_ne())
774 }
775
776 /// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
777 ///
778 /// The current position is advanced by 16.
779 ///
780 /// # Examples
781 ///
782 /// ```
783 /// use algorithm::buf::Bt;
784 ///
785 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
786 /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128());
787 /// ```
788 ///
789 /// # Panics
790 ///
791 /// This function panics if there is not enough remaining data in `self`.
792 fn get_u128(&mut self) -> u128 {
793 buf_get_impl!(self, u128::from_be_bytes);
794 }
795
796 fn peek_u128(&mut self) -> u128 {
797 buf_peek_impl!(self, u128::from_be_bytes);
798 }
799
800 fn try_get_u128(&mut self) -> io::Result<u128> {
801 try_advance!(self.remaining() >= 16);
802 Ok(self.get_u128())
803 }
804 /// Gets an unsigned 128 bit integer from `self` in little-endian byte order.
805 ///
806 /// The current position is advanced by 16.
807 ///
808 /// # Examples
809 ///
810 /// ```
811 /// use algorithm::buf::Bt;
812 ///
813 /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
814 /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le());
815 /// ```
816 ///
817 /// # Panics
818 ///
819 /// This function panics if there is not enough remaining data in `self`.
820 fn get_u128_le(&mut self) -> u128 {
821 buf_get_impl!(self, u128::from_le_bytes);
822 }
823
824 fn peek_u128_le(&mut self) -> u128 {
825 buf_peek_impl!(self, u128::from_le_bytes);
826 }
827
828 fn try_get_u128_le(&mut self) -> io::Result<u128> {
829 try_advance!(self.remaining() >= 16);
830 Ok(self.get_u128_le())
831 }
832 /// Gets an unsigned 128 bit integer from `self` in native-endian byte order.
833 ///
834 /// The current position is advanced by 16.
835 ///
836 /// # Examples
837 ///
838 /// ```
839 /// use algorithm::buf::Bt;
840 ///
841 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
842 /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
843 /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
844 /// };
845 /// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_ne());
846 /// ```
847 ///
848 /// # Panics
849 ///
850 /// This function panics if there is not enough remaining data in `self`.
851 fn get_u128_ne(&mut self) -> u128 {
852 buf_get_impl!(self, u128::from_ne_bytes);
853 }
854
855 fn peek_u128_ne(&mut self) -> u128 {
856 buf_peek_impl!(self, u128::from_ne_bytes);
857 }
858
859 fn try_get_u128_ne(&mut self) -> io::Result<u128> {
860 try_advance!(self.remaining() >= 16);
861 Ok(self.get_u128_ne())
862 }
863
864 /// Gets a signed 128 bit integer from `self` in big-endian byte order.
865 ///
866 /// The current position is advanced by 16.
867 ///
868 /// # Examples
869 ///
870 /// ```
871 /// use algorithm::buf::Bt;
872 ///
873 /// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..];
874 /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128());
875 /// ```
876 ///
877 /// # Panics
878 ///
879 /// This function panics if there is not enough remaining data in `self`.
880 fn get_i128(&mut self) -> i128 {
881 buf_get_impl!(self, i128::from_be_bytes);
882 }
883
884 fn peek_i128(&mut self) -> i128 {
885 buf_peek_impl!(self, i128::from_be_bytes);
886 }
887
888 fn try_get_i128(&mut self) -> io::Result<i128> {
889 try_advance!(self.remaining() >= 16);
890 Ok(self.get_i128())
891 }
892
893 /// Gets a signed 128 bit integer from `self` in little-endian byte order.
894 ///
895 /// The current position is advanced by 16.
896 ///
897 /// # Examples
898 ///
899 /// ```
900 /// use algorithm::buf::Bt;
901 ///
902 /// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..];
903 /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le());
904 /// ```
905 ///
906 /// # Panics
907 ///
908 /// This function panics if there is not enough remaining data in `self`.
909 fn get_i128_le(&mut self) -> i128 {
910 buf_get_impl!(self, i128::from_le_bytes);
911 }
912
913 fn peek_i128_le(&mut self) -> i128 {
914 buf_peek_impl!(self, i128::from_le_bytes);
915 }
916
917 fn try_get_i128_le(&mut self) -> io::Result<i128> {
918 try_advance!(self.remaining() >= 16);
919 Ok(self.get_i128_le())
920 }
921 /// Gets a signed 128 bit integer from `self` in native-endian byte order.
922 ///
923 /// The current position is advanced by 16.
924 ///
925 /// # Examples
926 ///
927 /// ```
928 /// use algorithm::buf::Bt;
929 ///
930 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
931 /// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello",
932 /// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello",
933 /// };
934 /// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_ne());
935 /// ```
936 ///
937 /// # Panics
938 ///
939 /// This function panics if there is not enough remaining data in `self`.
940 fn get_i128_ne(&mut self) -> i128 {
941 buf_get_impl!(self, i128::from_ne_bytes);
942 }
943
944 fn peek_i128_ne(&mut self) -> i128 {
945 buf_peek_impl!(self, i128::from_ne_bytes);
946 }
947
948 fn try_get_i128_ne(&mut self) -> io::Result<i128> {
949 try_advance!(self.remaining() >= 16);
950 Ok(self.get_i128_ne())
951 }
952 /// Gets an unsigned n-byte integer from `self` in big-endian byte order.
953 ///
954 /// The current position is advanced by `nbytes`.
955 ///
956 /// # Examples
957 ///
958 /// ```
959 /// use algorithm::buf::Bt;
960 ///
961 /// let mut buf = &b"\x01\x02\x03 hello"[..];
962 /// assert_eq!(0x010203, buf.get_uint(3));
963 /// ```
964 ///
965 /// # Panics
966 ///
967 /// This function panics if there is not enough remaining data in `self`.
968 fn get_uint(&mut self, nbytes: usize) -> u64 {
969 buf_get_impl!(be => self, u64, nbytes);
970 }
971
972 fn peek_uint(&mut self, nbytes: usize) -> u64 {
973 buf_peek_impl!(be => self, u64, nbytes);
974 }
975
976 fn try_get_uint(&mut self, nbytes: usize) -> io::Result<u64> {
977 try_advance!(self.remaining() >= 8);
978 Ok(self.get_uint(nbytes))
979 }
980 /// Gets an unsigned n-byte integer from `self` in little-endian byte order.
981 ///
982 /// The current position is advanced by `nbytes`.
983 ///
984 /// # Examples
985 ///
986 /// ```
987 /// use algorithm::buf::Bt;
988 ///
989 /// let mut buf = &b"\x03\x02\x01 hello"[..];
990 /// assert_eq!(0x010203, buf.get_uint_le(3));
991 /// ```
992 ///
993 /// # Panics
994 ///
995 /// This function panics if there is not enough remaining data in `self`.
996 fn get_uint_le(&mut self, nbytes: usize) -> u64 {
997 buf_get_impl!(le => self, u64, nbytes);
998 }
999
1000 fn peek_uint_le(&mut self, nbytes: usize) -> u64 {
1001 buf_peek_impl!(le => self, u64, nbytes);
1002 }
1003
1004 fn try_get_uint_le(&mut self, nbytes: usize) -> io::Result<u64> {
1005 try_advance!(self.remaining() >= 8);
1006 Ok(self.get_uint_le(nbytes))
1007 }
1008 /// Gets an unsigned n-byte integer from `self` in native-endian byte order.
1009 ///
1010 /// The current position is advanced by `nbytes`.
1011 ///
1012 /// # Examples
1013 ///
1014 /// ```
1015 /// use algorithm::buf::Bt;
1016 ///
1017 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1018 /// true => b"\x01\x02\x03 hello",
1019 /// false => b"\x03\x02\x01 hello",
1020 /// };
1021 /// assert_eq!(0x010203, buf.get_uint_ne(3));
1022 /// ```
1023 ///
1024 /// # Panics
1025 ///
1026 /// This function panics if there is not enough remaining data in `self`.
1027 fn get_uint_ne(&mut self, nbytes: usize) -> u64 {
1028 if cfg!(target_endian = "big") {
1029 self.get_uint(nbytes)
1030 } else {
1031 self.get_uint_le(nbytes)
1032 }
1033 }
1034
1035 fn peek_uint_ne(&mut self, nbytes: usize) -> u64 {
1036 if cfg!(target_endian = "big") {
1037 self.peek_uint(nbytes)
1038 } else {
1039 self.peek_uint_le(nbytes)
1040 }
1041 }
1042
1043 fn try_get_uint_ne(&mut self, nbytes: usize) -> io::Result<u64> {
1044 try_advance!(self.remaining() >= 8);
1045 Ok(self.get_uint_ne(nbytes))
1046 }
1047 /// Gets a signed n-byte integer from `self` in big-endian byte order.
1048 ///
1049 /// The current position is advanced by `nbytes`.
1050 ///
1051 /// # Examples
1052 ///
1053 /// ```
1054 /// use algorithm::buf::Bt;
1055 ///
1056 /// let mut buf = &b"\x01\x02\x03 hello"[..];
1057 /// assert_eq!(0x010203, buf.get_int(3));
1058 /// ```
1059 ///
1060 /// # Panics
1061 ///
1062 /// This function panics if there is not enough remaining data in `self`.
1063 fn get_int(&mut self, nbytes: usize) -> i64 {
1064 buf_get_impl!(be => self, i64, nbytes);
1065 }
1066
1067 fn peek_int(&mut self, nbytes: usize) -> i64 {
1068 buf_peek_impl!(be => self, i64, nbytes);
1069 }
1070
1071 fn try_get_int(&mut self, nbytes: usize) -> io::Result<i64> {
1072 try_advance!(self.remaining() >= 8);
1073 Ok(self.get_int(nbytes))
1074 }
1075 /// Gets a signed n-byte integer from `self` in little-endian byte order.
1076 ///
1077 /// The current position is advanced by `nbytes`.
1078 ///
1079 /// # Examples
1080 ///
1081 /// ```
1082 /// use algorithm::buf::Bt;
1083 ///
1084 /// let mut buf = &b"\x03\x02\x01 hello"[..];
1085 /// assert_eq!(0x010203, buf.get_int_le(3));
1086 /// ```
1087 ///
1088 /// # Panics
1089 ///
1090 /// This function panics if there is not enough remaining data in `self`.
1091 fn get_int_le(&mut self, nbytes: usize) -> i64 {
1092 buf_get_impl!(le => self, i64, nbytes);
1093 }
1094
1095 fn peek_int_le(&mut self, nbytes: usize) -> i64 {
1096 buf_peek_impl!(le => self, i64, nbytes);
1097 }
1098
1099 fn try_get_int_le(&mut self, nbytes: usize) -> io::Result<i64> {
1100 try_advance!(self.remaining() >= 8);
1101 Ok(self.get_int_le(nbytes))
1102 }
1103 /// Gets a signed n-byte integer from `self` in native-endian byte order.
1104 ///
1105 /// The current position is advanced by `nbytes`.
1106 ///
1107 /// # Examples
1108 ///
1109 /// ```
1110 /// use algorithm::buf::Bt;
1111 ///
1112 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1113 /// true => b"\x01\x02\x03 hello",
1114 /// false => b"\x03\x02\x01 hello",
1115 /// };
1116 /// assert_eq!(0x010203, buf.get_int_ne(3));
1117 /// ```
1118 ///
1119 /// # Panics
1120 ///
1121 /// This function panics if there is not enough remaining data in `self`.
1122 fn get_int_ne(&mut self, nbytes: usize) -> i64 {
1123 if cfg!(target_endian = "big") {
1124 self.get_int(nbytes)
1125 } else {
1126 self.get_int_le(nbytes)
1127 }
1128 }
1129
1130 fn peek_int_ne(&mut self, nbytes: usize) -> i64 {
1131 if cfg!(target_endian = "big") {
1132 self.peek_int(nbytes)
1133 } else {
1134 self.peek_int_le(nbytes)
1135 }
1136 }
1137
1138 fn try_get_int_ne(&mut self, nbytes: usize) -> io::Result<i64> {
1139 try_advance!(self.remaining() >= 8);
1140 Ok(self.get_int_ne(nbytes))
1141 }
1142 /// Gets an IEEE754 single-precision (4 bytes) floating point number from
1143 /// `self` in big-endian byte order.
1144 ///
1145 /// The current position is advanced by 4.
1146 ///
1147 /// # Examples
1148 ///
1149 /// ```
1150 /// use algorithm::buf::Bt;
1151 ///
1152 /// let mut buf = &b"\x3F\x99\x99\x9A hello"[..];
1153 /// assert_eq!(1.2f32, buf.get_f32());
1154 /// ```
1155 ///
1156 /// # Panics
1157 ///
1158 /// This function panics if there is not enough remaining data in `self`.
1159 fn get_f32(&mut self) -> f32 {
1160 f32::from_bits(Self::get_u32(self))
1161 }
1162
1163 fn peek_f32(&mut self) -> f32 {
1164 f32::from_bits(Self::peek_u32(self))
1165 }
1166
1167 fn try_get_f32(&mut self) -> io::Result<f32> {
1168 try_advance!(self.remaining() >= 4);
1169 Ok(self.get_f32())
1170 }
1171 /// Gets an IEEE754 single-precision (4 bytes) floating point number from
1172 /// `self` in little-endian byte order.
1173 ///
1174 /// The current position is advanced by 4.
1175 ///
1176 /// # Examples
1177 ///
1178 /// ```
1179 /// use algorithm::buf::Bt;
1180 ///
1181 /// let mut buf = &b"\x9A\x99\x99\x3F hello"[..];
1182 /// assert_eq!(1.2f32, buf.get_f32_le());
1183 /// ```
1184 ///
1185 /// # Panics
1186 ///
1187 /// This function panics if there is not enough remaining data in `self`.
1188 fn get_f32_le(&mut self) -> f32 {
1189 f32::from_bits(Self::get_u32_le(self))
1190 }
1191
1192 fn peek_f32_le(&mut self) -> f32 {
1193 f32::from_bits(Self::peek_u32_le(self))
1194 }
1195
1196 fn try_get_f32_le(&mut self) -> io::Result<f32> {
1197 try_advance!(self.remaining() >= 4);
1198 Ok(self.get_f32_le())
1199 }
1200 /// Gets an IEEE754 single-precision (4 bytes) floating point number from
1201 /// `self` in native-endian byte order.
1202 ///
1203 /// The current position is advanced by 4.
1204 ///
1205 /// # Examples
1206 ///
1207 /// ```
1208 /// use algorithm::buf::Bt;
1209 ///
1210 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1211 /// true => b"\x3F\x99\x99\x9A hello",
1212 /// false => b"\x9A\x99\x99\x3F hello",
1213 /// };
1214 /// assert_eq!(1.2f32, buf.get_f32_ne());
1215 /// ```
1216 ///
1217 /// # Panics
1218 ///
1219 /// This function panics if there is not enough remaining data in `self`.
1220 fn get_f32_ne(&mut self) -> f32 {
1221 f32::from_bits(Self::get_u32_ne(self))
1222 }
1223
1224 fn peek_f32_ne(&mut self) -> f32 {
1225 f32::from_bits(Self::peek_u32_ne(self))
1226 }
1227
1228 fn try_get_f32_ne(&mut self) -> io::Result<f32> {
1229 try_advance!(self.remaining() >= 4);
1230 Ok(self.get_f32_ne())
1231 }
1232 /// Gets an IEEE754 double-precision (8 bytes) floating point number from
1233 /// `self` in big-endian byte order.
1234 ///
1235 /// The current position is advanced by 8.
1236 ///
1237 /// # Examples
1238 ///
1239 /// ```
1240 /// use algorithm::buf::Bt;
1241 ///
1242 /// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..];
1243 /// assert_eq!(1.2f64, buf.get_f64());
1244 /// ```
1245 ///
1246 /// # Panics
1247 ///
1248 /// This function panics if there is not enough remaining data in `self`.
1249 fn get_f64(&mut self) -> f64 {
1250 f64::from_bits(Self::get_u64(self))
1251 }
1252
1253 fn peek_f64(&mut self) -> f64 {
1254 f64::from_bits(Self::peek_u64(self))
1255 }
1256
1257 fn try_get_f64(&mut self) -> io::Result<f64> {
1258 try_advance!(self.remaining() >= 8);
1259 Ok(self.get_f64())
1260 }
1261 /// Gets an IEEE754 double-precision (8 bytes) floating point number from
1262 /// `self` in little-endian byte order.
1263 ///
1264 /// The current position is advanced by 8.
1265 ///
1266 /// # Examples
1267 ///
1268 /// ```
1269 /// use algorithm::buf::Bt;
1270 ///
1271 /// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..];
1272 /// assert_eq!(1.2f64, buf.get_f64_le());
1273 /// ```
1274 ///
1275 /// # Panics
1276 ///
1277 /// This function panics if there is not enough remaining data in `self`.
1278 fn get_f64_le(&mut self) -> f64 {
1279 f64::from_bits(Self::get_u64_le(self))
1280 }
1281
1282 fn peek_f64_le(&mut self) -> f64 {
1283 f64::from_bits(Self::peek_u64_le(self))
1284 }
1285
1286 fn try_get_f64_le(&mut self) -> io::Result<f64> {
1287 try_advance!(self.remaining() >= 8);
1288 Ok(self.get_f64_le())
1289 }
1290 /// Gets an IEEE754 double-precision (8 bytes) floating point number from
1291 /// `self` in native-endian byte order.
1292 ///
1293 /// The current position is advanced by 8.
1294 ///
1295 /// # Examples
1296 ///
1297 /// ```
1298 /// use algorithm::buf::Bt;
1299 ///
1300 /// let mut buf: &[u8] = match cfg!(target_endian = "big") {
1301 /// true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello",
1302 /// false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello",
1303 /// };
1304 /// assert_eq!(1.2f64, buf.get_f64_ne());
1305 /// ```
1306 ///
1307 /// # Panics
1308 ///
1309 /// This function panics if there is not enough remaining data in `self`.
1310 fn get_f64_ne(&mut self) -> f64 {
1311 f64::from_bits(Self::get_u64_ne(self))
1312 }
1313
1314 fn peek_f64_ne(&mut self) -> f64 {
1315 f64::from_bits(Self::peek_u64_ne(self))
1316 }
1317
1318 fn try_get_f64_ne(&mut self) -> io::Result<f64> {
1319 try_advance!(self.remaining() >= 8);
1320 Ok(self.get_f64_ne())
1321 }
1322}
1323
1324impl Bt for &[u8] {
1325 #[inline]
1326 fn remaining(&self) -> usize {
1327 self.len()
1328 }
1329
1330 #[inline]
1331 fn chunk(&self) -> &[u8] {
1332 self
1333 }
1334
1335 fn advance_chunk(&mut self, n: usize) -> &[u8] {
1336 let ret = &self[..n];
1337 *self = &self[n..];
1338 ret
1339 }
1340
1341 #[inline]
1342 fn advance(&mut self, cnt: usize) {
1343 if self.len() < cnt {
1344 panic_advance(cnt, self.len());
1345 }
1346
1347 *self = &self[cnt..];
1348 }
1349
1350 fn into_binary(self) -> Binary {
1351 Binary::from(self.to_vec())
1352 }
1353}
1354
1355impl<T: AsRef<[u8]>> Bt for std::io::Cursor<T> {
1356 #[inline]
1357 fn remaining(&self) -> usize {
1358 self.get_ref().as_ref().len() - self.position() as usize
1359 }
1360
1361 #[inline]
1362 fn chunk(&self) -> &[u8] {
1363 &self.get_ref().as_ref()[(self.position() as usize)..]
1364 }
1365
1366 fn advance_chunk(&mut self, n: usize) -> &[u8] {
1367 let position = self.position() as usize;
1368 self.set_position(self.position() + n as u64);
1369 let ret = &self.get_ref().as_ref()[position..(position + n)];
1370 ret
1371 }
1372
1373 #[inline]
1374 fn advance(&mut self, cnt: usize) {
1375 if self.remaining() < cnt {
1376 panic_advance(cnt, self.remaining());
1377 }
1378 self.set_position(self.position() + cnt as u64);
1379 }
1380
1381 fn into_binary(self) -> Binary {
1382 Binary::from(self.get_ref().as_ref()[(self.position() as usize)..].to_vec())
1383 }
1384}
1385
1386
1387impl<T: Bt> Bt for &mut T {
1388 fn remaining(&self) -> usize {
1389 T::remaining(self)
1390 }
1391
1392 fn chunk(&self) -> &[u8] {
1393 T::chunk(self)
1394 }
1395
1396 fn advance(&mut self, n: usize) {
1397 T::advance(self, n)
1398 }
1399
1400 fn advance_chunk(&mut self, n: usize) -> &[u8] {
1401 T::advance_chunk(self, n)
1402 }
1403
1404 fn into_binary(self) -> Binary {
1405 panic!("mut ref must not into")
1406 }
1407}