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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
use core::nonzero::NonZero; use std::fmt::Debug; use std::sync::Arc; use raw::{Allocator, RawIobuf}; use impls::{AROIobuf, RWIobuf, UniqueIobuf}; use intlike::IntLike; /// Input/Output Buffer /// /// Have your functions take a generic IObuf when they don't modify the buffer /// contents. This allows them to be used with both `ROIobuf`s and `RWIobuf`s. /// /// `peek` accesses a value at a position relative to the start of the /// window without advancing, and is meant to be used with `try!`. Its dual, /// `poke`, is only implemented for `RWIobuf`, since it needs to write into the /// buffer. /// /// `consume` accesses a value at the beginning of the window, and advances the /// window to no longer cover include it. Its dual, `fill`, is only implemented /// for `RWIobuf`, since it needs to write into the buffer. /// /// A suffix `_be` means the data will be read big-endian. A suffix `_le` means /// the data will be read little-endian. /// /// The `unsafe_` prefix means the function omits bounds checks. Misuse can /// easily cause security issues. Be careful! pub trait Iobuf: Clone + Debug { /// Copies the data byte-by-byte in the Iobuf into a new, writeable Iobuf. /// The new Iobuf and the old Iobuf will not share storage. /// /// ```rust /// use iobuf::{RWIobuf,Iobuf}; /// /// let mut s = [ 1, 2 ]; /// /// let mut b = RWIobuf::from_slice(&mut s); /// assert_eq!(b.sub_from(1), Ok(())); /// let mut c = b.deep_clone(); /// /// assert_eq!(b.poke_be(0, 0u8), Ok(())); /// assert_eq!(c.peek_be::<u8>(0), Ok(2u8)); /// ``` fn deep_clone(&self) -> RWIobuf<'static>; /// Copies the data byte-by-byte in the Iobuf into a new, writable Iobuf. /// The new Iobuf will have storage allocated out of `allocator`, and will not /// share the buffer with the original Iobuf. fn deep_clone_with_allocator(&self, allocator: Arc<Box<Allocator>>) -> RWIobuf<'static>; /// Returns `Ok` if the Iobuf is the last to reference the underlying data, /// and converts it to a `UniqueIobuf` for sending to another task. This can /// also be used to safely convert from a `ROIobuf` to a `RWIobuf`, and to /// downgrade atomically refcounted Iobufs to non-atomically refcounted ones. /// /// Only Iobufs which were originally allocated on the heap (for example, with /// a `_copy` constructor or `RWIobuf::new`) may be converted to a /// `UniqueIobuf`. /// /// Returns `Err` if the buffer is not the last to reference the underlying /// data. If this case is hit, the buffer passed by value is returned by value. /// /// ```rust /// use iobuf::{RWIobuf,Iobuf}; /// /// let b = RWIobuf::from_str_copy("hello, world"); /// assert!(b.unique().is_ok()); /// /// let b = RWIobuf::from_str_copy("hi"); /// let c = b.clone(); /// assert!(b.unique().is_err()); /// let d = c.clone(); /// assert!(d.unique().is_err()); /// assert!(c.unique().is_ok()); /// ``` fn unique(self) -> Result<UniqueIobuf, Self>; /// Returns `Ok` if the Iobuf is the last to reference the underlying data, /// and upgrades it to an `AROIobuf` which can be sent over channels and /// `Arc`ed with impunity. This is extremely useful in situations where Iobufs /// are created and written in one thread, and consumed in another. /// /// Returns `Err` if the buffer is not the last to reference the underlying /// data. If this case is hit, the buffer passed by value is returned by value. /// /// ```rust /// use iobuf::{RWIobuf,Iobuf}; /// /// let b = RWIobuf::from_str_copy("hello, world"); /// assert!(b.atomic_read_only().is_ok()); /// /// let b = RWIobuf::from_str_copy("hi"); /// let c = b.clone(); /// assert!(b.atomic_read_only().is_err()); /// let d = c.clone(); /// assert!(d.atomic_read_only().is_err()); /// assert!(c.atomic_read_only().is_ok()); /// ``` fn atomic_read_only(self) -> Result<AROIobuf, Self>; /// Returns the size of the window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("Hello"); /// /// assert_eq!(b.len(), 5); /// assert_eq!(b.advance(2), Ok(())); /// assert_eq!(b.len(), 3); /// ``` fn len(&self) -> u32; /// Returns the size of the limits. The capacity of an iobuf can be reduced /// via `narrow`. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("Hello"); /// /// assert_eq!(b.cap(), 5); /// assert_eq!(b.advance(2), Ok(())); /// assert_eq!(b.cap(), 5); /// /// b.narrow(); /// assert_eq!(b.cap(), 3); /// ``` fn cap(&self) -> u32; /// `true` if `len() == 0`. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// assert!(ROIobuf::from_str("").is_empty()); /// assert!(!ROIobuf::from_str("a").is_empty()); /// ``` fn is_empty(&self) -> bool; /// Reads the data in the window as an immutable slice. Note that `Peek`s /// and `Poke`s into the iobuf will change the contents of the slice, even /// though it advertises itself as immutable. Therefore, this function is /// `unsafe`. /// /// To use this function safely, you must manually ensure that the slice never /// interacts with the same Iobuf. If you take a slice of an Iobuf, you can /// immediately poke it into itself. This is unsafe, and undefined. However, /// it can be safely poked into a _different_ iobuf without issue. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// unsafe { /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.as_window_slice(), b"hello"); /// assert_eq!(b.advance(2), Ok(())); /// assert_eq!(b.as_window_slice(), b"llo"); /// } /// ``` unsafe fn as_window_slice<'b>(&'b self) -> &'b [u8]; /// Reads the data in the limits as an immutable slice. Note that `Peek`s /// and `Poke`s into the iobuf will change the contents of the slice, even /// though it advertises itself as immutable. Therefore, this function is /// `unsafe`. /// /// To use this function safely, you must manually ensure that the slice never /// interacts with the same Iobuf. If you take a slice of an Iobuf, you can /// immediately poke it into itself. This is unsafe, and undefined. However, /// it can be safely poked into a _different_ iobuf without issue. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// unsafe { /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.as_limit_slice(), b"hello"); /// assert_eq!(b.advance(2), Ok(())); /// assert_eq!(b.as_limit_slice(), b"hello"); /// b.narrow(); /// assert_eq!(b.as_limit_slice(), b"llo"); /// } /// ``` unsafe fn as_limit_slice<'b>(&'b self) -> &'b [u8]; /// Changes the Iobuf's bounds to the subrange specified by `pos` and `len`, /// which must lie within the current window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.sub_window(1, 3), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"ell") }; /// // The limits are unchanged. If you just want them to match the bounds, use /// // `sub` and friends. /// b.reset(); /// unsafe { assert_eq!(b.as_window_slice(), b"hello") }; /// ``` /// /// If your position and length do not lie in the current window, you will get /// an error. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.advance(2), Ok(())); /// assert_eq!(b.sub_window(0, 5), Err(())); // boom /// ``` /// /// If you want to slice from the start, use `sub_to`: /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.sub_window_to(3), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"hel") }; /// ``` /// /// If you want to slice to the end, use `sub_from`: /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.sub_window_from(2), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"llo") }; /// ``` fn sub_window(&mut self, pos: u32, len: u32) -> Result<(), ()>; /// Changes the Iobuf's bounds to start at `pos`, and go to the end of the /// current window. fn sub_window_from(&mut self, pos: u32) -> Result<(), ()>; /// Changes the Iobuf's bounds to extend for only `len` bytes. /// /// This is the same as `resize`, but might make more semantic sense at the /// call site depending on context. fn sub_window_to(&mut self, len: u32) -> Result<(), ()>; /// The same as `sub_window`, but no bounds checks are performed. You should /// probably just use `sub_window`. unsafe fn unsafe_sub_window(&mut self, pos: u32, len: u32); /// The same as `sub_window_from`, but no bounds checks are performed. You /// should probably just use `sub_window_from`. unsafe fn unsafe_sub_window_from(&mut self, pos: u32); /// The same as `sub_window_to`, but no bounds checks are performed. You /// should probably just use `sub_window_to`. unsafe fn unsafe_sub_window_to(&mut self, pos: u32); /// Changes the Iobuf's limits and bounds to the subrange specified by /// `pos` and `len`, which must lie within the current window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.sub(1, 3), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"ell") }; /// /// // The limits are changed, too! If you just want to change the bounds, use /// // `sub_window` and friends. /// b.reset(); /// unsafe { assert_eq!(b.as_window_slice(), b"ell") }; /// ``` /// /// If your position and length do not lie in the current window, you will get /// an error. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.advance(2), Ok(())); /// assert_eq!(b.sub(0, 5), Err(())); // boom /// ``` /// /// If you want to slice from the start, use `sub_to`: /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.sub_to(3), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"hel") }; /// ``` /// /// If you want to slice to the end, use `sub_from`: /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.sub_from(2), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"llo") }; /// ``` fn sub(&mut self, pos: u32, len: u32) -> Result<(), ()>; /// Changes the Iobuf's limits and bounds to start from `pos` and extend to /// the end of the current window. fn sub_from(&mut self, pos: u32) -> Result<(), ()>; /// Changes the Iobuf's limits and bounds to start at the beginning of the /// current window, and extend for `len` bytes. fn sub_to(&mut self, len: u32) -> Result<(), ()>; /// The same as `sub`, but no bounds checks are performed. You should probably /// just use `sub`. unsafe fn unsafe_sub(&mut self, pos: u32, len: u32); /// The same as `sub_from`, but no bounds checks are performed. You should /// probably just use `sub_from`. unsafe fn unsafe_sub_from(&mut self, pos: u32); /// The same as `sub_to`, but no bounds checks are performed. You should /// probably just use `sub_to`. unsafe fn unsafe_sub_to(&mut self, len: u32); /// Overrides the existing limits and window of the Iobuf, returning `Err(())` /// if attempting to widen either of them. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.set_limits_and_window((1, 3), (2, 3)), Ok(())); /// assert_eq!(b.cap(), 2); /// assert_eq!(b.len(), 1); /// // trying to shrink the limits... /// assert_eq!(b.set_limits_and_window((1, 4), (2, 2)), Err(())); /// // trying to shrink the window... /// assert_eq!(b.set_limits_and_window((1, 3), (2, 4)), Err(())); /// ``` fn set_limits_and_window(&mut self, limits: (u32, u32), window: (u32, u32)) -> Result<(), ()>; /// Sets the limits to the current window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.advance(2), Ok(())); /// b.narrow(); /// assert_eq!(b.cap(), 3); /// ``` fn narrow(&mut self); /// Advances the lower bound of the window by `len`. `Err(())` will be /// returned if you advance past the upper bound of the window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.advance(3), Ok(())); /// assert_eq!(b.advance(3), Err(())); /// unsafe { assert_eq!(b.as_window_slice(), b"lo"); } /// ``` fn advance(&mut self, len: u32) -> Result<(), ()>; /// Advances the lower bound of the window by `len`. No bounds checking will /// be performed. /// /// A common pattern with `unsafe_advance` is to consolidate multiple bounds /// checks into one. In this example, O(n) bounds checks are consolidated into /// O(1) bounds checks: /// /// ```rust /// use std::mem; /// use std::result::Result::{self,Ok}; /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [2, 0x12, 0x34, 0x56, 0x78]; /// let mut b = ROIobuf::from_slice(&data); /// /// fn parse<B: Iobuf>(b: &mut B) -> Result<u16, ()> { /// let num_shorts: u8 = try!(b.consume_be()); /// let short_size = mem::size_of::<u16>() as u32; /// let num_bytes = num_shorts as u32 * short_size; /// /// unsafe { /// try!(b.check_range(0, num_bytes)); /// /// let mut sum = 0u16; /// /// for i in (0 .. num_shorts as u32).map(|x| x * short_size) { /// sum += b.unsafe_peek_be::<u16>(i); /// } /// /// b.unsafe_advance(num_bytes); /// /// Ok(sum) /// } /// } /// /// assert_eq!(parse(&mut b), Ok(0x1234 + 0x5678)); /// assert_eq!(b.len(), 0); /// ``` /// /// Alternatively, you could use `unsafe_consume` in a similar, arguably /// clearer way: /// /// ```rust /// use std::mem; /// use std::result::Result::{self,Ok}; /// use iobuf::{ROIobuf,Iobuf}; /// let data = [2, 0x12, 0x34, 0x56, 0x78]; /// let mut b = ROIobuf::from_slice(&data); /// /// fn parse<B: Iobuf>(b: &mut B) -> Result<u16, ()> { /// let num_shorts: u8 = try!(b.consume_be()); /// let short_size = mem::size_of::<u16>() as u32; /// let num_bytes = num_shorts as u32 * short_size; /// /// unsafe { /// try!(b.check_range(0, num_bytes)); /// /// let mut sum = 0u16; /// /// for _ in 0..num_shorts { /// sum += b.unsafe_consume_be::<u16>(); /// } /// /// Ok(sum) /// } /// } /// /// assert_eq!(parse(&mut b), Ok(0x1234 + 0x5678)); /// assert_eq!(b.len(), 0); /// ``` unsafe fn unsafe_advance(&mut self, len: u32); /// Advances the upper bound of the window by `len`. `Err(())` will be /// returned if you advance past the upper limit. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.resize(2), Ok(())); /// assert_eq!(b.extend(1), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"hel"); } /// assert_eq!(b.extend(3), Err(())); /// unsafe { assert_eq!(b.as_window_slice(), b"hel"); } /// ``` fn extend(&mut self, len: u32) -> Result<(), ()>; /// Advances the upper bound of the window by `len`. No bounds checking will /// be performed. unsafe fn unsafe_extend(&mut self, len: u32); /// Returns `true` if the `other` Iobuf's window is the region directly after /// our window. This does not inspect the buffer -- it only compares raw /// pointers. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut a = ROIobuf::from_str_copy("hello"); /// let mut b = a.clone(); /// let mut c = a.clone(); /// let mut d = ROIobuf::from_str_copy("hello"); /// /// assert_eq!(a.sub_window_to(2), Ok(())); /// /// // b actually IS an extension of a. /// assert_eq!(b.sub_window_from(2), Ok(())); /// assert_eq!(a.is_extended_by(&b), true); /// /// // a == "he", b == "lo", it's missing the "l", therefore not an extension. /// assert_eq!(c.sub_window_from(3), Ok(())); /// assert_eq!(b.is_extended_by(&a), false); /// /// // Different allocations => not an extension. /// assert_eq!(d.sub_window_from(2), Ok(())); /// assert_eq!(a.is_extended_by(&d), false); /// ``` fn is_extended_by<Buf: Iobuf>(&self, other: &Buf) -> bool; /// Attempts to extend an Iobuf with the contents of another Iobuf. If this /// Iobuf's window is not the region directly before the other Iobuf's window, /// no extension will be performed and `Err(())` will be returned. If the /// operation was successful, `Ok(())` will be returned. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut a = ROIobuf::from_str_copy("hello"); /// let mut b = a.clone(); /// let mut c = a.clone(); /// let mut d = ROIobuf::from_str_copy("hello"); /// /// assert_eq!(a.sub_window_to(2), Ok(())); /// /// // Different allocations => not an extension. /// assert_eq!(d.sub_window_from(2), Ok(())); /// assert_eq!(a.extend_with(&d), Err(())); /// /// // b actually IS an extension of a. /// assert_eq!(b.sub_window_from(2), Ok(())); /// assert_eq!(a.extend_with(&b), Ok(())); /// unsafe { /// assert_eq!(a.as_window_slice(), b"hello"); /// } /// /// assert_eq!(a.sub_window_to(2), Ok(())); /// /// // a == "he", b == "lo", it's missing the "l", therefore not an extension. /// assert_eq!(c.sub_window_from(3), Ok(())); /// assert_eq!(b.extend_with(&a), Err(())); /// ``` fn extend_with<Buf: Iobuf>(&mut self, other: &Buf) -> Result<(), ()>; /// Sets the length of the window, provided it does not exceed the limits. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.resize(3), Ok(())); /// assert_eq!(b.peek_be(2), Ok(b'l')); /// assert_eq!(unsafe { b.as_window_slice() }, b"hel"); /// assert_eq!(b.peek_be::<u8>(3), Err(())); /// assert_eq!(b.advance(1), Ok(())); /// assert_eq!(b.resize(5), Err(())); /// ``` fn resize(&mut self, len: u32) -> Result<(), ()>; /// Sets the length of the window. No bounds checking will be performed. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// /// unsafe { /// assert_eq!(b.check_range(1, 3), Ok(())); /// assert_eq!(b.peek_be(1), Ok(b'e')); /// assert_eq!(b.peek_be(2), Ok(b'l')); /// assert_eq!(b.peek_be(3), Ok(b'l')); /// b.unsafe_resize(4); // safe, since we already checked it. /// } /// ``` unsafe fn unsafe_resize(&mut self, len: u32); /// Splits an Iobuf around an index. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("helloworld"); /// /// match b.split_at(5) { /// Err(()) => panic!("This won't happen."), /// Ok((c, d)) => unsafe { /// assert_eq!(c.as_window_slice(), b"hello"); /// assert_eq!(d.as_window_slice(), b"world"); /// } /// } /// /// match b.split_at(0) { /// Err(()) => panic!("This won't happen, either."), /// Ok((c, d)) => unsafe { /// assert_eq!(c.as_window_slice(), b""); /// assert_eq!(d.as_window_slice(), b"helloworld"); /// } /// } /// /// match b.split_at(10000) { /// Ok(_) => panic!("This won't happen!"), /// Err(()) => unsafe { assert_eq!(b.as_window_slice(), b"helloworld"); }, /// } /// ``` fn split_at(&self, pos: u32) -> Result<(Self, Self), ()>; /// Like `split_at`, but does not perform bounds checking. unsafe fn unsafe_split_at(&self, pos: u32) -> (Self, Self); /// Splits out the start of an Iobuf at an index. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("helloworld"); /// /// match b.split_start_at(5) { /// Err(()) => panic!("This won't happen."), /// Ok(c) => unsafe { /// assert_eq!(b.as_window_slice(), b"world"); /// assert_eq!(c.as_window_slice(), b"hello"); /// } /// } /// /// match b.split_start_at(0) { /// Err(()) => panic!("This won't happen, either."), /// Ok(c) => unsafe { /// assert_eq!(b.as_window_slice(), b"world"); /// assert_eq!(c.as_window_slice(), b""); /// } /// } /// /// match b.split_start_at(10000) { /// Ok(_) => panic!("This won't happen!"), /// Err(()) => unsafe { assert_eq!(b.as_window_slice(), b"world"); }, /// } /// ``` fn split_start_at(&mut self, pos: u32) -> Result<Self, ()>; /// Like `split_start_at`, but does not perform bounds checking. unsafe fn unsafe_split_start_at(&mut self, pos: u32) -> Self; /// Sets the lower bound of the window to the lower limit. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// /// assert_eq!(b.advance(2), Ok(())); /// assert_eq!(b.resize(2), Ok(())); /// /// unsafe { assert_eq!(b.as_window_slice(), b"ll"); } /// /// b.rewind(); /// /// unsafe { assert_eq!(b.as_window_slice(), b"hell"); } /// ``` fn rewind(&mut self); /// Sets the window to the limits. /// /// "Take it to the limit..." /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// assert_eq!(b.resize(3), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"hel"); } /// assert_eq!(b.advance(2), Ok(())); /// unsafe { assert_eq!(b.as_window_slice(), b"l"); } /// b.reset(); /// unsafe { assert_eq!(b.as_window_slice(), b"hello"); } /// ``` fn reset(&mut self); /// Sets the window to range from the lower limit to the lower bound of the /// old window. This is typically called after a series of `Fill`s, to /// reposition the window in preparation to `Consume` the newly written data. /// /// If the area of the limits is denoted with `[ ]` and the area of the window /// is denoted with `x`, then the `flip_lo` looks like: /// /// `Before: [ xxxx ]` /// /// `After: [xxxxxxx ]` /// /// ```rust /// use iobuf::{RWIobuf,Iobuf}; /// /// let mut b = RWIobuf::new(4); /// /// assert_eq!(b.fill_be(1u8), Ok(())); /// assert_eq!(b.fill_be(2u8), Ok(())); /// assert_eq!(b.fill_be(3u8), Ok(())); /// assert_eq!(b.len(), 1); /// /// b.flip_lo(); /// /// assert_eq!(b.consume_be(), Ok(0x0102u16)); /// assert_eq!(b.consume_be(), Ok(0x03u8)); /// assert!(b.is_empty()); /// ``` fn flip_lo(&mut self); /// Sets the window to range from the upper bound of the old window to the /// upper limit. This is a dual to `flip_lo`, and is typically called when the /// data in the current (narrowed) window has been processed and the window /// needs to be positioned over the remaining data in the buffer. /// /// If the area of the limits is denoted with `[ ]` and the area of the window /// is denoted with `x`, then the `flip_lo` looks like: /// /// `Before: [ xxxx ]` /// /// `After: [ xx]` /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// /// assert_eq!(b.resize(3), Ok(())); /// /// assert_eq!(b.consume_be(), Ok(b'h')); /// assert_eq!(b.consume_be(), Ok(b'e')); /// assert_eq!(b.consume_be(), Ok(b'l')); /// assert!(b.is_empty()); /// /// b.flip_hi(); /// /// assert!(!b.is_empty()); /// assert_eq!(b.consume_be(), Ok(b'l')); /// assert_eq!(b.consume_be(), Ok(b'o')); /// assert!(b.is_empty()); /// ``` fn flip_hi(&mut self); /// Returns the number of bytes between the lower limit and the lower bound. /// /// ```rust /// use iobuf::{RWIobuf,Iobuf}; /// /// let mut b = RWIobuf::new(100); /// /// assert_eq!(b.advance(20), Ok(())); /// assert_eq!(b.lo_space(), 20); /// b.flip_lo(); /// assert_eq!(b.lo_space(), 0); /// ``` fn lo_space(&self) -> u32; /// Returns the number of bytes between the upper bound and the upper limit. /// /// ```rust /// use iobuf::{RWIobuf,Iobuf}; /// /// let mut b = RWIobuf::new(100); /// /// assert_eq!(b.resize(20), Ok(())); /// assert_eq!(b.hi_space(), 80); /// b.flip_hi(); /// assert_eq!(b.hi_space(), 0); /// ``` fn hi_space(&self) -> u32; /// Reads the bytes at a given offset from the beginning of the window, into /// the supplied buffer. Either the entire buffer is filled, or an error is /// returned because bytes outside of the window were requested. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// /// let mut b = ROIobuf::from_slice(&data); /// let mut tgt4 = [ 0x00, 0x00, 0x00, 0x00 ]; /// let mut tgt3 = [ 0x00, 0x00, 0x00 ]; /// /// assert_eq!(b.peek(0, &mut tgt4), Ok(())); /// assert_eq!(tgt4.iter().map(|&x| x).fold(0,|a, b| a + b), 10); /// assert_eq!(b.peek(1, &mut tgt3), Ok(())); /// assert_eq!(tgt3.iter().map(|&x| x).fold(0, |a, b| a + b), 9); /// assert_eq!(b.peek(1, &mut tgt4), Err(())); /// ``` fn peek(&self, pos: u32, dst: &mut [u8]) -> Result<(), ()>; /// Reads a big-endian primitive at a given offset from the beginning of the /// window. /// /// An error is returned if bytes outside of the window were requested. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// let mut b = ROIobuf::from_slice(&data); /// /// assert_eq!(b.advance(1), Ok(())); /// /// assert_eq!(b.peek_be(0), Ok(0x0203u16)); /// assert_eq!(b.peek_be(1), Ok(0x0304u16)); /// assert_eq!(b.peek_be::<u16>(2), Err(())); /// ``` fn peek_be<T: IntLike>(&self, pos: u32) -> Result<T, ()>; /// Reads a little-endian primitive at a given offset from the beginning of /// the window. /// /// An error is returned if bytes outside of the window were requested. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// let mut b = ROIobuf::from_slice(&data); /// /// assert_eq!(b.advance(1), Ok(())); /// /// assert_eq!(b.peek_le(0), Ok(0x0302u16)); /// assert_eq!(b.peek_le(1), Ok(0x0403u16)); /// assert_eq!(b.peek_le::<u16>(2), Err(())); /// ``` fn peek_le<T: IntLike>(&self, pos: u32) -> Result<T, ()>; /// Reads bytes, starting from the front of the window, into the supplied /// buffer. Either the entire buffer is filled, or an error is returned /// because bytes outside the window were requested. /// /// After the bytes have been read, the window will be moved to no longer /// include then. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// /// let mut b = ROIobuf::from_slice(&data); /// let mut tgt3 = [ 0x00, 0x00, 0x00 ]; /// let mut tgt1 = [ 0x00 ]; /// /// assert_eq!(b.consume(&mut tgt3), Ok(())); /// assert_eq!(tgt3.iter().map(|&x| x).fold(0, |a, b| a + b), 6); /// assert_eq!(b.consume(&mut tgt3), Err(())); /// assert_eq!(b.consume(&mut tgt1), Ok(())); /// assert_eq!(tgt1[0], 4); /// ``` fn consume(&mut self, dst: &mut [u8]) -> Result<(), ()>; /// Reads a big-endian primitive from the beginning of the window. /// /// After the primitive has been read, the window will be moved such that it /// is no longer included. /// /// An error is returned if bytes outside of the window were requested. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// let mut b = ROIobuf::from_slice(&data); /// /// assert_eq!(b.advance(1), Ok(())); /// /// assert_eq!(b.consume_be(), Ok(0x0203u16)); /// assert_eq!(b.consume_be::<u16>(), Err(())); /// assert_eq!(b.consume_be(), Ok(0x04u8)); /// ``` fn consume_be<T: IntLike>(&mut self) -> Result<T, ()>; /// Reads a little-endian primitive from the beginning of the window. /// /// After the primitive has been read, the window will be moved such that it /// is no longer included. /// /// An error is returned if bytes outside of the window were requested. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// let mut b = ROIobuf::from_slice(&data); /// /// assert_eq!(b.advance(1), Ok(())); /// /// assert_eq!(b.consume_le(), Ok(0x0302u16)); /// assert_eq!(b.consume_le::<u16>(), Err(())); /// assert_eq!(b.consume_le(), Ok(0x04u8)); /// ``` fn consume_le<T: IntLike>(&mut self) -> Result<T, ()>; /// Returns an `Err(())` if the `len` bytes, starting at `pos`, are not all /// in the window. To be used with the `try!` macro. /// /// Make sure you use this in conjunction with the `unsafe` combinators. It /// is recommended you minimize your bounds checks by doing it once with /// `check_range`, followed by unsafe accesses. If you do unsafe accesses /// without a `check_range`, there will likely be reliability and security /// issues with your application. /// /// Below is a correct usage of `check_range` to minimize bounds checks: /// /// ```rust /// use std::result::Result::{self,Ok}; /// use iobuf::{ROIobuf,Iobuf}; /// /// // [ number of byte buffers, size of first byte buffer, ...bytes, etc. ] /// let data = [ 0x02, 0x02, 0x55, 0x66, 0x03, 0x11, 0x22, 0x33 ]; /// let mut b = ROIobuf::from_slice(&data); /// /// fn parse<B: Iobuf>(b: &mut B) -> Result<usize, ()> { /// let mut sum = 0; /// /// let num_buffers: u8 = try!(b.consume_be()); /// /// for _ in 0..num_buffers { /// let len: u8 = try!(b.consume_be()); /// /// unsafe { /// try!(b.check_range(0, len as u32)); /// /// for _ in 0..len { /// sum += b.unsafe_consume_be::<u8>() as usize; /// } /// } /// } /// /// Ok(sum) /// } /// /// assert_eq!(parse(&mut b), Ok(0x55 + 0x66 + 0x11 + 0x22 + 0x33)); /// ``` fn check_range(&self, pos: u32, len: u32) -> Result<(), ()>; /// The same as `check_range`, but with a `usize` length. If you're checking /// the range of something which might overflow an `i32`, use this version /// instead of `check_range`. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// /// assert_eq!(b.check_range_usize(1u32, 5usize), Err(())); /// ``` fn check_range_usize(&self, pos: u32, len: usize) -> Result<(), ()>; /// The same as `check_range`, but fails if the bounds check returns `Err(())`. /// /// ```should_fail /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// /// b.check_range_fail(1, 5); // boom. /// ``` fn check_range_fail(&self, pos: u32, len: u32); /// The same as `check_range_usize`, but fails if the bounds check returns /// `Err(())`. /// /// ```should_fail /// use iobuf::{ROIobuf,Iobuf}; /// /// let mut b = ROIobuf::from_str("hello"); /// /// b.check_range_usize_fail(1u32, 5u); // boom. /// ``` fn check_range_usize_fail(&self, pos: u32, len: usize); /// Reads the bytes at a given offset from the beginning of the window, into /// the supplied buffer. It is undefined behavior to read outside the iobuf /// window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [1,2,3,4,5,6]; /// let mut b = ROIobuf::from_slice(&data); /// /// let mut dst = [0; 4]; /// /// unsafe { /// // one range check, instead of two! /// assert_eq!(b.check_range(0, 5), Ok(())); /// /// assert_eq!(b.unsafe_peek_be::<u8>(0), 1u8); /// b.unsafe_peek(1, &mut dst); /// } /// /// let expected = [ 2u8, 3, 4, 5 ]; /// assert_eq!(dst, expected); /// ``` unsafe fn unsafe_peek(&self, pos: u32, dst: &mut [u8]); /// Reads a big-endian primitive at a given offset from the beginning of the /// window. It is undefined behavior to read outside the iobuf window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [1,2,3,4,5,6]; /// let mut b = ROIobuf::from_slice(&data); /// /// unsafe { /// assert_eq!(b.check_range(0, 6), Ok(())); /// let x: u16 = b.unsafe_peek_be(0); /// let y: u32 = b.unsafe_peek_be(2); /// b.unsafe_advance(6); /// /// let z: u32 = x as u32 + y; /// assert_eq!(z, 0x0102 + 0x03040506); /// } /// ``` unsafe fn unsafe_peek_be<T: IntLike>(&self, pos: u32) -> T; /// Reads a little-endian primitive at a given offset from the beginning of /// the window. It is undefined behavior to read outside the iobuf window. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [1,2,3,4,5,6]; /// let mut b = ROIobuf::from_slice(&data); /// /// unsafe { /// assert_eq!(b.check_range(0, 6), Ok(())); /// let x: u16 = b.unsafe_peek_le(0); /// let y: u32 = b.unsafe_peek_le(2); /// b.unsafe_advance(6); /// /// let z: u32 = x as u32 + y; /// assert_eq!(z, 0x0201 + 0x06050403); /// } /// ``` unsafe fn unsafe_peek_le<T: IntLike>(&self, pos: u32) -> T; /// Reads bytes, starting from the front of the window, into the supplied /// buffer. After the bytes have been read, the window will be moved to no /// longer include then. /// /// It is undefined behavior to request bytes outside the iobuf window. unsafe fn unsafe_consume(&mut self, dst: &mut [u8]); /// Reads a big-endian primitive at the beginning of the window. /// /// After the primitive has been read, the window will be moved such that it /// is no longer included. /// /// It is undefined behavior if bytes outside the window are requested. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// let mut b = ROIobuf::from_slice(&data); /// /// assert_eq!(b.advance(1), Ok(())); /// /// unsafe { /// assert_eq!(b.check_range(0, 3), Ok(())); /// assert_eq!(b.unsafe_consume_be::<u16>(), 0x0203u16); /// assert_eq!(b.unsafe_consume_be::<u8>(), 0x04u8); /// } /// ``` unsafe fn unsafe_consume_be<T: IntLike>(&mut self) -> T; /// Reads a little-endian primitive at the beginning of the window. /// /// After the primitive has been read, the window will be moved such that it /// is no longer included. /// /// It is undefined behavior if bytes outside of the window are requested. /// /// ```rust /// use iobuf::{ROIobuf,Iobuf}; /// /// let data = [ 0x01, 0x02, 0x03, 0x04 ]; /// let mut b = ROIobuf::from_slice(&data); /// /// assert_eq!(b.advance(1), Ok(())); /// /// unsafe { /// assert_eq!(b.check_range(0, 3), Ok(())); /// assert_eq!(b.unsafe_consume_le::<u16>(), 0x0302u16); /// assert_eq!(b.unsafe_consume_le::<u8>(), 0x04u8); /// } /// ``` unsafe fn unsafe_consume_le<T: IntLike>(&mut self) -> T; /// Checks internal state of the iobuf, to ensure that internal invariants are /// satisified. Returns `Err(msg)` if any invariant isn't satisfied. fn invariant(&self) -> Result<(), Box<String>>; /// For internal use only. unsafe fn as_raw<'b>(&'b self) -> &'b RawIobuf<'b>; /// Gets a pointer to the start of the internal backing buffer. This is /// extremely low level, and it is not recommended you use this interface. fn ptr(&self) -> NonZero<*mut u8>; /// Returns `true` if the Iobuf points to owned memory (i.e. has to do a /// refcount modification on `clone` or `drop`) or borrowed memory. fn is_owned(&self) -> bool; /// Returns an index into the buffer returned by `ptr` that represents the /// inclusive lower bound of the limits. fn lo_min(&self) -> u32; /// Returns an index into the buffer returned by `ptr` that represents the /// inclusive lower bound of the window. fn lo(&self) -> u32; /// Returns an index into the buffer returned by `ptr` that represents the /// exclusive upper bound of the window. fn hi(&self) -> u32; /// Returns an index into the buffer returned by `ptr` that represents the /// exclusive upper bound of the limits. fn hi_max(&self) -> u32; }