From 6d7927fcf9f2566d106f4d23fa0d29bd830b9bb7 Mon Sep 17 00:00:00 2001 From: Yi Kong Date: Sun, 14 Feb 2021 22:04:08 +0800 Subject: Update to 1.4.2 Change-Id: I9b660b3276276d55040849a3cd6e5b70dbcd513b --- .cargo_vcs_info.json | 2 +- .github/workflows/ci.yml | 151 ++++ Android.bp | 10 +- CHANGELOG.md | 25 + Cargo.toml | 15 +- Cargo.toml.orig | 14 +- LICENSE | 1 + METADATA | 19 + MODULE_LICENSE_MIT | 0 OWNERS | 1 + README.md | 6 +- benches/bench.rs | 236 +++--- build.rs | 87 --- rustfmt.toml | 2 + src/io.rs | 92 +-- src/lib.rs | 1794 ++++++++++++++++++++++++++++++++-------------- 16 files changed, 1623 insertions(+), 832 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 120000 LICENSE create mode 100644 METADATA create mode 100644 MODULE_LICENSE_MIT create mode 100644 OWNERS delete mode 100644 build.rs create mode 100644 rustfmt.toml diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index e7e8b81..e260c24 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "058237661a88c2f610f280340310fce19d98d265" + "sha1": "ca8c10a3f85e2c66781f7d12e90196e6923592d7" } } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2c2c790 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,151 @@ +name: ci +on: + pull_request: + push: + branches: + - master + schedule: + - cron: '00 01 * * *' +jobs: + test: + name: test + env: + # For some builds, we use cross to test on 32-bit and big-endian + # systems. + CARGO: cargo + # When CARGO is set to CROSS, TARGET is set to `--target matrix.target`. + TARGET: + runs-on: ${{ matrix.os }} + strategy: + matrix: + build: + - pinned + - stable + - stable-32 + - stable-mips + - beta + - nightly + - macos + - win-msvc + - win-gnu + include: + - build: pinned + os: ubuntu-18.04 + rust: 1.41.1 + - build: stable + os: ubuntu-18.04 + rust: stable + - build: stable-32 + os: ubuntu-18.04 + rust: stable + target: i686-unknown-linux-gnu + - build: stable-mips + os: ubuntu-18.04 + rust: stable + target: mips64-unknown-linux-gnuabi64 + - build: beta + os: ubuntu-18.04 + rust: beta + - build: nightly + os: ubuntu-18.04 + rust: nightly + - build: macos + os: macos-latest + rust: stable + - build: win-msvc + os: windows-2019 + rust: stable + - build: win-gnu + os: windows-2019 + rust: stable-x86_64-gnu + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + profile: minimal + override: true + + - name: Use Cross + if: matrix.target != '' + run: | + # FIXME: to work around bugs in latest cross release, install master. + # See: https://github.com/rust-embedded/cross/issues/357 + cargo install --git https://github.com/rust-embedded/cross + echo "CARGO=cross" >> $GITHUB_ENV + echo "TARGET=--target ${{ matrix.target }}" >> $GITHUB_ENV + + - name: Show command used for Cargo + run: | + echo "cargo command is: ${{ env.CARGO }}" + echo "target flag is: ${{ env.TARGET }}" + + - name: Show CPU info for debugging + if: matrix.os == 'ubuntu-18.04' + run: lscpu + + - name: Build + run: ${{ env.CARGO }} build --verbose $TARGET + + - name: Build (no default) + run: ${{ env.CARGO }} build --verbose $TARGET --no-default-features + + - name: Build docs + run: ${{ env.CARGO }} doc --verbose $TARGET + + # Our dev dependencies evolve more rapidly than we'd like, so only run + # tests when we aren't pinning the Rust version. + - name: Tests + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose $TARGET + + - name: Tests (no default, lib only) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --lib $TARGET + + - name: Tests (i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --features i128 $TARGET + + - name: Tests (no default, lib only, i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --features i128 --lib $TARGET + + - name: Compile benchmarks + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run $TARGET + + - name: Compile benchmarks (no default) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features $TARGET + + - name: Compile benchmarks (i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --features i128 $TARGET + + - name: Compile benchmarks (no default, i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features --features i128 $TARGET + + rustfmt: + name: rustfmt + runs-on: ubuntu-18.04 + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + profile: minimal + components: rustfmt + - name: Check formatting + run: cargo fmt -- --check diff --git a/Android.bp b/Android.bp index 72ef217..8f42c1a 100644 --- a/Android.bp +++ b/Android.bp @@ -1,17 +1,13 @@ -// This file is generated by cargo2android.py. +// This file is generated by cargo2android.py --run --device --dependencies. -rust_library_rlib { +rust_library { name: "libbyteorder", - deny_warnings: false, host_supported: true, crate_name: "byteorder", srcs: ["src/lib.rs"], - edition: "2015", + edition: "2018", features: [ "default", "std", ], - flags: [ - "--cfg byteorder_i128", - ], } diff --git a/CHANGELOG.md b/CHANGELOG.md index 020beb4..6b51eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,28 @@ +1.3.4 +===== +This patch release squashes deprecation warnings for the `try!` macro, in +accordance with byteorder's minimum supported Rust version (currently at Rust +1.12.0). + + +1.3.3 +===== +This patch release adds `ByteOrder::write_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.2 +===== +This patch release adds `ReadBytesExt::read_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.1 +===== +This minor release performs mostly small internal changes. Going forward, these +are not going to be incorporated into the changelog. + + 1.3.0 ===== This new minor release now enables `i128` support automatically on Rust diff --git a/Cargo.toml b/Cargo.toml index fb2acad..ff60b60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,17 +11,17 @@ # will likely look very different (and much more reasonable) [package] +edition = "2018" name = "byteorder" -version = "1.3.2" +version = "1.4.2" authors = ["Andrew Gallant "] -build = "build.rs" exclude = ["/ci/*"] description = "Library for reading/writing numbers in big-endian and little-endian." homepage = "https://github.com/BurntSushi/byteorder" documentation = "https://docs.rs/byteorder" readme = "README.md" keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] license = "Unlicense OR MIT" repository = "https://github.com/BurntSushi/byteorder" [profile.bench] @@ -30,19 +30,14 @@ opt-level = 3 [lib] name = "byteorder" bench = false -[dev-dependencies.doc-comment] -version = "0.3" - [dev-dependencies.quickcheck] -version = "0.8" +version = "0.9.2" default-features = false [dev-dependencies.rand] -version = "0.6" +version = "0.7" [features] default = ["std"] i128 = [] std = [] -[badges.travis-ci] -repository = "BurntSushi/byteorder" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 91f2aa3..efeafb2 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,26 +1,25 @@ [package] name = "byteorder" -version = "1.3.2" #:version +version = "1.4.2" #:version authors = ["Andrew Gallant "] description = "Library for reading/writing numbers in big-endian and little-endian." documentation = "https://docs.rs/byteorder" homepage = "https://github.com/BurntSushi/byteorder" repository = "https://github.com/BurntSushi/byteorder" readme = "README.md" -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] license = "Unlicense OR MIT" exclude = ["/ci/*"] -build = "build.rs" +edition = "2018" [lib] name = "byteorder" bench = false [dev-dependencies] -quickcheck = { version = "0.8", default-features = false } -rand = "0.6" -doc-comment = "0.3" +quickcheck = { version = "0.9.2", default-features = false } +rand = "0.7" [features] default = ["std"] @@ -33,6 +32,3 @@ i128 = [] [profile.bench] opt-level = 3 - -[badges] -travis-ci = { repository = "BurntSushi/byteorder" } diff --git a/LICENSE b/LICENSE new file mode 120000 index 0000000..7f9a88e --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +LICENSE-MIT \ No newline at end of file diff --git a/METADATA b/METADATA new file mode 100644 index 0000000..95a62dd --- /dev/null +++ b/METADATA @@ -0,0 +1,19 @@ +name: "byteorder" +description: "Library for reading/writing numbers in big-endian and little-endian." +third_party { + url { + type: HOMEPAGE + value: "https://crates.io/crates/byteorder" + } + url { + type: ARCHIVE + value: "https://static.crates.io/crates/byteorder/byteorder-1.4.2.crate" + } + version: "1.4.2" + license_type: NOTICE + last_upgrade_date { + year: 2021 + month: 2 + day: 7 + } +} diff --git a/MODULE_LICENSE_MIT b/MODULE_LICENSE_MIT new file mode 100644 index 0000000..e69de29 diff --git a/OWNERS b/OWNERS new file mode 100644 index 0000000..46fc303 --- /dev/null +++ b/OWNERS @@ -0,0 +1 @@ +include platform/prebuilts/rust:/OWNERS diff --git a/README.md b/README.md index 8940b29..8b2ecc4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ +byteorder +========= This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order. -[![Build status](https://api.travis-ci.org/BurntSushi/byteorder.svg)](https://travis-ci.org/BurntSushi/byteorder) +[![Build status](https://github.com/BurntSushi/byteorder/workflows/ci/badge.svg)](https://github.com/BurntSushi/byteorder/actions) [![](http://meritbadge.herokuapp.com/byteorder)](https://crates.io/crates/byteorder) Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org). @@ -27,8 +29,6 @@ If you want to augment existing `Read` and `Write` traits, then import the extension methods like so: ```rust -extern crate byteorder; - use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian, LittleEndian}; ``` diff --git a/benches/bench.rs b/benches/bench.rs index d53d25e..bb00422 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,15 +1,15 @@ #![feature(test)] -extern crate byteorder; -extern crate rand; extern crate test; macro_rules! bench_num { - ($name:ident, $read:ident, $bytes:expr, $data:expr) => ( + ($name:ident, $read:ident, $bytes:expr, $data:expr) => { mod $name { - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -43,14 +43,16 @@ macro_rules! bench_num { }); } } - ); + }; ($ty:ident, $max:ident, - $read:ident, $write:ident, $size:expr, $data:expr) => ( + $read:ident, $write:ident, $size:expr, $data:expr) => { mod $ty { + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use std::$ty; - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -117,7 +119,7 @@ macro_rules! bench_num { }); } } - ); + }; } bench_num!(u16, MAX, read_u16, write_u16, 2, [1, 2]); @@ -127,8 +129,7 @@ bench_num!(i32, MAX, read_i32, write_i32, 4, [1, 2, 3, 4]); bench_num!(u64, MAX, read_u64, write_u64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(i64, MAX, read_i64, write_i64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(f32, MAX, read_f32, write_f32, 4, [1, 2, 3, 4]); -bench_num!(f64, MAX, read_f64, write_f64, 8, - [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(f64, MAX, read_f64, write_f64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(uint_1, read_uint, 1, [1]); bench_num!(uint_2, read_uint, 2, [1, 2]); @@ -148,120 +149,115 @@ bench_num!(int_6, read_int, 6, [1, 2, 3, 4, 5, 6]); bench_num!(int_7, read_int, 7, [1, 2, 3, 4, 5, 6, 7]); bench_num!(int_8, read_int, 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(u128, MAX, read_u128, write_u128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); -#[cfg(byteorder_i128)] -bench_num!(i128, MAX, read_i128, write_i128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); - -#[cfg(byteorder_i128)] -bench_num!(uint128_1, read_uint128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(uint128_2, read_uint128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(uint128_3, read_uint128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(uint128_4, read_uint128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(uint128_5, read_uint128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(uint128_6, read_uint128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(uint128_7, read_uint128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(uint128_8, read_uint128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(uint128_9, read_uint128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(uint128_10, read_uint128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(uint128_11, read_uint128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(uint128_12, read_uint128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(uint128_13, read_uint128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(uint128_14, read_uint128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(uint128_15, read_uint128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(uint128_16, read_uint128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!( + u128, + MAX, + read_u128, + write_u128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!( + i128, + MAX, + read_i128, + write_i128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); -#[cfg(byteorder_i128)] -bench_num!(int128_1, read_int128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(int128_2, read_int128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(int128_3, read_int128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(int128_4, read_int128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(int128_5, read_int128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(int128_6, read_int128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(int128_7, read_int128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(int128_8, read_int128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(int128_9, read_int128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(int128_10, read_int128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(int128_11, read_int128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(int128_12, read_int128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(int128_13, read_int128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(int128_14, read_int128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(int128_15, read_int128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(int128_16, read_int128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!(uint128_1, read_uint128, 1, [1]); +bench_num!(uint128_2, read_uint128, 2, [1, 2]); +bench_num!(uint128_3, read_uint128, 3, [1, 2, 3]); +bench_num!(uint128_4, read_uint128, 4, [1, 2, 3, 4]); +bench_num!(uint128_5, read_uint128, 5, [1, 2, 3, 4, 5]); +bench_num!(uint128_6, read_uint128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(uint128_7, read_uint128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(uint128_8, read_uint128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(uint128_9, read_uint128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(uint128_10, read_uint128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(uint128_11, read_uint128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + uint128_12, + read_uint128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + uint128_13, + read_uint128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + uint128_14, + read_uint128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + uint128_15, + read_uint128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + uint128_16, + read_uint128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!(int128_1, read_int128, 1, [1]); +bench_num!(int128_2, read_int128, 2, [1, 2]); +bench_num!(int128_3, read_int128, 3, [1, 2, 3]); +bench_num!(int128_4, read_int128, 4, [1, 2, 3, 4]); +bench_num!(int128_5, read_int128, 5, [1, 2, 3, 4, 5]); +bench_num!(int128_6, read_int128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(int128_7, read_int128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(int128_8, read_int128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(int128_9, read_int128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(int128_10, read_int128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(int128_11, read_int128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + int128_12, + read_int128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + int128_13, + read_int128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + int128_14, + read_int128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + int128_15, + read_int128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + int128_16, + read_int128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); macro_rules! bench_slice { ($name:ident, $numty:ty, $read:ident, $write:ident) => { mod $name { use std::mem::size_of; - use byteorder::{ByteOrder, BigEndian, LittleEndian}; - use rand::{self, Rng}; + use byteorder::{BigEndian, ByteOrder, LittleEndian}; use rand::distributions; + use rand::{self, Rng}; use test::Bencher; #[bench] @@ -322,7 +318,7 @@ macro_rules! bench_slice { }); } } - } + }; } bench_slice!(slice_u64, u64, read_u64_into, write_u64_into); diff --git a/build.rs b/build.rs deleted file mode 100644 index 002135b..0000000 --- a/build.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::env; -use std::ffi::OsString; -use std::io::{self, Write}; -use std::process::Command; - -fn main() { - let version = match Version::read() { - Ok(version) => version, - Err(err) => { - writeln!( - &mut io::stderr(), - "failed to parse `rustc --version`: {}", - err - ).unwrap(); - return; - } - }; - enable_i128(version); -} - -fn enable_i128(version: Version) { - if version < (Version { major: 1, minor: 26, patch: 0 }) { - return; - } - - println!("cargo:rustc-cfg=byteorder_i128"); -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] -struct Version { - major: u32, - minor: u32, - patch: u32, -} - -impl Version { - fn read() -> Result { - let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); - let output = Command::new(&rustc) - .arg("--version") - .output() - .unwrap() - .stdout; - Version::parse(&String::from_utf8(output).unwrap()) - } - - fn parse(mut s: &str) -> Result { - if !s.starts_with("rustc ") { - return Err(format!("unrecognized version string: {}", s)); - } - s = &s["rustc ".len()..]; - - let parts: Vec<&str> = s.split(".").collect(); - if parts.len() < 3 { - return Err(format!("not enough version parts: {:?}", parts)); - } - - let mut num = String::new(); - for c in parts[0].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let major = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[1].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let minor = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[2].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let patch = try!(num.parse::().map_err(|e| e.to_string())); - - Ok(Version { major: major, minor: minor, patch: patch }) - } -} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..aa37a21 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 79 +use_small_heuristics = "max" diff --git a/src/io.rs b/src/io.rs index ed6a848..ac41ff7 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,7 +1,9 @@ -use std::io::{self, Result}; -use std::slice; +use std::{ + io::{self, Result}, + slice, +}; -use ByteOrder; +use crate::ByteOrder; /// Extends [`Read`] with methods for reading numbers. (For `std::io`.) /// @@ -52,7 +54,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0]) } @@ -82,7 +84,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0] as i8) } @@ -109,7 +111,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u16(&buf)) } @@ -136,7 +138,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i16(&buf)) } @@ -162,7 +164,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u24(&buf)) } @@ -188,7 +190,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i24(&buf)) } @@ -214,7 +216,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u32(&buf)) } @@ -240,7 +242,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i32(&buf)) } @@ -266,7 +268,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u48(&buf)) } @@ -292,7 +294,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i48(&buf)) } @@ -318,7 +320,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u64(&buf)) } @@ -344,7 +346,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i64(&buf)) } @@ -370,11 +372,10 @@ pub trait ReadBytesExt: io::Read { /// ]); /// assert_eq!(16947640962301618749969007319746179, rdr.read_u128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u128(&buf)) } @@ -397,11 +398,10 @@ pub trait ReadBytesExt: io::Read { /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); /// assert_eq!(i128::min_value(), rdr.read_i128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i128(&buf)) } @@ -426,7 +426,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_uint(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint(&buf[..nbytes], nbytes)) } @@ -451,25 +451,23 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_int(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int(&buf[..nbytes], nbytes)) } /// Reads an unsigned n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_uint128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint128(&buf[..nbytes], nbytes)) } /// Reads a signed n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_int128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int128(&buf[..nbytes], nbytes)) } @@ -500,7 +498,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f32(&buf)) } @@ -531,7 +529,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f64(&buf)) } @@ -564,7 +562,7 @@ pub trait ReadBytesExt: io::Read { fn read_u16_into(&mut self, dst: &mut [u16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u16(dst); Ok(()) @@ -599,7 +597,7 @@ pub trait ReadBytesExt: io::Read { fn read_u32_into(&mut self, dst: &mut [u32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u32(dst); Ok(()) @@ -637,7 +635,7 @@ pub trait ReadBytesExt: io::Read { fn read_u64_into(&mut self, dst: &mut [u64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u64(dst); Ok(()) @@ -671,7 +669,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_u128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128_into( &mut self, @@ -679,7 +676,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u128(dst); Ok(()) @@ -750,7 +747,7 @@ pub trait ReadBytesExt: io::Read { fn read_i16_into(&mut self, dst: &mut [i16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i16(dst); Ok(()) @@ -785,7 +782,7 @@ pub trait ReadBytesExt: io::Read { fn read_i32_into(&mut self, dst: &mut [i32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i32(dst); Ok(()) @@ -823,7 +820,7 @@ pub trait ReadBytesExt: io::Read { fn read_i64_into(&mut self, dst: &mut [i64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i64(dst); Ok(()) @@ -857,7 +854,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_i128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into( &mut self, @@ -865,7 +861,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i128(dst); Ok(()) @@ -903,13 +899,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f32_into( - &mut self, - dst: &mut [f32], - ) -> Result<()> { + fn read_f32_into(&mut self, dst: &mut [f32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f32(dst); Ok(()) @@ -951,7 +944,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked( &mut self, dst: &mut [f32], @@ -991,13 +984,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f64_into( - &mut self, - dst: &mut [f64], - ) -> Result<()> { + fn read_f64_into(&mut self, dst: &mut [f64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f64(dst); Ok(()) @@ -1045,7 +1035,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked( &mut self, dst: &mut [f64], @@ -1408,7 +1398,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes an unsigned 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_u128(&mut self, n: u128) -> Result<()> { let mut buf = [0; 16]; @@ -1417,7 +1406,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes a signed 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_i128(&mut self, n: i128) -> Result<()> { let mut buf = [0; 16]; @@ -1501,7 +1489,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_uint128( &mut self, @@ -1517,7 +1504,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_int128( &mut self, diff --git a/src/lib.rs b/src/lib.rs index db4d24d..d56574d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,23 +70,12 @@ cases. #![deny(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "std")] -extern crate core; - -#[cfg(test)] -#[macro_use] -extern crate doc_comment; - -#[cfg(test)] -doctest!("../README.md"); - -use core::fmt::Debug; -use core::hash::Hash; -use core::ptr::copy_nonoverlapping; -use core::slice; +use core::{ + convert::TryInto, fmt::Debug, hash::Hash, ptr::copy_nonoverlapping, slice, +}; #[cfg(feature = "std")] -pub use io::{ReadBytesExt, WriteBytesExt}; +pub use crate::io::{ReadBytesExt, WriteBytesExt}; #[cfg(feature = "std")] mod io; @@ -97,7 +86,6 @@ fn extend_sign(val: u64, nbytes: usize) -> i64 { (val << shift) as i64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn extend_sign128(val: u128, nbytes: usize) -> i128 { let shift = (16 - nbytes) * 8; @@ -110,7 +98,6 @@ fn unextend_sign(val: i64, nbytes: usize) -> u64 { (val << shift) as u64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn unextend_sign128(val: i128, nbytes: usize) -> u128 { let shift = (16 - nbytes) * 8; @@ -138,7 +125,6 @@ fn pack_size(n: u64) -> usize { } } -#[cfg(byteorder_i128)] #[inline] fn pack_size128(n: u128) -> usize { if n < 1 << 8 { @@ -179,7 +165,7 @@ fn pack_size128(n: u128) -> usize { mod private { /// Sealed stops crates other than byteorder from implementing any traits /// that use it. - pub trait Sealed{} + pub trait Sealed {} impl Sealed for super::LittleEndian {} impl Sealed for super::BigEndian {} } @@ -219,8 +205,16 @@ mod private { /// /// [`BigEndian`]: enum.BigEndian.html /// [`LittleEndian`]: enum.LittleEndian.html -pub trait ByteOrder - : Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd +pub trait ByteOrder: + Clone + + Copy + + Debug + + Default + + Eq + + Hash + + Ord + + PartialEq + + PartialOrd + private::Sealed { /// Reads an unsigned 16 bit integer from `buf`. @@ -327,7 +321,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn read_u128(buf: &[u8]) -> u128; /// Reads an unsigned n-bytes integer from `buf`. @@ -368,7 +361,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn read_uint128(buf: &[u8], nbytes: usize) -> u128; /// Writes an unsigned 16 bit integer `n` to `buf`. @@ -487,7 +479,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn write_u128(buf: &mut [u8], n: u128); /// Writes an unsigned integer `n` to `buf` using only `nbytes`. @@ -528,7 +519,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize); /// Reads a signed 16 bit integer from `buf`. @@ -658,7 +648,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(buf: &[u8]) -> i128 { Self::read_u128(buf) as i128 @@ -705,7 +694,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_int128(buf: &[u8], nbytes: usize) -> i128 { extend_sign128(Self::read_uint128(buf, nbytes), nbytes) @@ -731,7 +719,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f32(buf: &[u8]) -> f32 { - unsafe { *(&Self::read_u32(buf) as *const u32 as *const f32) } + f32::from_bits(Self::read_u32(buf)) } /// Reads a IEEE754 double-precision (8 bytes) floating point number. @@ -754,7 +742,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f64(buf: &[u8]) -> f64 { - unsafe { *(&Self::read_u64(buf) as *const u64 as *const f64) } + f64::from_bits(Self::read_u64(buf)) } /// Writes a signed 16 bit integer `n` to `buf`. @@ -884,7 +872,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_i128(buf: &mut [u8], n: i128) { Self::write_u128(buf, n as u128) @@ -931,7 +918,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_int128(buf: &mut [u8], n: i128, nbytes: usize) { Self::write_uint128(buf, unextend_sign128(n, nbytes), nbytes) @@ -957,8 +943,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f32(buf: &mut [u8], n: f32) { - let n = unsafe { *(&n as *const f32 as *const u32) }; - Self::write_u32(buf, n) + Self::write_u32(buf, n.to_bits()) } /// Writes a IEEE754 double-precision (8 bytes) floating point number. @@ -981,8 +966,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f64(buf: &mut [u8], n: f64) { - let n = unsafe { *(&n as *const f64 as *const u64) }; - Self::write_u64(buf, n) + Self::write_u64(buf, n.to_bits()) } /// Reads unsigned 16 bit integers from `src` into `dst`. @@ -1075,7 +1059,6 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn read_u128_into(src: &[u8], dst: &mut [u128]); /// Reads signed 16 bit integers from `src` to `dst`. @@ -1186,7 +1169,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into(src: &[u8], dst: &mut [i128]) { let dst = unsafe { @@ -1251,7 +1233,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) { Self::read_f32_into(src, dst); } @@ -1313,7 +1295,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) { Self::read_f64_into(src, dst); } @@ -1408,9 +1390,42 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_u128_into(src: &[u128], dst: &mut [u8]); + /// Writes signed 8 bit integers from `src` into `dst`. + /// + /// Note that since each `i8` is a single byte, no byte order conversions + /// are used. This method is included because it provides a safe, simple + /// way for the caller to write from a `&[i8]` buffer. (Without this + /// method, the caller would have to either use `unsafe` code or convert + /// each byte to `u8` individually.) + /// + /// # Panics + /// + /// Panics when `buf.len() != src.len()`. + /// + /// # Examples + /// + /// Write and read `i8` numbers in little endian order: + /// + /// ```rust + /// use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; + /// + /// let mut bytes = [0; 4]; + /// let numbers_given = [1, 2, 0xf, 0xe]; + /// LittleEndian::write_i8_into(&numbers_given, &mut bytes); + /// + /// let mut numbers_got = [0; 4]; + /// bytes.as_ref().read_i8_into(&mut numbers_got); + /// assert_eq!(numbers_given, numbers_got); + /// ``` + fn write_i8_into(src: &[i8], dst: &mut [u8]) { + let src = unsafe { + slice::from_raw_parts(src.as_ptr() as *const u8, src.len()) + }; + dst.copy_from_slice(src); + } + /// Writes signed 16 bit integers from `src` into `dst`. /// /// # Panics @@ -1516,7 +1531,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_i128_into(src: &[i128], dst: &mut [u8]) { let src = unsafe { slice::from_raw_parts(src.as_ptr() as *const u128, src.len()) @@ -1660,7 +1674,6 @@ pub trait ByteOrder /// BigEndian::from_slice_u128(&mut numbers); /// assert_eq!(numbers, [5u128.to_be(), 65000u128.to_be()]); /// ``` - #[cfg(byteorder_i128)] fn from_slice_u128(numbers: &mut [u128]); /// Converts the given slice of signed 16 bit integers to a particular @@ -1755,7 +1768,6 @@ pub trait ByteOrder /// BigEndian::from_slice_i128(&mut numbers); /// assert_eq!(numbers, [5i128.to_be(), 65000i128.to_be()]); /// ``` - #[cfg(byteorder_i128)] #[inline] fn from_slice_i128(src: &mut [i128]) { let src = unsafe { @@ -1887,30 +1899,15 @@ pub type NativeEndian = LittleEndian; #[cfg(target_endian = "big")] pub type NativeEndian = BigEndian; -macro_rules! read_num_bytes { - ($ty:ty, $size:expr, $src:expr, $which:ident) => ({ - assert!($size == ::core::mem::size_of::<$ty>()); - assert!($size <= $src.len()); - let mut data: $ty = 0; - unsafe { - copy_nonoverlapping( - $src.as_ptr(), - &mut data as *mut $ty as *mut u8, - $size); - } - data.$which() - }); -} - macro_rules! write_num_bytes { - ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => ({ + ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => {{ assert!($size <= $dst.len()); unsafe { // N.B. https://github.com/rust-lang/rust/issues/22776 let bytes = *(&$n.$which() as *const _ as *const [u8; $size]); copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size); } - }); + }}; } macro_rules! read_slice { @@ -1921,7 +1918,8 @@ macro_rules! read_slice { copy_nonoverlapping( $src.as_ptr(), $dst.as_mut_ptr() as *mut u8, - $src.len()); + $src.len(), + ); } for v in $dst.iter_mut() { *v = v.$which(); @@ -1938,67 +1936,72 @@ macro_rules! write_slice_native { copy_nonoverlapping( $src.as_ptr() as *const u8, $dst.as_mut_ptr(), - $dst.len()); + $dst.len(), + ); } }}; } macro_rules! write_slice { - ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => ({ + ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => {{ assert!($size == ::core::mem::size_of::<$ty>()); assert_eq!($size * $src.len(), $dst.len()); for (&n, chunk) in $src.iter().zip($dst.chunks_mut($size)) { $write(chunk, n); } - }); + }}; } impl ByteOrder for BigEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_be) + u16::from_be_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_be) + u32::from_be_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_be) + u64::from_be_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_be) + u128::from_be_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((8 - nbytes) as isize), nbytes); - (*(ptr_out as *const u64)).to_be() + buf.as_ptr(), + ptr_out.offset((8 - nbytes) as isize), + nbytes, + ); } + out.to_be() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((16 - nbytes) as isize), nbytes); - (*(ptr_out as *const u128)).to_be() + buf.as_ptr(), + ptr_out.offset((16 - nbytes) as isize), + nbytes, + ); } + out.to_be() } #[inline] @@ -2016,7 +2019,6 @@ impl ByteOrder for BigEndian { write_num_bytes!(u64, 8, n, buf, to_be); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_be); @@ -2031,11 +2033,11 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((8 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n) <= nbytes && nbytes <= 16); @@ -2045,7 +2047,8 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((16 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } @@ -2064,7 +2067,6 @@ impl ByteOrder for BigEndian { read_slice!(src, dst, 8, to_be); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_be); @@ -2097,7 +2099,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "big") { @@ -2134,7 +2135,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "little") { @@ -2172,46 +2172,44 @@ impl ByteOrder for BigEndian { impl ByteOrder for LittleEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_le) + u16::from_le_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_le) + u32::from_le_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_le) + u64::from_le_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_le) + u128::from_le_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u64)).to_le() } + out.to_le() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u128)).to_le() } + out.to_le() } #[inline] @@ -2229,7 +2227,6 @@ impl ByteOrder for LittleEndian { write_num_bytes!(u64, 8, n, buf, to_le); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_le); @@ -2245,7 +2242,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n as u128) <= nbytes && nbytes <= 16); @@ -2271,7 +2267,6 @@ impl ByteOrder for LittleEndian { read_slice!(src, dst, 8, to_le); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_le); @@ -2304,7 +2299,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "little") { @@ -2341,7 +2335,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "big") { @@ -2378,15 +2371,8 @@ impl ByteOrder for LittleEndian { #[cfg(test)] mod test { - extern crate quickcheck; - extern crate rand; - - use self::quickcheck::{QuickCheck, StdGen, Testable}; - use self::rand::thread_rng; - #[cfg(byteorder_i128)] - use self::rand::Rng; - #[cfg(byteorder_i128)] - use self::quickcheck::{Arbitrary, Gen}; + use quickcheck::{Arbitrary, Gen, QuickCheck, StdGen, Testable}; + use rand::{thread_rng, Rng}; pub const U24_MAX: u32 = 16_777_215; pub const I24_MAX: i32 = 8_388_607; @@ -2397,7 +2383,9 @@ mod test { pub const I64_MAX: u64 = ::core::i64::MAX as u64; macro_rules! calc_max { - ($max:expr, $bytes:expr) => { calc_max!($max, $bytes, 8) }; + ($max:expr, $bytes:expr) => { + calc_max!($max, $bytes, 8) + }; ($max:expr, $bytes:expr, $maxbytes:expr) => { ($max - 1) >> (8 * ($maxbytes - $bytes)) }; @@ -2406,7 +2394,6 @@ mod test { #[derive(Clone, Debug)] pub struct Wi128(pub T); - #[cfg(byteorder_i128)] impl Wi128 { pub fn clone(&self) -> T { self.0.clone() @@ -2419,24 +2406,20 @@ mod test { } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::u128::MAX, gen.size(), 16); - let output = - (gen.gen::() as u128) | - ((gen.gen::() as u128) << 64); + let output = (gen.gen::() as u128) + | ((gen.gen::() as u128) << 64); Wi128(output & (max - 1)) } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::i128::MAX, gen.size(), 16); - let output = - (gen.gen::() as i128) | - ((gen.gen::() as i128) << 64); + let output = (gen.gen::() as i128) + | ((gen.gen::() as i128) << 64); Wi128(output & (max - 1)) } } @@ -2451,17 +2434,20 @@ mod test { macro_rules! qc_byte_order { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] fn big_endian() { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; BigEndian::$write(&mut buf, n.clone(), $bytes); - n == BigEndian::$read(&mut buf[..$bytes], $bytes) + n == BigEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2471,7 +2457,7 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; LittleEndian::$write(&mut buf, n.clone(), $bytes); - n == LittleEndian::$read(&mut buf[..$bytes], $bytes) + n == LittleEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2481,18 +2467,21 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; NativeEndian::$write(&mut buf, n.clone(), $bytes); - n == NativeEndian::$read(&mut buf[..$bytes], $bytes) + n == NativeEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); + }; ($name:ident, $ty_int:ty, $max:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use core::mem::size_of; - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; #[test] fn big_endian() { @@ -2500,7 +2489,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; BigEndian::$write(&mut buf[16 - bytes..], n.clone()); - n == BigEndian::$read(&mut buf[16 - bytes..]) + n == BigEndian::$read(&buf[16 - bytes..]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2511,7 +2500,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; LittleEndian::$write(&mut buf[..bytes], n.clone()); - n == LittleEndian::$read(&mut buf[..bytes]) + n == LittleEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2522,164 +2511,489 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; NativeEndian::$write(&mut buf[..bytes], n.clone()); - n == NativeEndian::$read(&mut buf[..bytes]) + n == NativeEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_byte_order!(prop_u16, u16, ::core::u16::MAX as u64, read_u16, write_u16); - qc_byte_order!(prop_i16, i16, ::core::i16::MAX as u64, read_i16, write_i16); - qc_byte_order!(prop_u24, u32, ::test::U24_MAX as u64, read_u24, write_u24); - qc_byte_order!(prop_i24, i32, ::test::I24_MAX as u64, read_i24, write_i24); - qc_byte_order!(prop_u32, u32, ::core::u32::MAX as u64, read_u32, write_u32); - qc_byte_order!(prop_i32, i32, ::core::i32::MAX as u64, read_i32, write_i32); - qc_byte_order!(prop_u48, u64, ::test::U48_MAX as u64, read_u48, write_u48); - qc_byte_order!(prop_i48, i64, ::test::I48_MAX as u64, read_i48, write_i48); - qc_byte_order!(prop_u64, u64, ::core::u64::MAX as u64, read_u64, write_u64); - qc_byte_order!(prop_i64, i64, ::core::i64::MAX as u64, read_i64, write_i64); - qc_byte_order!(prop_f32, f32, ::core::u64::MAX as u64, read_f32, write_f32); - qc_byte_order!(prop_f64, f64, ::core::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_byte_order!( + prop_u16, + u16, + ::core::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_byte_order!( + prop_i16, + i16, + ::core::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_byte_order!( + prop_u24, + u32, + crate::test::U24_MAX as u64, + read_u24, + write_u24 + ); + qc_byte_order!( + prop_i24, + i32, + crate::test::I24_MAX as u64, + read_i24, + write_i24 + ); + qc_byte_order!( + prop_u32, + u32, + ::core::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_byte_order!( + prop_i32, + i32, + ::core::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_byte_order!( + prop_u48, + u64, + crate::test::U48_MAX as u64, + read_u48, + write_u48 + ); + qc_byte_order!( + prop_i48, + i64, + crate::test::I48_MAX as u64, + read_i48, + write_i48 + ); + qc_byte_order!( + prop_u64, + u64, + ::core::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_byte_order!( + prop_i64, + i64, + ::core::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_byte_order!( + prop_f32, + f32, + ::core::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_byte_order!( + prop_f64, + f64, + ::core::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_byte_order!(prop_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_byte_order!(prop_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_byte_order!(prop_uint_1, - u64, calc_max!(super::U64_MAX, 1), 1, read_uint, write_uint); - qc_byte_order!(prop_uint_2, - u64, calc_max!(super::U64_MAX, 2), 2, read_uint, write_uint); - qc_byte_order!(prop_uint_3, - u64, calc_max!(super::U64_MAX, 3), 3, read_uint, write_uint); - qc_byte_order!(prop_uint_4, - u64, calc_max!(super::U64_MAX, 4), 4, read_uint, write_uint); - qc_byte_order!(prop_uint_5, - u64, calc_max!(super::U64_MAX, 5), 5, read_uint, write_uint); - qc_byte_order!(prop_uint_6, - u64, calc_max!(super::U64_MAX, 6), 6, read_uint, write_uint); - qc_byte_order!(prop_uint_7, - u64, calc_max!(super::U64_MAX, 7), 7, read_uint, write_uint); - qc_byte_order!(prop_uint_8, - u64, calc_max!(super::U64_MAX, 8), 8, read_uint, write_uint); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_1, - Wi128, 1, 1, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_2, - Wi128, 2, 2, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_3, - Wi128, 3, 3, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_4, - Wi128, 4, 4, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_5, - Wi128, 5, 5, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_6, - Wi128, 6, 6, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_7, - Wi128, 7, 7, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_8, - Wi128, 8, 8, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_9, - Wi128, 9, 9, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_10, - Wi128, 10, 10, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_11, - Wi128, 11, 11, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_12, - Wi128, 12, 12, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_13, - Wi128, 13, 13, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_14, - Wi128, 14, 14, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_15, - Wi128, 15, 15, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_16, - Wi128, 16, 16, read_uint128, write_uint128); - - qc_byte_order!(prop_int_1, - i64, calc_max!(super::I64_MAX, 1), 1, read_int, write_int); - qc_byte_order!(prop_int_2, - i64, calc_max!(super::I64_MAX, 2), 2, read_int, write_int); - qc_byte_order!(prop_int_3, - i64, calc_max!(super::I64_MAX, 3), 3, read_int, write_int); - qc_byte_order!(prop_int_4, - i64, calc_max!(super::I64_MAX, 4), 4, read_int, write_int); - qc_byte_order!(prop_int_5, - i64, calc_max!(super::I64_MAX, 5), 5, read_int, write_int); - qc_byte_order!(prop_int_6, - i64, calc_max!(super::I64_MAX, 6), 6, read_int, write_int); - qc_byte_order!(prop_int_7, - i64, calc_max!(super::I64_MAX, 7), 7, read_int, write_int); - qc_byte_order!(prop_int_8, - i64, calc_max!(super::I64_MAX, 8), 8, read_int, write_int); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_1, - Wi128, 1, 1, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_2, - Wi128, 2, 2, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_3, - Wi128, 3, 3, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_4, - Wi128, 4, 4, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_5, - Wi128, 5, 5, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_6, - Wi128, 6, 6, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_7, - Wi128, 7, 7, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_8, - Wi128, 8, 8, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_9, - Wi128, 9, 9, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_10, - Wi128, 10, 10, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_11, - Wi128, 11, 11, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_12, - Wi128, 12, 12, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_13, - Wi128, 13, 13, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_14, - Wi128, 14, 14, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_15, - Wi128, 15, 15, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_16, - Wi128, 16, 16, read_int128, write_int128); - + qc_byte_order!( + prop_uint_1, + u64, + calc_max!(super::U64_MAX, 1), + 1, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_2, + u64, + calc_max!(super::U64_MAX, 2), + 2, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_3, + u64, + calc_max!(super::U64_MAX, 3), + 3, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_4, + u64, + calc_max!(super::U64_MAX, 4), + 4, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_5, + u64, + calc_max!(super::U64_MAX, 5), + 5, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_6, + u64, + calc_max!(super::U64_MAX, 6), + 6, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_7, + u64, + calc_max!(super::U64_MAX, 7), + 7, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_8, + u64, + calc_max!(super::U64_MAX, 8), + 8, + read_uint, + write_uint + ); + + qc_byte_order!( + prop_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_uint128 + ); + + qc_byte_order!( + prop_int_1, + i64, + calc_max!(super::I64_MAX, 1), + 1, + read_int, + write_int + ); + qc_byte_order!( + prop_int_2, + i64, + calc_max!(super::I64_MAX, 2), + 2, + read_int, + write_int + ); + qc_byte_order!( + prop_int_3, + i64, + calc_max!(super::I64_MAX, 3), + 3, + read_int, + write_int + ); + qc_byte_order!( + prop_int_4, + i64, + calc_max!(super::I64_MAX, 4), + 4, + read_int, + write_int + ); + qc_byte_order!( + prop_int_5, + i64, + calc_max!(super::I64_MAX, 5), + 5, + read_int, + write_int + ); + qc_byte_order!( + prop_int_6, + i64, + calc_max!(super::I64_MAX, 6), + 6, + read_int, + write_int + ); + qc_byte_order!( + prop_int_7, + i64, + calc_max!(super::I64_MAX, 7), + 7, + read_int, + write_int + ); + qc_byte_order!( + prop_int_8, + i64, + calc_max!(super::I64_MAX, 8), + 8, + read_int, + write_int + ); + + qc_byte_order!( + prop_int128_1, + Wi128, + 1, + 1, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_2, + Wi128, + 2, + 2, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_3, + Wi128, + 3, + 3, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_4, + Wi128, + 4, + 4, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_5, + Wi128, + 5, + 5, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_6, + Wi128, + 6, + 6, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_7, + Wi128, + 7, + 7, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_8, + Wi128, + 8, + 8, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_9, + Wi128, + 9, + 9, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_10, + Wi128, + 10, + 10, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_11, + Wi128, + 11, + 11, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_12, + Wi128, + 12, + 12, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_13, + Wi128, + 13, + 13, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_14, + Wi128, + 14, + 14, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_15, + Wi128, + 15, + 15, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_16, + Wi128, + 16, + 16, + read_int128, + write_int128 + ); // Test that all of the byte conversion functions panic when given a // buffer that is too small. @@ -2688,9 +3002,11 @@ mod test { // with a buffer overflow. macro_rules! too_small { ($name:ident, $maximally_small:expr, $zero:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2734,10 +3050,12 @@ mod test { NativeEndian::$write(&mut buf, $zero); } } - ); - ($name:ident, $maximally_small:expr, $read:ident) => ( + }; + ($name:ident, $maximally_small:expr, $read:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2760,7 +3078,7 @@ mod test { NativeEndian::$read(&buf, $maximally_small + 1); } } - ); + }; } too_small!(small_u16, 1, 0, read_u16, write_u16); @@ -2771,9 +3089,7 @@ mod test { too_small!(small_i64, 7, 0, read_i64, write_i64); too_small!(small_f32, 3, 0.0, read_f32, write_f32); too_small!(small_f64, 7, 0.0, read_f64, write_f64); - #[cfg(byteorder_i128)] too_small!(small_u128, 15, 0, read_u128, write_u128); - #[cfg(byteorder_i128)] too_small!(small_i128, 15, 0, read_i128, write_i128); too_small!(small_uint_1, 1, read_uint); @@ -2784,35 +3100,20 @@ mod test { too_small!(small_uint_6, 6, read_uint); too_small!(small_uint_7, 7, read_uint); - #[cfg(byteorder_i128)] too_small!(small_uint128_1, 1, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_2, 2, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_3, 3, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_4, 4, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_5, 5, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_6, 6, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_7, 7, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_8, 8, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_9, 9, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_10, 10, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_11, 11, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_12, 12, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_13, 13, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_14, 14, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_15, 15, read_uint128); too_small!(small_int_1, 1, read_int); @@ -2823,35 +3124,20 @@ mod test { too_small!(small_int_6, 6, read_int); too_small!(small_int_7, 7, read_int); - #[cfg(byteorder_i128)] too_small!(small_int128_1, 1, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_2, 2, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_3, 3, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_4, 4, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_5, 5, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_6, 6, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_7, 7, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_8, 8, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_9, 9, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_10, 10, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_11, 11, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_12, 12, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_13, 13, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_14, 14, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_15, 15, read_int128); // Test that reading/writing slices enforces the correct lengths. @@ -2859,7 +3145,9 @@ mod test { ($name:ident, $read:ident, $write:ident, $num_bytes:expr, $numbers:expr) => { mod $name { - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2909,54 +3197,171 @@ mod test { NativeEndian::$write(&numbers, &mut bytes); } } - } + }; } slice_lengths!( - slice_len_too_small_u16, read_u16_into, write_u16_into, 3, [0, 0]); + slice_len_too_small_u16, + read_u16_into, + write_u16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u16, read_u16_into, write_u16_into, 5, [0, 0]); + slice_len_too_big_u16, + read_u16_into, + write_u16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i16, read_i16_into, write_i16_into, 3, [0, 0]); + slice_len_too_small_i16, + read_i16_into, + write_i16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i16, read_i16_into, write_i16_into, 5, [0, 0]); + slice_len_too_big_i16, + read_i16_into, + write_i16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u32, read_u32_into, write_u32_into, 7, [0, 0]); + slice_len_too_small_u32, + read_u32_into, + write_u32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u32, read_u32_into, write_u32_into, 9, [0, 0]); + slice_len_too_big_u32, + read_u32_into, + write_u32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i32, read_i32_into, write_i32_into, 7, [0, 0]); + slice_len_too_small_i32, + read_i32_into, + write_i32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i32, read_i32_into, write_i32_into, 9, [0, 0]); + slice_len_too_big_i32, + read_i32_into, + write_i32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u64, read_u64_into, write_u64_into, 15, [0, 0]); + slice_len_too_small_u64, + read_u64_into, + write_u64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u64, read_u64_into, write_u64_into, 17, [0, 0]); + slice_len_too_big_u64, + read_u64_into, + write_u64_into, + 17, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i64, read_i64_into, write_i64_into, 15, [0, 0]); + slice_len_too_small_i64, + read_i64_into, + write_i64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i64, read_i64_into, write_i64_into, 17, [0, 0]); + slice_len_too_big_i64, + read_i64_into, + write_i64_into, + 17, + [0, 0] + ); - #[cfg(byteorder_i128)] slice_lengths!( - slice_len_too_small_u128, read_u128_into, write_u128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_u128, + read_u128_into, + write_u128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u128, read_u128_into, write_u128_into, 33, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_big_u128, + read_u128_into, + write_u128_into, + 33, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i128, read_i128_into, write_i128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_i128, + read_i128_into, + write_i128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i128, read_i128_into, write_i128_into, 33, [0, 0]); + slice_len_too_big_i128, + read_i128_into, + write_i128_into, + 33, + [0, 0] + ); #[test] fn uint_bigger_buffer() { - use {ByteOrder, LittleEndian}; + use crate::{ByteOrder, LittleEndian}; let n = LittleEndian::read_uint(&[1, 2, 3, 4, 5, 6, 7, 8], 5); - assert_eq!(n, 0x0504030201); + assert_eq!(n, 0x05_0403_0201); + } + + #[test] + fn regression173_array_impl() { + use crate::{BigEndian, ByteOrder, LittleEndian}; + + let xs = [0; 100]; + + let x = BigEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i128(&xs); + assert_eq!(x, 0); + + let x = LittleEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i128(&xs); + assert_eq!(x, 0); } } @@ -2970,7 +3375,6 @@ mod stdtests { use self::rand::thread_rng; fn qc_unsized(f: A) { - QuickCheck::new() .gen(StdGen::new(thread_rng(), 16)) .tests(1_00) @@ -2979,19 +3383,22 @@ mod stdtests { } macro_rules! calc_max { - ($max:expr, $bytes:expr) => { ($max - 1) >> (8 * (8 - $bytes)) }; + ($max:expr, $bytes:expr) => { + ($max - 1) >> (8 * (8 - $bytes)) + }; } macro_rules! qc_bytes_ext { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3032,15 +3439,16 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); - ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => ( + }; + ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3075,188 +3483,484 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_bytes_ext!(prop_ext_u16, - u16, ::std::u16::MAX as u64, read_u16, write_u16); - qc_bytes_ext!(prop_ext_i16, - i16, ::std::i16::MAX as u64, read_i16, write_i16); - qc_bytes_ext!(prop_ext_u32, - u32, ::std::u32::MAX as u64, read_u32, write_u32); - qc_bytes_ext!(prop_ext_i32, - i32, ::std::i32::MAX as u64, read_i32, write_i32); - qc_bytes_ext!(prop_ext_u64, - u64, ::std::u64::MAX as u64, read_u64, write_u64); - qc_bytes_ext!(prop_ext_i64, - i64, ::std::i64::MAX as u64, read_i64, write_i64); - qc_bytes_ext!(prop_ext_f32, - f32, ::std::u64::MAX as u64, read_f32, write_f32); - qc_bytes_ext!(prop_ext_f64, - f64, ::std::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_bytes_ext!( + prop_ext_u16, + u16, + ::std::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_bytes_ext!( + prop_ext_i16, + i16, + ::std::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_bytes_ext!( + prop_ext_u32, + u32, + ::std::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_bytes_ext!( + prop_ext_i32, + i32, + ::std::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_bytes_ext!( + prop_ext_u64, + u64, + ::std::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_bytes_ext!( + prop_ext_i64, + i64, + ::std::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_bytes_ext!( + prop_ext_f32, + f32, + ::std::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_bytes_ext!( + prop_ext_f64, + f64, + ::std::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_bytes_ext!(prop_ext_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_bytes_ext!(prop_ext_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_bytes_ext!(prop_ext_uint_1, - u64, calc_max!(::test::U64_MAX, 1), 1, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_2, - u64, calc_max!(::test::U64_MAX, 2), 2, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_3, - u64, calc_max!(::test::U64_MAX, 3), 3, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_4, - u64, calc_max!(::test::U64_MAX, 4), 4, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_5, - u64, calc_max!(::test::U64_MAX, 5), 5, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_6, - u64, calc_max!(::test::U64_MAX, 6), 6, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_7, - u64, calc_max!(::test::U64_MAX, 7), 7, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_8, - u64, calc_max!(::test::U64_MAX, 8), 8, read_uint, write_u64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_1, - Wi128, 1, 1, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_2, - Wi128, 2, 2, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_3, - Wi128, 3, 3, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_4, - Wi128, 4, 4, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_5, - Wi128, 5, 5, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_6, - Wi128, 6, 6, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_7, - Wi128, 7, 7, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_8, - Wi128, 8, 8, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_9, - Wi128, 9, 9, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_10, - Wi128, 10, 10, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_11, - Wi128, 11, 11, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_12, - Wi128, 12, 12, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_13, - Wi128, 13, 13, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_14, - Wi128, 14, 14, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_15, - Wi128, 15, 15, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_16, - Wi128, 16, 16, read_uint128, write_u128); - - qc_bytes_ext!(prop_ext_int_1, - i64, calc_max!(::test::I64_MAX, 1), 1, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_2, - i64, calc_max!(::test::I64_MAX, 2), 2, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_3, - i64, calc_max!(::test::I64_MAX, 3), 3, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_4, - i64, calc_max!(::test::I64_MAX, 4), 4, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_5, - i64, calc_max!(::test::I64_MAX, 5), 5, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_6, - i64, calc_max!(::test::I64_MAX, 6), 6, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_7, - i64, calc_max!(::test::I64_MAX, 1), 7, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_8, - i64, calc_max!(::test::I64_MAX, 8), 8, read_int, write_i64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_1, - Wi128, 1, 1, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_2, - Wi128, 2, 2, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_3, - Wi128, 3, 3, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_4, - Wi128, 4, 4, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_5, - Wi128, 5, 5, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_6, - Wi128, 6, 6, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_7, - Wi128, 7, 7, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_8, - Wi128, 8, 8, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_9, - Wi128, 9, 9, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_10, - Wi128, 10, 10, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_11, - Wi128, 11, 11, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_12, - Wi128, 12, 12, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_13, - Wi128, 13, 13, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_14, - Wi128, 14, 14, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_15, - Wi128, 15, 15, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_16, - Wi128, 16, 16, read_int128, write_i128); + qc_bytes_ext!( + prop_ext_uint_1, + u64, + calc_max!(crate::test::U64_MAX, 1), + 1, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_2, + u64, + calc_max!(crate::test::U64_MAX, 2), + 2, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_3, + u64, + calc_max!(crate::test::U64_MAX, 3), + 3, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_4, + u64, + calc_max!(crate::test::U64_MAX, 4), + 4, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_5, + u64, + calc_max!(crate::test::U64_MAX, 5), + 5, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_6, + u64, + calc_max!(crate::test::U64_MAX, 6), + 6, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_7, + u64, + calc_max!(crate::test::U64_MAX, 7), + 7, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_8, + u64, + calc_max!(crate::test::U64_MAX, 8), + 8, + read_uint, + write_u64 + ); + + qc_bytes_ext!( + prop_ext_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_u128 + ); + + qc_bytes_ext!( + prop_ext_int_1, + i64, + calc_max!(crate::test::I64_MAX, 1), + 1, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_2, + i64, + calc_max!(crate::test::I64_MAX, 2), + 2, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_3, + i64, + calc_max!(crate::test::I64_MAX, 3), + 3, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_4, + i64, + calc_max!(crate::test::I64_MAX, 4), + 4, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_5, + i64, + calc_max!(crate::test::I64_MAX, 5), + 5, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_6, + i64, + calc_max!(crate::test::I64_MAX, 6), + 6, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_7, + i64, + calc_max!(crate::test::I64_MAX, 1), + 7, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_8, + i64, + calc_max!(crate::test::I64_MAX, 8), + 8, + read_int, + write_i64 + ); + + qc_bytes_ext!( + prop_ext_int128_1, + Wi128, + 1, + 1, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_2, + Wi128, + 2, + 2, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_3, + Wi128, + 3, + 3, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_4, + Wi128, + 4, + 4, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_5, + Wi128, + 5, + 5, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_6, + Wi128, + 6, + 6, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_7, + Wi128, + 7, + 7, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_8, + Wi128, + 8, + 8, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_9, + Wi128, + 9, + 9, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_10, + Wi128, + 10, + 10, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_11, + Wi128, + 11, + 11, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_12, + Wi128, + 12, + 12, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_13, + Wi128, + 13, + 13, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_14, + Wi128, + 14, + 14, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_15, + Wi128, + 15, + 15, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_16, + Wi128, + 16, + 16, + read_int128, + write_i128 + ); // Test slice serialization/deserialization. macro_rules! qc_slice { ($name:ident, $ty_int:ty, $read:ident, $write:ident, $zero:expr) => { mod $name { - use core::mem::size_of; - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; use super::qc_unsized; #[allow(unused_imports)] - use test::Wi128; + use crate::test::Wi128; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use core::mem::size_of; #[test] fn big_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; BigEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { BigEndian::$read(&bytes, &mut got); } + unsafe { + BigEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3267,17 +3971,17 @@ mod stdtests { fn little_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; LittleEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { LittleEndian::$read(&bytes, &mut got); } + unsafe { + LittleEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3288,24 +3992,24 @@ mod stdtests { fn native_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; NativeEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { NativeEndian::$read(&bytes, &mut got); } + unsafe { + NativeEndian::$read(&bytes, &mut got); + } numbers == got } qc_unsized(prop as fn(_) -> bool); } } - } + }; } qc_slice!(prop_slice_u16, u16, read_u16_into, write_u16_into, 0); @@ -3314,15 +4018,21 @@ mod stdtests { qc_slice!(prop_slice_i32, i32, read_i32_into, write_i32_into, 0); qc_slice!(prop_slice_u64, u64, read_u64_into, write_u64_into, 0); qc_slice!(prop_slice_i64, i64, read_i64_into, write_i64_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_u128, Wi128, read_u128_into, write_u128_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_i128, Wi128, read_i128_into, write_i128_into, 0); - qc_slice!( - prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + prop_slice_u128, + Wi128, + read_u128_into, + write_u128_into, + 0 + ); qc_slice!( - prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); + prop_slice_i128, + Wi128, + read_i128_into, + write_i128_into, + 0 + ); + + qc_slice!(prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + qc_slice!(prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); } -- cgit v1.2.3 From 26669abe9cec373cda35d16f9dc94cca88d40226 Mon Sep 17 00:00:00 2001 From: Yi Kong Date: Sun, 14 Feb 2021 22:04:08 +0800 Subject: Update to 1.4.2 Change-Id: I9b660b3276276d55040849a3cd6e5b70dbcd513b Merged-In: I9b660b3276276d55040849a3cd6e5b70dbcd513b (cherry picked from commit 6d7927fcf9f2566d106f4d23fa0d29bd830b9bb7) --- .cargo_vcs_info.json | 2 +- .github/workflows/ci.yml | 151 ++++ Android.bp | 10 +- CHANGELOG.md | 25 + Cargo.toml | 15 +- Cargo.toml.orig | 14 +- LICENSE | 1 + METADATA | 19 + MODULE_LICENSE_MIT | 0 OWNERS | 1 + README.md | 6 +- benches/bench.rs | 236 +++--- build.rs | 87 --- rustfmt.toml | 2 + src/io.rs | 92 +-- src/lib.rs | 1794 ++++++++++++++++++++++++++++++++-------------- 16 files changed, 1623 insertions(+), 832 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 120000 LICENSE create mode 100644 METADATA create mode 100644 MODULE_LICENSE_MIT create mode 100644 OWNERS delete mode 100644 build.rs create mode 100644 rustfmt.toml diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index e7e8b81..e260c24 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "058237661a88c2f610f280340310fce19d98d265" + "sha1": "ca8c10a3f85e2c66781f7d12e90196e6923592d7" } } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2c2c790 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,151 @@ +name: ci +on: + pull_request: + push: + branches: + - master + schedule: + - cron: '00 01 * * *' +jobs: + test: + name: test + env: + # For some builds, we use cross to test on 32-bit and big-endian + # systems. + CARGO: cargo + # When CARGO is set to CROSS, TARGET is set to `--target matrix.target`. + TARGET: + runs-on: ${{ matrix.os }} + strategy: + matrix: + build: + - pinned + - stable + - stable-32 + - stable-mips + - beta + - nightly + - macos + - win-msvc + - win-gnu + include: + - build: pinned + os: ubuntu-18.04 + rust: 1.41.1 + - build: stable + os: ubuntu-18.04 + rust: stable + - build: stable-32 + os: ubuntu-18.04 + rust: stable + target: i686-unknown-linux-gnu + - build: stable-mips + os: ubuntu-18.04 + rust: stable + target: mips64-unknown-linux-gnuabi64 + - build: beta + os: ubuntu-18.04 + rust: beta + - build: nightly + os: ubuntu-18.04 + rust: nightly + - build: macos + os: macos-latest + rust: stable + - build: win-msvc + os: windows-2019 + rust: stable + - build: win-gnu + os: windows-2019 + rust: stable-x86_64-gnu + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + profile: minimal + override: true + + - name: Use Cross + if: matrix.target != '' + run: | + # FIXME: to work around bugs in latest cross release, install master. + # See: https://github.com/rust-embedded/cross/issues/357 + cargo install --git https://github.com/rust-embedded/cross + echo "CARGO=cross" >> $GITHUB_ENV + echo "TARGET=--target ${{ matrix.target }}" >> $GITHUB_ENV + + - name: Show command used for Cargo + run: | + echo "cargo command is: ${{ env.CARGO }}" + echo "target flag is: ${{ env.TARGET }}" + + - name: Show CPU info for debugging + if: matrix.os == 'ubuntu-18.04' + run: lscpu + + - name: Build + run: ${{ env.CARGO }} build --verbose $TARGET + + - name: Build (no default) + run: ${{ env.CARGO }} build --verbose $TARGET --no-default-features + + - name: Build docs + run: ${{ env.CARGO }} doc --verbose $TARGET + + # Our dev dependencies evolve more rapidly than we'd like, so only run + # tests when we aren't pinning the Rust version. + - name: Tests + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose $TARGET + + - name: Tests (no default, lib only) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --lib $TARGET + + - name: Tests (i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --features i128 $TARGET + + - name: Tests (no default, lib only, i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --features i128 --lib $TARGET + + - name: Compile benchmarks + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run $TARGET + + - name: Compile benchmarks (no default) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features $TARGET + + - name: Compile benchmarks (i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --features i128 $TARGET + + - name: Compile benchmarks (no default, i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features --features i128 $TARGET + + rustfmt: + name: rustfmt + runs-on: ubuntu-18.04 + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + profile: minimal + components: rustfmt + - name: Check formatting + run: cargo fmt -- --check diff --git a/Android.bp b/Android.bp index 72ef217..8f42c1a 100644 --- a/Android.bp +++ b/Android.bp @@ -1,17 +1,13 @@ -// This file is generated by cargo2android.py. +// This file is generated by cargo2android.py --run --device --dependencies. -rust_library_rlib { +rust_library { name: "libbyteorder", - deny_warnings: false, host_supported: true, crate_name: "byteorder", srcs: ["src/lib.rs"], - edition: "2015", + edition: "2018", features: [ "default", "std", ], - flags: [ - "--cfg byteorder_i128", - ], } diff --git a/CHANGELOG.md b/CHANGELOG.md index 020beb4..6b51eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,28 @@ +1.3.4 +===== +This patch release squashes deprecation warnings for the `try!` macro, in +accordance with byteorder's minimum supported Rust version (currently at Rust +1.12.0). + + +1.3.3 +===== +This patch release adds `ByteOrder::write_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.2 +===== +This patch release adds `ReadBytesExt::read_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.1 +===== +This minor release performs mostly small internal changes. Going forward, these +are not going to be incorporated into the changelog. + + 1.3.0 ===== This new minor release now enables `i128` support automatically on Rust diff --git a/Cargo.toml b/Cargo.toml index fb2acad..ff60b60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,17 +11,17 @@ # will likely look very different (and much more reasonable) [package] +edition = "2018" name = "byteorder" -version = "1.3.2" +version = "1.4.2" authors = ["Andrew Gallant "] -build = "build.rs" exclude = ["/ci/*"] description = "Library for reading/writing numbers in big-endian and little-endian." homepage = "https://github.com/BurntSushi/byteorder" documentation = "https://docs.rs/byteorder" readme = "README.md" keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] license = "Unlicense OR MIT" repository = "https://github.com/BurntSushi/byteorder" [profile.bench] @@ -30,19 +30,14 @@ opt-level = 3 [lib] name = "byteorder" bench = false -[dev-dependencies.doc-comment] -version = "0.3" - [dev-dependencies.quickcheck] -version = "0.8" +version = "0.9.2" default-features = false [dev-dependencies.rand] -version = "0.6" +version = "0.7" [features] default = ["std"] i128 = [] std = [] -[badges.travis-ci] -repository = "BurntSushi/byteorder" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 91f2aa3..efeafb2 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,26 +1,25 @@ [package] name = "byteorder" -version = "1.3.2" #:version +version = "1.4.2" #:version authors = ["Andrew Gallant "] description = "Library for reading/writing numbers in big-endian and little-endian." documentation = "https://docs.rs/byteorder" homepage = "https://github.com/BurntSushi/byteorder" repository = "https://github.com/BurntSushi/byteorder" readme = "README.md" -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] license = "Unlicense OR MIT" exclude = ["/ci/*"] -build = "build.rs" +edition = "2018" [lib] name = "byteorder" bench = false [dev-dependencies] -quickcheck = { version = "0.8", default-features = false } -rand = "0.6" -doc-comment = "0.3" +quickcheck = { version = "0.9.2", default-features = false } +rand = "0.7" [features] default = ["std"] @@ -33,6 +32,3 @@ i128 = [] [profile.bench] opt-level = 3 - -[badges] -travis-ci = { repository = "BurntSushi/byteorder" } diff --git a/LICENSE b/LICENSE new file mode 120000 index 0000000..7f9a88e --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +LICENSE-MIT \ No newline at end of file diff --git a/METADATA b/METADATA new file mode 100644 index 0000000..95a62dd --- /dev/null +++ b/METADATA @@ -0,0 +1,19 @@ +name: "byteorder" +description: "Library for reading/writing numbers in big-endian and little-endian." +third_party { + url { + type: HOMEPAGE + value: "https://crates.io/crates/byteorder" + } + url { + type: ARCHIVE + value: "https://static.crates.io/crates/byteorder/byteorder-1.4.2.crate" + } + version: "1.4.2" + license_type: NOTICE + last_upgrade_date { + year: 2021 + month: 2 + day: 7 + } +} diff --git a/MODULE_LICENSE_MIT b/MODULE_LICENSE_MIT new file mode 100644 index 0000000..e69de29 diff --git a/OWNERS b/OWNERS new file mode 100644 index 0000000..46fc303 --- /dev/null +++ b/OWNERS @@ -0,0 +1 @@ +include platform/prebuilts/rust:/OWNERS diff --git a/README.md b/README.md index 8940b29..8b2ecc4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ +byteorder +========= This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order. -[![Build status](https://api.travis-ci.org/BurntSushi/byteorder.svg)](https://travis-ci.org/BurntSushi/byteorder) +[![Build status](https://github.com/BurntSushi/byteorder/workflows/ci/badge.svg)](https://github.com/BurntSushi/byteorder/actions) [![](http://meritbadge.herokuapp.com/byteorder)](https://crates.io/crates/byteorder) Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org). @@ -27,8 +29,6 @@ If you want to augment existing `Read` and `Write` traits, then import the extension methods like so: ```rust -extern crate byteorder; - use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian, LittleEndian}; ``` diff --git a/benches/bench.rs b/benches/bench.rs index d53d25e..bb00422 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,15 +1,15 @@ #![feature(test)] -extern crate byteorder; -extern crate rand; extern crate test; macro_rules! bench_num { - ($name:ident, $read:ident, $bytes:expr, $data:expr) => ( + ($name:ident, $read:ident, $bytes:expr, $data:expr) => { mod $name { - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -43,14 +43,16 @@ macro_rules! bench_num { }); } } - ); + }; ($ty:ident, $max:ident, - $read:ident, $write:ident, $size:expr, $data:expr) => ( + $read:ident, $write:ident, $size:expr, $data:expr) => { mod $ty { + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use std::$ty; - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -117,7 +119,7 @@ macro_rules! bench_num { }); } } - ); + }; } bench_num!(u16, MAX, read_u16, write_u16, 2, [1, 2]); @@ -127,8 +129,7 @@ bench_num!(i32, MAX, read_i32, write_i32, 4, [1, 2, 3, 4]); bench_num!(u64, MAX, read_u64, write_u64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(i64, MAX, read_i64, write_i64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(f32, MAX, read_f32, write_f32, 4, [1, 2, 3, 4]); -bench_num!(f64, MAX, read_f64, write_f64, 8, - [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(f64, MAX, read_f64, write_f64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(uint_1, read_uint, 1, [1]); bench_num!(uint_2, read_uint, 2, [1, 2]); @@ -148,120 +149,115 @@ bench_num!(int_6, read_int, 6, [1, 2, 3, 4, 5, 6]); bench_num!(int_7, read_int, 7, [1, 2, 3, 4, 5, 6, 7]); bench_num!(int_8, read_int, 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(u128, MAX, read_u128, write_u128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); -#[cfg(byteorder_i128)] -bench_num!(i128, MAX, read_i128, write_i128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); - -#[cfg(byteorder_i128)] -bench_num!(uint128_1, read_uint128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(uint128_2, read_uint128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(uint128_3, read_uint128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(uint128_4, read_uint128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(uint128_5, read_uint128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(uint128_6, read_uint128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(uint128_7, read_uint128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(uint128_8, read_uint128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(uint128_9, read_uint128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(uint128_10, read_uint128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(uint128_11, read_uint128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(uint128_12, read_uint128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(uint128_13, read_uint128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(uint128_14, read_uint128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(uint128_15, read_uint128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(uint128_16, read_uint128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!( + u128, + MAX, + read_u128, + write_u128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!( + i128, + MAX, + read_i128, + write_i128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); -#[cfg(byteorder_i128)] -bench_num!(int128_1, read_int128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(int128_2, read_int128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(int128_3, read_int128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(int128_4, read_int128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(int128_5, read_int128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(int128_6, read_int128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(int128_7, read_int128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(int128_8, read_int128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(int128_9, read_int128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(int128_10, read_int128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(int128_11, read_int128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(int128_12, read_int128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(int128_13, read_int128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(int128_14, read_int128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(int128_15, read_int128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(int128_16, read_int128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!(uint128_1, read_uint128, 1, [1]); +bench_num!(uint128_2, read_uint128, 2, [1, 2]); +bench_num!(uint128_3, read_uint128, 3, [1, 2, 3]); +bench_num!(uint128_4, read_uint128, 4, [1, 2, 3, 4]); +bench_num!(uint128_5, read_uint128, 5, [1, 2, 3, 4, 5]); +bench_num!(uint128_6, read_uint128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(uint128_7, read_uint128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(uint128_8, read_uint128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(uint128_9, read_uint128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(uint128_10, read_uint128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(uint128_11, read_uint128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + uint128_12, + read_uint128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + uint128_13, + read_uint128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + uint128_14, + read_uint128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + uint128_15, + read_uint128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + uint128_16, + read_uint128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!(int128_1, read_int128, 1, [1]); +bench_num!(int128_2, read_int128, 2, [1, 2]); +bench_num!(int128_3, read_int128, 3, [1, 2, 3]); +bench_num!(int128_4, read_int128, 4, [1, 2, 3, 4]); +bench_num!(int128_5, read_int128, 5, [1, 2, 3, 4, 5]); +bench_num!(int128_6, read_int128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(int128_7, read_int128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(int128_8, read_int128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(int128_9, read_int128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(int128_10, read_int128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(int128_11, read_int128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + int128_12, + read_int128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + int128_13, + read_int128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + int128_14, + read_int128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + int128_15, + read_int128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + int128_16, + read_int128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); macro_rules! bench_slice { ($name:ident, $numty:ty, $read:ident, $write:ident) => { mod $name { use std::mem::size_of; - use byteorder::{ByteOrder, BigEndian, LittleEndian}; - use rand::{self, Rng}; + use byteorder::{BigEndian, ByteOrder, LittleEndian}; use rand::distributions; + use rand::{self, Rng}; use test::Bencher; #[bench] @@ -322,7 +318,7 @@ macro_rules! bench_slice { }); } } - } + }; } bench_slice!(slice_u64, u64, read_u64_into, write_u64_into); diff --git a/build.rs b/build.rs deleted file mode 100644 index 002135b..0000000 --- a/build.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::env; -use std::ffi::OsString; -use std::io::{self, Write}; -use std::process::Command; - -fn main() { - let version = match Version::read() { - Ok(version) => version, - Err(err) => { - writeln!( - &mut io::stderr(), - "failed to parse `rustc --version`: {}", - err - ).unwrap(); - return; - } - }; - enable_i128(version); -} - -fn enable_i128(version: Version) { - if version < (Version { major: 1, minor: 26, patch: 0 }) { - return; - } - - println!("cargo:rustc-cfg=byteorder_i128"); -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] -struct Version { - major: u32, - minor: u32, - patch: u32, -} - -impl Version { - fn read() -> Result { - let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); - let output = Command::new(&rustc) - .arg("--version") - .output() - .unwrap() - .stdout; - Version::parse(&String::from_utf8(output).unwrap()) - } - - fn parse(mut s: &str) -> Result { - if !s.starts_with("rustc ") { - return Err(format!("unrecognized version string: {}", s)); - } - s = &s["rustc ".len()..]; - - let parts: Vec<&str> = s.split(".").collect(); - if parts.len() < 3 { - return Err(format!("not enough version parts: {:?}", parts)); - } - - let mut num = String::new(); - for c in parts[0].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let major = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[1].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let minor = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[2].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let patch = try!(num.parse::().map_err(|e| e.to_string())); - - Ok(Version { major: major, minor: minor, patch: patch }) - } -} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..aa37a21 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 79 +use_small_heuristics = "max" diff --git a/src/io.rs b/src/io.rs index ed6a848..ac41ff7 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,7 +1,9 @@ -use std::io::{self, Result}; -use std::slice; +use std::{ + io::{self, Result}, + slice, +}; -use ByteOrder; +use crate::ByteOrder; /// Extends [`Read`] with methods for reading numbers. (For `std::io`.) /// @@ -52,7 +54,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0]) } @@ -82,7 +84,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0] as i8) } @@ -109,7 +111,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u16(&buf)) } @@ -136,7 +138,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i16(&buf)) } @@ -162,7 +164,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u24(&buf)) } @@ -188,7 +190,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i24(&buf)) } @@ -214,7 +216,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u32(&buf)) } @@ -240,7 +242,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i32(&buf)) } @@ -266,7 +268,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u48(&buf)) } @@ -292,7 +294,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i48(&buf)) } @@ -318,7 +320,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u64(&buf)) } @@ -344,7 +346,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i64(&buf)) } @@ -370,11 +372,10 @@ pub trait ReadBytesExt: io::Read { /// ]); /// assert_eq!(16947640962301618749969007319746179, rdr.read_u128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u128(&buf)) } @@ -397,11 +398,10 @@ pub trait ReadBytesExt: io::Read { /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); /// assert_eq!(i128::min_value(), rdr.read_i128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i128(&buf)) } @@ -426,7 +426,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_uint(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint(&buf[..nbytes], nbytes)) } @@ -451,25 +451,23 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_int(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int(&buf[..nbytes], nbytes)) } /// Reads an unsigned n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_uint128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint128(&buf[..nbytes], nbytes)) } /// Reads a signed n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_int128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int128(&buf[..nbytes], nbytes)) } @@ -500,7 +498,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f32(&buf)) } @@ -531,7 +529,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f64(&buf)) } @@ -564,7 +562,7 @@ pub trait ReadBytesExt: io::Read { fn read_u16_into(&mut self, dst: &mut [u16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u16(dst); Ok(()) @@ -599,7 +597,7 @@ pub trait ReadBytesExt: io::Read { fn read_u32_into(&mut self, dst: &mut [u32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u32(dst); Ok(()) @@ -637,7 +635,7 @@ pub trait ReadBytesExt: io::Read { fn read_u64_into(&mut self, dst: &mut [u64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u64(dst); Ok(()) @@ -671,7 +669,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_u128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128_into( &mut self, @@ -679,7 +676,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u128(dst); Ok(()) @@ -750,7 +747,7 @@ pub trait ReadBytesExt: io::Read { fn read_i16_into(&mut self, dst: &mut [i16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i16(dst); Ok(()) @@ -785,7 +782,7 @@ pub trait ReadBytesExt: io::Read { fn read_i32_into(&mut self, dst: &mut [i32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i32(dst); Ok(()) @@ -823,7 +820,7 @@ pub trait ReadBytesExt: io::Read { fn read_i64_into(&mut self, dst: &mut [i64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i64(dst); Ok(()) @@ -857,7 +854,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_i128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into( &mut self, @@ -865,7 +861,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i128(dst); Ok(()) @@ -903,13 +899,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f32_into( - &mut self, - dst: &mut [f32], - ) -> Result<()> { + fn read_f32_into(&mut self, dst: &mut [f32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f32(dst); Ok(()) @@ -951,7 +944,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked( &mut self, dst: &mut [f32], @@ -991,13 +984,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f64_into( - &mut self, - dst: &mut [f64], - ) -> Result<()> { + fn read_f64_into(&mut self, dst: &mut [f64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f64(dst); Ok(()) @@ -1045,7 +1035,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked( &mut self, dst: &mut [f64], @@ -1408,7 +1398,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes an unsigned 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_u128(&mut self, n: u128) -> Result<()> { let mut buf = [0; 16]; @@ -1417,7 +1406,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes a signed 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_i128(&mut self, n: i128) -> Result<()> { let mut buf = [0; 16]; @@ -1501,7 +1489,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_uint128( &mut self, @@ -1517,7 +1504,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_int128( &mut self, diff --git a/src/lib.rs b/src/lib.rs index db4d24d..d56574d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,23 +70,12 @@ cases. #![deny(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "std")] -extern crate core; - -#[cfg(test)] -#[macro_use] -extern crate doc_comment; - -#[cfg(test)] -doctest!("../README.md"); - -use core::fmt::Debug; -use core::hash::Hash; -use core::ptr::copy_nonoverlapping; -use core::slice; +use core::{ + convert::TryInto, fmt::Debug, hash::Hash, ptr::copy_nonoverlapping, slice, +}; #[cfg(feature = "std")] -pub use io::{ReadBytesExt, WriteBytesExt}; +pub use crate::io::{ReadBytesExt, WriteBytesExt}; #[cfg(feature = "std")] mod io; @@ -97,7 +86,6 @@ fn extend_sign(val: u64, nbytes: usize) -> i64 { (val << shift) as i64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn extend_sign128(val: u128, nbytes: usize) -> i128 { let shift = (16 - nbytes) * 8; @@ -110,7 +98,6 @@ fn unextend_sign(val: i64, nbytes: usize) -> u64 { (val << shift) as u64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn unextend_sign128(val: i128, nbytes: usize) -> u128 { let shift = (16 - nbytes) * 8; @@ -138,7 +125,6 @@ fn pack_size(n: u64) -> usize { } } -#[cfg(byteorder_i128)] #[inline] fn pack_size128(n: u128) -> usize { if n < 1 << 8 { @@ -179,7 +165,7 @@ fn pack_size128(n: u128) -> usize { mod private { /// Sealed stops crates other than byteorder from implementing any traits /// that use it. - pub trait Sealed{} + pub trait Sealed {} impl Sealed for super::LittleEndian {} impl Sealed for super::BigEndian {} } @@ -219,8 +205,16 @@ mod private { /// /// [`BigEndian`]: enum.BigEndian.html /// [`LittleEndian`]: enum.LittleEndian.html -pub trait ByteOrder - : Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd +pub trait ByteOrder: + Clone + + Copy + + Debug + + Default + + Eq + + Hash + + Ord + + PartialEq + + PartialOrd + private::Sealed { /// Reads an unsigned 16 bit integer from `buf`. @@ -327,7 +321,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn read_u128(buf: &[u8]) -> u128; /// Reads an unsigned n-bytes integer from `buf`. @@ -368,7 +361,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn read_uint128(buf: &[u8], nbytes: usize) -> u128; /// Writes an unsigned 16 bit integer `n` to `buf`. @@ -487,7 +479,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn write_u128(buf: &mut [u8], n: u128); /// Writes an unsigned integer `n` to `buf` using only `nbytes`. @@ -528,7 +519,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize); /// Reads a signed 16 bit integer from `buf`. @@ -658,7 +648,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(buf: &[u8]) -> i128 { Self::read_u128(buf) as i128 @@ -705,7 +694,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_int128(buf: &[u8], nbytes: usize) -> i128 { extend_sign128(Self::read_uint128(buf, nbytes), nbytes) @@ -731,7 +719,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f32(buf: &[u8]) -> f32 { - unsafe { *(&Self::read_u32(buf) as *const u32 as *const f32) } + f32::from_bits(Self::read_u32(buf)) } /// Reads a IEEE754 double-precision (8 bytes) floating point number. @@ -754,7 +742,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f64(buf: &[u8]) -> f64 { - unsafe { *(&Self::read_u64(buf) as *const u64 as *const f64) } + f64::from_bits(Self::read_u64(buf)) } /// Writes a signed 16 bit integer `n` to `buf`. @@ -884,7 +872,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_i128(buf: &mut [u8], n: i128) { Self::write_u128(buf, n as u128) @@ -931,7 +918,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_int128(buf: &mut [u8], n: i128, nbytes: usize) { Self::write_uint128(buf, unextend_sign128(n, nbytes), nbytes) @@ -957,8 +943,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f32(buf: &mut [u8], n: f32) { - let n = unsafe { *(&n as *const f32 as *const u32) }; - Self::write_u32(buf, n) + Self::write_u32(buf, n.to_bits()) } /// Writes a IEEE754 double-precision (8 bytes) floating point number. @@ -981,8 +966,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f64(buf: &mut [u8], n: f64) { - let n = unsafe { *(&n as *const f64 as *const u64) }; - Self::write_u64(buf, n) + Self::write_u64(buf, n.to_bits()) } /// Reads unsigned 16 bit integers from `src` into `dst`. @@ -1075,7 +1059,6 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn read_u128_into(src: &[u8], dst: &mut [u128]); /// Reads signed 16 bit integers from `src` to `dst`. @@ -1186,7 +1169,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into(src: &[u8], dst: &mut [i128]) { let dst = unsafe { @@ -1251,7 +1233,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) { Self::read_f32_into(src, dst); } @@ -1313,7 +1295,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) { Self::read_f64_into(src, dst); } @@ -1408,9 +1390,42 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_u128_into(src: &[u128], dst: &mut [u8]); + /// Writes signed 8 bit integers from `src` into `dst`. + /// + /// Note that since each `i8` is a single byte, no byte order conversions + /// are used. This method is included because it provides a safe, simple + /// way for the caller to write from a `&[i8]` buffer. (Without this + /// method, the caller would have to either use `unsafe` code or convert + /// each byte to `u8` individually.) + /// + /// # Panics + /// + /// Panics when `buf.len() != src.len()`. + /// + /// # Examples + /// + /// Write and read `i8` numbers in little endian order: + /// + /// ```rust + /// use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; + /// + /// let mut bytes = [0; 4]; + /// let numbers_given = [1, 2, 0xf, 0xe]; + /// LittleEndian::write_i8_into(&numbers_given, &mut bytes); + /// + /// let mut numbers_got = [0; 4]; + /// bytes.as_ref().read_i8_into(&mut numbers_got); + /// assert_eq!(numbers_given, numbers_got); + /// ``` + fn write_i8_into(src: &[i8], dst: &mut [u8]) { + let src = unsafe { + slice::from_raw_parts(src.as_ptr() as *const u8, src.len()) + }; + dst.copy_from_slice(src); + } + /// Writes signed 16 bit integers from `src` into `dst`. /// /// # Panics @@ -1516,7 +1531,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_i128_into(src: &[i128], dst: &mut [u8]) { let src = unsafe { slice::from_raw_parts(src.as_ptr() as *const u128, src.len()) @@ -1660,7 +1674,6 @@ pub trait ByteOrder /// BigEndian::from_slice_u128(&mut numbers); /// assert_eq!(numbers, [5u128.to_be(), 65000u128.to_be()]); /// ``` - #[cfg(byteorder_i128)] fn from_slice_u128(numbers: &mut [u128]); /// Converts the given slice of signed 16 bit integers to a particular @@ -1755,7 +1768,6 @@ pub trait ByteOrder /// BigEndian::from_slice_i128(&mut numbers); /// assert_eq!(numbers, [5i128.to_be(), 65000i128.to_be()]); /// ``` - #[cfg(byteorder_i128)] #[inline] fn from_slice_i128(src: &mut [i128]) { let src = unsafe { @@ -1887,30 +1899,15 @@ pub type NativeEndian = LittleEndian; #[cfg(target_endian = "big")] pub type NativeEndian = BigEndian; -macro_rules! read_num_bytes { - ($ty:ty, $size:expr, $src:expr, $which:ident) => ({ - assert!($size == ::core::mem::size_of::<$ty>()); - assert!($size <= $src.len()); - let mut data: $ty = 0; - unsafe { - copy_nonoverlapping( - $src.as_ptr(), - &mut data as *mut $ty as *mut u8, - $size); - } - data.$which() - }); -} - macro_rules! write_num_bytes { - ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => ({ + ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => {{ assert!($size <= $dst.len()); unsafe { // N.B. https://github.com/rust-lang/rust/issues/22776 let bytes = *(&$n.$which() as *const _ as *const [u8; $size]); copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size); } - }); + }}; } macro_rules! read_slice { @@ -1921,7 +1918,8 @@ macro_rules! read_slice { copy_nonoverlapping( $src.as_ptr(), $dst.as_mut_ptr() as *mut u8, - $src.len()); + $src.len(), + ); } for v in $dst.iter_mut() { *v = v.$which(); @@ -1938,67 +1936,72 @@ macro_rules! write_slice_native { copy_nonoverlapping( $src.as_ptr() as *const u8, $dst.as_mut_ptr(), - $dst.len()); + $dst.len(), + ); } }}; } macro_rules! write_slice { - ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => ({ + ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => {{ assert!($size == ::core::mem::size_of::<$ty>()); assert_eq!($size * $src.len(), $dst.len()); for (&n, chunk) in $src.iter().zip($dst.chunks_mut($size)) { $write(chunk, n); } - }); + }}; } impl ByteOrder for BigEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_be) + u16::from_be_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_be) + u32::from_be_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_be) + u64::from_be_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_be) + u128::from_be_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((8 - nbytes) as isize), nbytes); - (*(ptr_out as *const u64)).to_be() + buf.as_ptr(), + ptr_out.offset((8 - nbytes) as isize), + nbytes, + ); } + out.to_be() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((16 - nbytes) as isize), nbytes); - (*(ptr_out as *const u128)).to_be() + buf.as_ptr(), + ptr_out.offset((16 - nbytes) as isize), + nbytes, + ); } + out.to_be() } #[inline] @@ -2016,7 +2019,6 @@ impl ByteOrder for BigEndian { write_num_bytes!(u64, 8, n, buf, to_be); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_be); @@ -2031,11 +2033,11 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((8 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n) <= nbytes && nbytes <= 16); @@ -2045,7 +2047,8 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((16 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } @@ -2064,7 +2067,6 @@ impl ByteOrder for BigEndian { read_slice!(src, dst, 8, to_be); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_be); @@ -2097,7 +2099,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "big") { @@ -2134,7 +2135,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "little") { @@ -2172,46 +2172,44 @@ impl ByteOrder for BigEndian { impl ByteOrder for LittleEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_le) + u16::from_le_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_le) + u32::from_le_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_le) + u64::from_le_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_le) + u128::from_le_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u64)).to_le() } + out.to_le() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u128)).to_le() } + out.to_le() } #[inline] @@ -2229,7 +2227,6 @@ impl ByteOrder for LittleEndian { write_num_bytes!(u64, 8, n, buf, to_le); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_le); @@ -2245,7 +2242,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n as u128) <= nbytes && nbytes <= 16); @@ -2271,7 +2267,6 @@ impl ByteOrder for LittleEndian { read_slice!(src, dst, 8, to_le); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_le); @@ -2304,7 +2299,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "little") { @@ -2341,7 +2335,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "big") { @@ -2378,15 +2371,8 @@ impl ByteOrder for LittleEndian { #[cfg(test)] mod test { - extern crate quickcheck; - extern crate rand; - - use self::quickcheck::{QuickCheck, StdGen, Testable}; - use self::rand::thread_rng; - #[cfg(byteorder_i128)] - use self::rand::Rng; - #[cfg(byteorder_i128)] - use self::quickcheck::{Arbitrary, Gen}; + use quickcheck::{Arbitrary, Gen, QuickCheck, StdGen, Testable}; + use rand::{thread_rng, Rng}; pub const U24_MAX: u32 = 16_777_215; pub const I24_MAX: i32 = 8_388_607; @@ -2397,7 +2383,9 @@ mod test { pub const I64_MAX: u64 = ::core::i64::MAX as u64; macro_rules! calc_max { - ($max:expr, $bytes:expr) => { calc_max!($max, $bytes, 8) }; + ($max:expr, $bytes:expr) => { + calc_max!($max, $bytes, 8) + }; ($max:expr, $bytes:expr, $maxbytes:expr) => { ($max - 1) >> (8 * ($maxbytes - $bytes)) }; @@ -2406,7 +2394,6 @@ mod test { #[derive(Clone, Debug)] pub struct Wi128(pub T); - #[cfg(byteorder_i128)] impl Wi128 { pub fn clone(&self) -> T { self.0.clone() @@ -2419,24 +2406,20 @@ mod test { } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::u128::MAX, gen.size(), 16); - let output = - (gen.gen::() as u128) | - ((gen.gen::() as u128) << 64); + let output = (gen.gen::() as u128) + | ((gen.gen::() as u128) << 64); Wi128(output & (max - 1)) } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::i128::MAX, gen.size(), 16); - let output = - (gen.gen::() as i128) | - ((gen.gen::() as i128) << 64); + let output = (gen.gen::() as i128) + | ((gen.gen::() as i128) << 64); Wi128(output & (max - 1)) } } @@ -2451,17 +2434,20 @@ mod test { macro_rules! qc_byte_order { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] fn big_endian() { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; BigEndian::$write(&mut buf, n.clone(), $bytes); - n == BigEndian::$read(&mut buf[..$bytes], $bytes) + n == BigEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2471,7 +2457,7 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; LittleEndian::$write(&mut buf, n.clone(), $bytes); - n == LittleEndian::$read(&mut buf[..$bytes], $bytes) + n == LittleEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2481,18 +2467,21 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; NativeEndian::$write(&mut buf, n.clone(), $bytes); - n == NativeEndian::$read(&mut buf[..$bytes], $bytes) + n == NativeEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); + }; ($name:ident, $ty_int:ty, $max:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use core::mem::size_of; - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; #[test] fn big_endian() { @@ -2500,7 +2489,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; BigEndian::$write(&mut buf[16 - bytes..], n.clone()); - n == BigEndian::$read(&mut buf[16 - bytes..]) + n == BigEndian::$read(&buf[16 - bytes..]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2511,7 +2500,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; LittleEndian::$write(&mut buf[..bytes], n.clone()); - n == LittleEndian::$read(&mut buf[..bytes]) + n == LittleEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2522,164 +2511,489 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; NativeEndian::$write(&mut buf[..bytes], n.clone()); - n == NativeEndian::$read(&mut buf[..bytes]) + n == NativeEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_byte_order!(prop_u16, u16, ::core::u16::MAX as u64, read_u16, write_u16); - qc_byte_order!(prop_i16, i16, ::core::i16::MAX as u64, read_i16, write_i16); - qc_byte_order!(prop_u24, u32, ::test::U24_MAX as u64, read_u24, write_u24); - qc_byte_order!(prop_i24, i32, ::test::I24_MAX as u64, read_i24, write_i24); - qc_byte_order!(prop_u32, u32, ::core::u32::MAX as u64, read_u32, write_u32); - qc_byte_order!(prop_i32, i32, ::core::i32::MAX as u64, read_i32, write_i32); - qc_byte_order!(prop_u48, u64, ::test::U48_MAX as u64, read_u48, write_u48); - qc_byte_order!(prop_i48, i64, ::test::I48_MAX as u64, read_i48, write_i48); - qc_byte_order!(prop_u64, u64, ::core::u64::MAX as u64, read_u64, write_u64); - qc_byte_order!(prop_i64, i64, ::core::i64::MAX as u64, read_i64, write_i64); - qc_byte_order!(prop_f32, f32, ::core::u64::MAX as u64, read_f32, write_f32); - qc_byte_order!(prop_f64, f64, ::core::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_byte_order!( + prop_u16, + u16, + ::core::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_byte_order!( + prop_i16, + i16, + ::core::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_byte_order!( + prop_u24, + u32, + crate::test::U24_MAX as u64, + read_u24, + write_u24 + ); + qc_byte_order!( + prop_i24, + i32, + crate::test::I24_MAX as u64, + read_i24, + write_i24 + ); + qc_byte_order!( + prop_u32, + u32, + ::core::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_byte_order!( + prop_i32, + i32, + ::core::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_byte_order!( + prop_u48, + u64, + crate::test::U48_MAX as u64, + read_u48, + write_u48 + ); + qc_byte_order!( + prop_i48, + i64, + crate::test::I48_MAX as u64, + read_i48, + write_i48 + ); + qc_byte_order!( + prop_u64, + u64, + ::core::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_byte_order!( + prop_i64, + i64, + ::core::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_byte_order!( + prop_f32, + f32, + ::core::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_byte_order!( + prop_f64, + f64, + ::core::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_byte_order!(prop_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_byte_order!(prop_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_byte_order!(prop_uint_1, - u64, calc_max!(super::U64_MAX, 1), 1, read_uint, write_uint); - qc_byte_order!(prop_uint_2, - u64, calc_max!(super::U64_MAX, 2), 2, read_uint, write_uint); - qc_byte_order!(prop_uint_3, - u64, calc_max!(super::U64_MAX, 3), 3, read_uint, write_uint); - qc_byte_order!(prop_uint_4, - u64, calc_max!(super::U64_MAX, 4), 4, read_uint, write_uint); - qc_byte_order!(prop_uint_5, - u64, calc_max!(super::U64_MAX, 5), 5, read_uint, write_uint); - qc_byte_order!(prop_uint_6, - u64, calc_max!(super::U64_MAX, 6), 6, read_uint, write_uint); - qc_byte_order!(prop_uint_7, - u64, calc_max!(super::U64_MAX, 7), 7, read_uint, write_uint); - qc_byte_order!(prop_uint_8, - u64, calc_max!(super::U64_MAX, 8), 8, read_uint, write_uint); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_1, - Wi128, 1, 1, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_2, - Wi128, 2, 2, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_3, - Wi128, 3, 3, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_4, - Wi128, 4, 4, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_5, - Wi128, 5, 5, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_6, - Wi128, 6, 6, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_7, - Wi128, 7, 7, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_8, - Wi128, 8, 8, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_9, - Wi128, 9, 9, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_10, - Wi128, 10, 10, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_11, - Wi128, 11, 11, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_12, - Wi128, 12, 12, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_13, - Wi128, 13, 13, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_14, - Wi128, 14, 14, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_15, - Wi128, 15, 15, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_16, - Wi128, 16, 16, read_uint128, write_uint128); - - qc_byte_order!(prop_int_1, - i64, calc_max!(super::I64_MAX, 1), 1, read_int, write_int); - qc_byte_order!(prop_int_2, - i64, calc_max!(super::I64_MAX, 2), 2, read_int, write_int); - qc_byte_order!(prop_int_3, - i64, calc_max!(super::I64_MAX, 3), 3, read_int, write_int); - qc_byte_order!(prop_int_4, - i64, calc_max!(super::I64_MAX, 4), 4, read_int, write_int); - qc_byte_order!(prop_int_5, - i64, calc_max!(super::I64_MAX, 5), 5, read_int, write_int); - qc_byte_order!(prop_int_6, - i64, calc_max!(super::I64_MAX, 6), 6, read_int, write_int); - qc_byte_order!(prop_int_7, - i64, calc_max!(super::I64_MAX, 7), 7, read_int, write_int); - qc_byte_order!(prop_int_8, - i64, calc_max!(super::I64_MAX, 8), 8, read_int, write_int); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_1, - Wi128, 1, 1, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_2, - Wi128, 2, 2, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_3, - Wi128, 3, 3, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_4, - Wi128, 4, 4, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_5, - Wi128, 5, 5, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_6, - Wi128, 6, 6, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_7, - Wi128, 7, 7, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_8, - Wi128, 8, 8, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_9, - Wi128, 9, 9, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_10, - Wi128, 10, 10, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_11, - Wi128, 11, 11, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_12, - Wi128, 12, 12, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_13, - Wi128, 13, 13, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_14, - Wi128, 14, 14, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_15, - Wi128, 15, 15, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_16, - Wi128, 16, 16, read_int128, write_int128); - + qc_byte_order!( + prop_uint_1, + u64, + calc_max!(super::U64_MAX, 1), + 1, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_2, + u64, + calc_max!(super::U64_MAX, 2), + 2, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_3, + u64, + calc_max!(super::U64_MAX, 3), + 3, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_4, + u64, + calc_max!(super::U64_MAX, 4), + 4, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_5, + u64, + calc_max!(super::U64_MAX, 5), + 5, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_6, + u64, + calc_max!(super::U64_MAX, 6), + 6, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_7, + u64, + calc_max!(super::U64_MAX, 7), + 7, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_8, + u64, + calc_max!(super::U64_MAX, 8), + 8, + read_uint, + write_uint + ); + + qc_byte_order!( + prop_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_uint128 + ); + + qc_byte_order!( + prop_int_1, + i64, + calc_max!(super::I64_MAX, 1), + 1, + read_int, + write_int + ); + qc_byte_order!( + prop_int_2, + i64, + calc_max!(super::I64_MAX, 2), + 2, + read_int, + write_int + ); + qc_byte_order!( + prop_int_3, + i64, + calc_max!(super::I64_MAX, 3), + 3, + read_int, + write_int + ); + qc_byte_order!( + prop_int_4, + i64, + calc_max!(super::I64_MAX, 4), + 4, + read_int, + write_int + ); + qc_byte_order!( + prop_int_5, + i64, + calc_max!(super::I64_MAX, 5), + 5, + read_int, + write_int + ); + qc_byte_order!( + prop_int_6, + i64, + calc_max!(super::I64_MAX, 6), + 6, + read_int, + write_int + ); + qc_byte_order!( + prop_int_7, + i64, + calc_max!(super::I64_MAX, 7), + 7, + read_int, + write_int + ); + qc_byte_order!( + prop_int_8, + i64, + calc_max!(super::I64_MAX, 8), + 8, + read_int, + write_int + ); + + qc_byte_order!( + prop_int128_1, + Wi128, + 1, + 1, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_2, + Wi128, + 2, + 2, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_3, + Wi128, + 3, + 3, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_4, + Wi128, + 4, + 4, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_5, + Wi128, + 5, + 5, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_6, + Wi128, + 6, + 6, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_7, + Wi128, + 7, + 7, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_8, + Wi128, + 8, + 8, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_9, + Wi128, + 9, + 9, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_10, + Wi128, + 10, + 10, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_11, + Wi128, + 11, + 11, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_12, + Wi128, + 12, + 12, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_13, + Wi128, + 13, + 13, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_14, + Wi128, + 14, + 14, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_15, + Wi128, + 15, + 15, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_16, + Wi128, + 16, + 16, + read_int128, + write_int128 + ); // Test that all of the byte conversion functions panic when given a // buffer that is too small. @@ -2688,9 +3002,11 @@ mod test { // with a buffer overflow. macro_rules! too_small { ($name:ident, $maximally_small:expr, $zero:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2734,10 +3050,12 @@ mod test { NativeEndian::$write(&mut buf, $zero); } } - ); - ($name:ident, $maximally_small:expr, $read:ident) => ( + }; + ($name:ident, $maximally_small:expr, $read:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2760,7 +3078,7 @@ mod test { NativeEndian::$read(&buf, $maximally_small + 1); } } - ); + }; } too_small!(small_u16, 1, 0, read_u16, write_u16); @@ -2771,9 +3089,7 @@ mod test { too_small!(small_i64, 7, 0, read_i64, write_i64); too_small!(small_f32, 3, 0.0, read_f32, write_f32); too_small!(small_f64, 7, 0.0, read_f64, write_f64); - #[cfg(byteorder_i128)] too_small!(small_u128, 15, 0, read_u128, write_u128); - #[cfg(byteorder_i128)] too_small!(small_i128, 15, 0, read_i128, write_i128); too_small!(small_uint_1, 1, read_uint); @@ -2784,35 +3100,20 @@ mod test { too_small!(small_uint_6, 6, read_uint); too_small!(small_uint_7, 7, read_uint); - #[cfg(byteorder_i128)] too_small!(small_uint128_1, 1, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_2, 2, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_3, 3, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_4, 4, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_5, 5, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_6, 6, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_7, 7, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_8, 8, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_9, 9, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_10, 10, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_11, 11, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_12, 12, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_13, 13, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_14, 14, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_15, 15, read_uint128); too_small!(small_int_1, 1, read_int); @@ -2823,35 +3124,20 @@ mod test { too_small!(small_int_6, 6, read_int); too_small!(small_int_7, 7, read_int); - #[cfg(byteorder_i128)] too_small!(small_int128_1, 1, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_2, 2, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_3, 3, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_4, 4, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_5, 5, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_6, 6, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_7, 7, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_8, 8, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_9, 9, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_10, 10, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_11, 11, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_12, 12, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_13, 13, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_14, 14, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_15, 15, read_int128); // Test that reading/writing slices enforces the correct lengths. @@ -2859,7 +3145,9 @@ mod test { ($name:ident, $read:ident, $write:ident, $num_bytes:expr, $numbers:expr) => { mod $name { - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2909,54 +3197,171 @@ mod test { NativeEndian::$write(&numbers, &mut bytes); } } - } + }; } slice_lengths!( - slice_len_too_small_u16, read_u16_into, write_u16_into, 3, [0, 0]); + slice_len_too_small_u16, + read_u16_into, + write_u16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u16, read_u16_into, write_u16_into, 5, [0, 0]); + slice_len_too_big_u16, + read_u16_into, + write_u16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i16, read_i16_into, write_i16_into, 3, [0, 0]); + slice_len_too_small_i16, + read_i16_into, + write_i16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i16, read_i16_into, write_i16_into, 5, [0, 0]); + slice_len_too_big_i16, + read_i16_into, + write_i16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u32, read_u32_into, write_u32_into, 7, [0, 0]); + slice_len_too_small_u32, + read_u32_into, + write_u32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u32, read_u32_into, write_u32_into, 9, [0, 0]); + slice_len_too_big_u32, + read_u32_into, + write_u32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i32, read_i32_into, write_i32_into, 7, [0, 0]); + slice_len_too_small_i32, + read_i32_into, + write_i32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i32, read_i32_into, write_i32_into, 9, [0, 0]); + slice_len_too_big_i32, + read_i32_into, + write_i32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u64, read_u64_into, write_u64_into, 15, [0, 0]); + slice_len_too_small_u64, + read_u64_into, + write_u64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u64, read_u64_into, write_u64_into, 17, [0, 0]); + slice_len_too_big_u64, + read_u64_into, + write_u64_into, + 17, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i64, read_i64_into, write_i64_into, 15, [0, 0]); + slice_len_too_small_i64, + read_i64_into, + write_i64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i64, read_i64_into, write_i64_into, 17, [0, 0]); + slice_len_too_big_i64, + read_i64_into, + write_i64_into, + 17, + [0, 0] + ); - #[cfg(byteorder_i128)] slice_lengths!( - slice_len_too_small_u128, read_u128_into, write_u128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_u128, + read_u128_into, + write_u128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u128, read_u128_into, write_u128_into, 33, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_big_u128, + read_u128_into, + write_u128_into, + 33, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i128, read_i128_into, write_i128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_i128, + read_i128_into, + write_i128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i128, read_i128_into, write_i128_into, 33, [0, 0]); + slice_len_too_big_i128, + read_i128_into, + write_i128_into, + 33, + [0, 0] + ); #[test] fn uint_bigger_buffer() { - use {ByteOrder, LittleEndian}; + use crate::{ByteOrder, LittleEndian}; let n = LittleEndian::read_uint(&[1, 2, 3, 4, 5, 6, 7, 8], 5); - assert_eq!(n, 0x0504030201); + assert_eq!(n, 0x05_0403_0201); + } + + #[test] + fn regression173_array_impl() { + use crate::{BigEndian, ByteOrder, LittleEndian}; + + let xs = [0; 100]; + + let x = BigEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i128(&xs); + assert_eq!(x, 0); + + let x = LittleEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i128(&xs); + assert_eq!(x, 0); } } @@ -2970,7 +3375,6 @@ mod stdtests { use self::rand::thread_rng; fn qc_unsized(f: A) { - QuickCheck::new() .gen(StdGen::new(thread_rng(), 16)) .tests(1_00) @@ -2979,19 +3383,22 @@ mod stdtests { } macro_rules! calc_max { - ($max:expr, $bytes:expr) => { ($max - 1) >> (8 * (8 - $bytes)) }; + ($max:expr, $bytes:expr) => { + ($max - 1) >> (8 * (8 - $bytes)) + }; } macro_rules! qc_bytes_ext { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3032,15 +3439,16 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); - ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => ( + }; + ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3075,188 +3483,484 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_bytes_ext!(prop_ext_u16, - u16, ::std::u16::MAX as u64, read_u16, write_u16); - qc_bytes_ext!(prop_ext_i16, - i16, ::std::i16::MAX as u64, read_i16, write_i16); - qc_bytes_ext!(prop_ext_u32, - u32, ::std::u32::MAX as u64, read_u32, write_u32); - qc_bytes_ext!(prop_ext_i32, - i32, ::std::i32::MAX as u64, read_i32, write_i32); - qc_bytes_ext!(prop_ext_u64, - u64, ::std::u64::MAX as u64, read_u64, write_u64); - qc_bytes_ext!(prop_ext_i64, - i64, ::std::i64::MAX as u64, read_i64, write_i64); - qc_bytes_ext!(prop_ext_f32, - f32, ::std::u64::MAX as u64, read_f32, write_f32); - qc_bytes_ext!(prop_ext_f64, - f64, ::std::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_bytes_ext!( + prop_ext_u16, + u16, + ::std::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_bytes_ext!( + prop_ext_i16, + i16, + ::std::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_bytes_ext!( + prop_ext_u32, + u32, + ::std::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_bytes_ext!( + prop_ext_i32, + i32, + ::std::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_bytes_ext!( + prop_ext_u64, + u64, + ::std::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_bytes_ext!( + prop_ext_i64, + i64, + ::std::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_bytes_ext!( + prop_ext_f32, + f32, + ::std::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_bytes_ext!( + prop_ext_f64, + f64, + ::std::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_bytes_ext!(prop_ext_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_bytes_ext!(prop_ext_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_bytes_ext!(prop_ext_uint_1, - u64, calc_max!(::test::U64_MAX, 1), 1, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_2, - u64, calc_max!(::test::U64_MAX, 2), 2, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_3, - u64, calc_max!(::test::U64_MAX, 3), 3, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_4, - u64, calc_max!(::test::U64_MAX, 4), 4, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_5, - u64, calc_max!(::test::U64_MAX, 5), 5, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_6, - u64, calc_max!(::test::U64_MAX, 6), 6, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_7, - u64, calc_max!(::test::U64_MAX, 7), 7, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_8, - u64, calc_max!(::test::U64_MAX, 8), 8, read_uint, write_u64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_1, - Wi128, 1, 1, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_2, - Wi128, 2, 2, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_3, - Wi128, 3, 3, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_4, - Wi128, 4, 4, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_5, - Wi128, 5, 5, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_6, - Wi128, 6, 6, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_7, - Wi128, 7, 7, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_8, - Wi128, 8, 8, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_9, - Wi128, 9, 9, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_10, - Wi128, 10, 10, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_11, - Wi128, 11, 11, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_12, - Wi128, 12, 12, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_13, - Wi128, 13, 13, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_14, - Wi128, 14, 14, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_15, - Wi128, 15, 15, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_16, - Wi128, 16, 16, read_uint128, write_u128); - - qc_bytes_ext!(prop_ext_int_1, - i64, calc_max!(::test::I64_MAX, 1), 1, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_2, - i64, calc_max!(::test::I64_MAX, 2), 2, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_3, - i64, calc_max!(::test::I64_MAX, 3), 3, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_4, - i64, calc_max!(::test::I64_MAX, 4), 4, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_5, - i64, calc_max!(::test::I64_MAX, 5), 5, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_6, - i64, calc_max!(::test::I64_MAX, 6), 6, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_7, - i64, calc_max!(::test::I64_MAX, 1), 7, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_8, - i64, calc_max!(::test::I64_MAX, 8), 8, read_int, write_i64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_1, - Wi128, 1, 1, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_2, - Wi128, 2, 2, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_3, - Wi128, 3, 3, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_4, - Wi128, 4, 4, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_5, - Wi128, 5, 5, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_6, - Wi128, 6, 6, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_7, - Wi128, 7, 7, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_8, - Wi128, 8, 8, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_9, - Wi128, 9, 9, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_10, - Wi128, 10, 10, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_11, - Wi128, 11, 11, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_12, - Wi128, 12, 12, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_13, - Wi128, 13, 13, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_14, - Wi128, 14, 14, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_15, - Wi128, 15, 15, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_16, - Wi128, 16, 16, read_int128, write_i128); + qc_bytes_ext!( + prop_ext_uint_1, + u64, + calc_max!(crate::test::U64_MAX, 1), + 1, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_2, + u64, + calc_max!(crate::test::U64_MAX, 2), + 2, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_3, + u64, + calc_max!(crate::test::U64_MAX, 3), + 3, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_4, + u64, + calc_max!(crate::test::U64_MAX, 4), + 4, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_5, + u64, + calc_max!(crate::test::U64_MAX, 5), + 5, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_6, + u64, + calc_max!(crate::test::U64_MAX, 6), + 6, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_7, + u64, + calc_max!(crate::test::U64_MAX, 7), + 7, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_8, + u64, + calc_max!(crate::test::U64_MAX, 8), + 8, + read_uint, + write_u64 + ); + + qc_bytes_ext!( + prop_ext_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_u128 + ); + + qc_bytes_ext!( + prop_ext_int_1, + i64, + calc_max!(crate::test::I64_MAX, 1), + 1, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_2, + i64, + calc_max!(crate::test::I64_MAX, 2), + 2, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_3, + i64, + calc_max!(crate::test::I64_MAX, 3), + 3, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_4, + i64, + calc_max!(crate::test::I64_MAX, 4), + 4, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_5, + i64, + calc_max!(crate::test::I64_MAX, 5), + 5, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_6, + i64, + calc_max!(crate::test::I64_MAX, 6), + 6, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_7, + i64, + calc_max!(crate::test::I64_MAX, 1), + 7, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_8, + i64, + calc_max!(crate::test::I64_MAX, 8), + 8, + read_int, + write_i64 + ); + + qc_bytes_ext!( + prop_ext_int128_1, + Wi128, + 1, + 1, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_2, + Wi128, + 2, + 2, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_3, + Wi128, + 3, + 3, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_4, + Wi128, + 4, + 4, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_5, + Wi128, + 5, + 5, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_6, + Wi128, + 6, + 6, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_7, + Wi128, + 7, + 7, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_8, + Wi128, + 8, + 8, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_9, + Wi128, + 9, + 9, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_10, + Wi128, + 10, + 10, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_11, + Wi128, + 11, + 11, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_12, + Wi128, + 12, + 12, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_13, + Wi128, + 13, + 13, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_14, + Wi128, + 14, + 14, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_15, + Wi128, + 15, + 15, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_16, + Wi128, + 16, + 16, + read_int128, + write_i128 + ); // Test slice serialization/deserialization. macro_rules! qc_slice { ($name:ident, $ty_int:ty, $read:ident, $write:ident, $zero:expr) => { mod $name { - use core::mem::size_of; - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; use super::qc_unsized; #[allow(unused_imports)] - use test::Wi128; + use crate::test::Wi128; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use core::mem::size_of; #[test] fn big_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; BigEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { BigEndian::$read(&bytes, &mut got); } + unsafe { + BigEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3267,17 +3971,17 @@ mod stdtests { fn little_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; LittleEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { LittleEndian::$read(&bytes, &mut got); } + unsafe { + LittleEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3288,24 +3992,24 @@ mod stdtests { fn native_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; NativeEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { NativeEndian::$read(&bytes, &mut got); } + unsafe { + NativeEndian::$read(&bytes, &mut got); + } numbers == got } qc_unsized(prop as fn(_) -> bool); } } - } + }; } qc_slice!(prop_slice_u16, u16, read_u16_into, write_u16_into, 0); @@ -3314,15 +4018,21 @@ mod stdtests { qc_slice!(prop_slice_i32, i32, read_i32_into, write_i32_into, 0); qc_slice!(prop_slice_u64, u64, read_u64_into, write_u64_into, 0); qc_slice!(prop_slice_i64, i64, read_i64_into, write_i64_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_u128, Wi128, read_u128_into, write_u128_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_i128, Wi128, read_i128_into, write_i128_into, 0); - qc_slice!( - prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + prop_slice_u128, + Wi128, + read_u128_into, + write_u128_into, + 0 + ); qc_slice!( - prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); + prop_slice_i128, + Wi128, + read_i128_into, + write_i128_into, + 0 + ); + + qc_slice!(prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + qc_slice!(prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); } -- cgit v1.2.3 From 053c06814ac0a64d3e73081d306ba68ebdf3f500 Mon Sep 17 00:00:00 2001 From: Yi Kong Date: Sun, 14 Feb 2021 22:04:08 +0800 Subject: Update to 1.4.2 Change-Id: I9b660b3276276d55040849a3cd6e5b70dbcd513b Merged-In: I9b660b3276276d55040849a3cd6e5b70dbcd513b (cherry picked from commit 6d7927fcf9f2566d106f4d23fa0d29bd830b9bb7) --- .cargo_vcs_info.json | 2 +- .github/workflows/ci.yml | 151 ++++ Android.bp | 10 +- CHANGELOG.md | 25 + Cargo.toml | 15 +- Cargo.toml.orig | 14 +- LICENSE | 1 + METADATA | 19 + MODULE_LICENSE_MIT | 0 OWNERS | 1 + README.md | 6 +- benches/bench.rs | 236 +++--- build.rs | 87 --- rustfmt.toml | 2 + src/io.rs | 92 +-- src/lib.rs | 1794 ++++++++++++++++++++++++++++++++-------------- 16 files changed, 1623 insertions(+), 832 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 120000 LICENSE create mode 100644 METADATA create mode 100644 MODULE_LICENSE_MIT create mode 100644 OWNERS delete mode 100644 build.rs create mode 100644 rustfmt.toml diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index e7e8b81..e260c24 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "058237661a88c2f610f280340310fce19d98d265" + "sha1": "ca8c10a3f85e2c66781f7d12e90196e6923592d7" } } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2c2c790 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,151 @@ +name: ci +on: + pull_request: + push: + branches: + - master + schedule: + - cron: '00 01 * * *' +jobs: + test: + name: test + env: + # For some builds, we use cross to test on 32-bit and big-endian + # systems. + CARGO: cargo + # When CARGO is set to CROSS, TARGET is set to `--target matrix.target`. + TARGET: + runs-on: ${{ matrix.os }} + strategy: + matrix: + build: + - pinned + - stable + - stable-32 + - stable-mips + - beta + - nightly + - macos + - win-msvc + - win-gnu + include: + - build: pinned + os: ubuntu-18.04 + rust: 1.41.1 + - build: stable + os: ubuntu-18.04 + rust: stable + - build: stable-32 + os: ubuntu-18.04 + rust: stable + target: i686-unknown-linux-gnu + - build: stable-mips + os: ubuntu-18.04 + rust: stable + target: mips64-unknown-linux-gnuabi64 + - build: beta + os: ubuntu-18.04 + rust: beta + - build: nightly + os: ubuntu-18.04 + rust: nightly + - build: macos + os: macos-latest + rust: stable + - build: win-msvc + os: windows-2019 + rust: stable + - build: win-gnu + os: windows-2019 + rust: stable-x86_64-gnu + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + profile: minimal + override: true + + - name: Use Cross + if: matrix.target != '' + run: | + # FIXME: to work around bugs in latest cross release, install master. + # See: https://github.com/rust-embedded/cross/issues/357 + cargo install --git https://github.com/rust-embedded/cross + echo "CARGO=cross" >> $GITHUB_ENV + echo "TARGET=--target ${{ matrix.target }}" >> $GITHUB_ENV + + - name: Show command used for Cargo + run: | + echo "cargo command is: ${{ env.CARGO }}" + echo "target flag is: ${{ env.TARGET }}" + + - name: Show CPU info for debugging + if: matrix.os == 'ubuntu-18.04' + run: lscpu + + - name: Build + run: ${{ env.CARGO }} build --verbose $TARGET + + - name: Build (no default) + run: ${{ env.CARGO }} build --verbose $TARGET --no-default-features + + - name: Build docs + run: ${{ env.CARGO }} doc --verbose $TARGET + + # Our dev dependencies evolve more rapidly than we'd like, so only run + # tests when we aren't pinning the Rust version. + - name: Tests + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose $TARGET + + - name: Tests (no default, lib only) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --lib $TARGET + + - name: Tests (i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --features i128 $TARGET + + - name: Tests (no default, lib only, i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --features i128 --lib $TARGET + + - name: Compile benchmarks + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run $TARGET + + - name: Compile benchmarks (no default) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features $TARGET + + - name: Compile benchmarks (i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --features i128 $TARGET + + - name: Compile benchmarks (no default, i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features --features i128 $TARGET + + rustfmt: + name: rustfmt + runs-on: ubuntu-18.04 + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + profile: minimal + components: rustfmt + - name: Check formatting + run: cargo fmt -- --check diff --git a/Android.bp b/Android.bp index 72ef217..8f42c1a 100644 --- a/Android.bp +++ b/Android.bp @@ -1,17 +1,13 @@ -// This file is generated by cargo2android.py. +// This file is generated by cargo2android.py --run --device --dependencies. -rust_library_rlib { +rust_library { name: "libbyteorder", - deny_warnings: false, host_supported: true, crate_name: "byteorder", srcs: ["src/lib.rs"], - edition: "2015", + edition: "2018", features: [ "default", "std", ], - flags: [ - "--cfg byteorder_i128", - ], } diff --git a/CHANGELOG.md b/CHANGELOG.md index 020beb4..6b51eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,28 @@ +1.3.4 +===== +This patch release squashes deprecation warnings for the `try!` macro, in +accordance with byteorder's minimum supported Rust version (currently at Rust +1.12.0). + + +1.3.3 +===== +This patch release adds `ByteOrder::write_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.2 +===== +This patch release adds `ReadBytesExt::read_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.1 +===== +This minor release performs mostly small internal changes. Going forward, these +are not going to be incorporated into the changelog. + + 1.3.0 ===== This new minor release now enables `i128` support automatically on Rust diff --git a/Cargo.toml b/Cargo.toml index fb2acad..ff60b60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,17 +11,17 @@ # will likely look very different (and much more reasonable) [package] +edition = "2018" name = "byteorder" -version = "1.3.2" +version = "1.4.2" authors = ["Andrew Gallant "] -build = "build.rs" exclude = ["/ci/*"] description = "Library for reading/writing numbers in big-endian and little-endian." homepage = "https://github.com/BurntSushi/byteorder" documentation = "https://docs.rs/byteorder" readme = "README.md" keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] license = "Unlicense OR MIT" repository = "https://github.com/BurntSushi/byteorder" [profile.bench] @@ -30,19 +30,14 @@ opt-level = 3 [lib] name = "byteorder" bench = false -[dev-dependencies.doc-comment] -version = "0.3" - [dev-dependencies.quickcheck] -version = "0.8" +version = "0.9.2" default-features = false [dev-dependencies.rand] -version = "0.6" +version = "0.7" [features] default = ["std"] i128 = [] std = [] -[badges.travis-ci] -repository = "BurntSushi/byteorder" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 91f2aa3..efeafb2 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,26 +1,25 @@ [package] name = "byteorder" -version = "1.3.2" #:version +version = "1.4.2" #:version authors = ["Andrew Gallant "] description = "Library for reading/writing numbers in big-endian and little-endian." documentation = "https://docs.rs/byteorder" homepage = "https://github.com/BurntSushi/byteorder" repository = "https://github.com/BurntSushi/byteorder" readme = "README.md" -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] license = "Unlicense OR MIT" exclude = ["/ci/*"] -build = "build.rs" +edition = "2018" [lib] name = "byteorder" bench = false [dev-dependencies] -quickcheck = { version = "0.8", default-features = false } -rand = "0.6" -doc-comment = "0.3" +quickcheck = { version = "0.9.2", default-features = false } +rand = "0.7" [features] default = ["std"] @@ -33,6 +32,3 @@ i128 = [] [profile.bench] opt-level = 3 - -[badges] -travis-ci = { repository = "BurntSushi/byteorder" } diff --git a/LICENSE b/LICENSE new file mode 120000 index 0000000..7f9a88e --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +LICENSE-MIT \ No newline at end of file diff --git a/METADATA b/METADATA new file mode 100644 index 0000000..95a62dd --- /dev/null +++ b/METADATA @@ -0,0 +1,19 @@ +name: "byteorder" +description: "Library for reading/writing numbers in big-endian and little-endian." +third_party { + url { + type: HOMEPAGE + value: "https://crates.io/crates/byteorder" + } + url { + type: ARCHIVE + value: "https://static.crates.io/crates/byteorder/byteorder-1.4.2.crate" + } + version: "1.4.2" + license_type: NOTICE + last_upgrade_date { + year: 2021 + month: 2 + day: 7 + } +} diff --git a/MODULE_LICENSE_MIT b/MODULE_LICENSE_MIT new file mode 100644 index 0000000..e69de29 diff --git a/OWNERS b/OWNERS new file mode 100644 index 0000000..46fc303 --- /dev/null +++ b/OWNERS @@ -0,0 +1 @@ +include platform/prebuilts/rust:/OWNERS diff --git a/README.md b/README.md index 8940b29..8b2ecc4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ +byteorder +========= This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order. -[![Build status](https://api.travis-ci.org/BurntSushi/byteorder.svg)](https://travis-ci.org/BurntSushi/byteorder) +[![Build status](https://github.com/BurntSushi/byteorder/workflows/ci/badge.svg)](https://github.com/BurntSushi/byteorder/actions) [![](http://meritbadge.herokuapp.com/byteorder)](https://crates.io/crates/byteorder) Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org). @@ -27,8 +29,6 @@ If you want to augment existing `Read` and `Write` traits, then import the extension methods like so: ```rust -extern crate byteorder; - use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian, LittleEndian}; ``` diff --git a/benches/bench.rs b/benches/bench.rs index d53d25e..bb00422 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,15 +1,15 @@ #![feature(test)] -extern crate byteorder; -extern crate rand; extern crate test; macro_rules! bench_num { - ($name:ident, $read:ident, $bytes:expr, $data:expr) => ( + ($name:ident, $read:ident, $bytes:expr, $data:expr) => { mod $name { - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -43,14 +43,16 @@ macro_rules! bench_num { }); } } - ); + }; ($ty:ident, $max:ident, - $read:ident, $write:ident, $size:expr, $data:expr) => ( + $read:ident, $write:ident, $size:expr, $data:expr) => { mod $ty { + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use std::$ty; - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -117,7 +119,7 @@ macro_rules! bench_num { }); } } - ); + }; } bench_num!(u16, MAX, read_u16, write_u16, 2, [1, 2]); @@ -127,8 +129,7 @@ bench_num!(i32, MAX, read_i32, write_i32, 4, [1, 2, 3, 4]); bench_num!(u64, MAX, read_u64, write_u64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(i64, MAX, read_i64, write_i64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(f32, MAX, read_f32, write_f32, 4, [1, 2, 3, 4]); -bench_num!(f64, MAX, read_f64, write_f64, 8, - [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(f64, MAX, read_f64, write_f64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(uint_1, read_uint, 1, [1]); bench_num!(uint_2, read_uint, 2, [1, 2]); @@ -148,120 +149,115 @@ bench_num!(int_6, read_int, 6, [1, 2, 3, 4, 5, 6]); bench_num!(int_7, read_int, 7, [1, 2, 3, 4, 5, 6, 7]); bench_num!(int_8, read_int, 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(u128, MAX, read_u128, write_u128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); -#[cfg(byteorder_i128)] -bench_num!(i128, MAX, read_i128, write_i128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); - -#[cfg(byteorder_i128)] -bench_num!(uint128_1, read_uint128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(uint128_2, read_uint128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(uint128_3, read_uint128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(uint128_4, read_uint128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(uint128_5, read_uint128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(uint128_6, read_uint128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(uint128_7, read_uint128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(uint128_8, read_uint128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(uint128_9, read_uint128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(uint128_10, read_uint128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(uint128_11, read_uint128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(uint128_12, read_uint128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(uint128_13, read_uint128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(uint128_14, read_uint128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(uint128_15, read_uint128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(uint128_16, read_uint128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!( + u128, + MAX, + read_u128, + write_u128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!( + i128, + MAX, + read_i128, + write_i128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); -#[cfg(byteorder_i128)] -bench_num!(int128_1, read_int128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(int128_2, read_int128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(int128_3, read_int128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(int128_4, read_int128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(int128_5, read_int128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(int128_6, read_int128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(int128_7, read_int128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(int128_8, read_int128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(int128_9, read_int128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(int128_10, read_int128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(int128_11, read_int128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(int128_12, read_int128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(int128_13, read_int128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(int128_14, read_int128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(int128_15, read_int128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(int128_16, read_int128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!(uint128_1, read_uint128, 1, [1]); +bench_num!(uint128_2, read_uint128, 2, [1, 2]); +bench_num!(uint128_3, read_uint128, 3, [1, 2, 3]); +bench_num!(uint128_4, read_uint128, 4, [1, 2, 3, 4]); +bench_num!(uint128_5, read_uint128, 5, [1, 2, 3, 4, 5]); +bench_num!(uint128_6, read_uint128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(uint128_7, read_uint128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(uint128_8, read_uint128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(uint128_9, read_uint128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(uint128_10, read_uint128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(uint128_11, read_uint128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + uint128_12, + read_uint128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + uint128_13, + read_uint128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + uint128_14, + read_uint128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + uint128_15, + read_uint128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + uint128_16, + read_uint128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!(int128_1, read_int128, 1, [1]); +bench_num!(int128_2, read_int128, 2, [1, 2]); +bench_num!(int128_3, read_int128, 3, [1, 2, 3]); +bench_num!(int128_4, read_int128, 4, [1, 2, 3, 4]); +bench_num!(int128_5, read_int128, 5, [1, 2, 3, 4, 5]); +bench_num!(int128_6, read_int128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(int128_7, read_int128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(int128_8, read_int128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(int128_9, read_int128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(int128_10, read_int128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(int128_11, read_int128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + int128_12, + read_int128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + int128_13, + read_int128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + int128_14, + read_int128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + int128_15, + read_int128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + int128_16, + read_int128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); macro_rules! bench_slice { ($name:ident, $numty:ty, $read:ident, $write:ident) => { mod $name { use std::mem::size_of; - use byteorder::{ByteOrder, BigEndian, LittleEndian}; - use rand::{self, Rng}; + use byteorder::{BigEndian, ByteOrder, LittleEndian}; use rand::distributions; + use rand::{self, Rng}; use test::Bencher; #[bench] @@ -322,7 +318,7 @@ macro_rules! bench_slice { }); } } - } + }; } bench_slice!(slice_u64, u64, read_u64_into, write_u64_into); diff --git a/build.rs b/build.rs deleted file mode 100644 index 002135b..0000000 --- a/build.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::env; -use std::ffi::OsString; -use std::io::{self, Write}; -use std::process::Command; - -fn main() { - let version = match Version::read() { - Ok(version) => version, - Err(err) => { - writeln!( - &mut io::stderr(), - "failed to parse `rustc --version`: {}", - err - ).unwrap(); - return; - } - }; - enable_i128(version); -} - -fn enable_i128(version: Version) { - if version < (Version { major: 1, minor: 26, patch: 0 }) { - return; - } - - println!("cargo:rustc-cfg=byteorder_i128"); -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] -struct Version { - major: u32, - minor: u32, - patch: u32, -} - -impl Version { - fn read() -> Result { - let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); - let output = Command::new(&rustc) - .arg("--version") - .output() - .unwrap() - .stdout; - Version::parse(&String::from_utf8(output).unwrap()) - } - - fn parse(mut s: &str) -> Result { - if !s.starts_with("rustc ") { - return Err(format!("unrecognized version string: {}", s)); - } - s = &s["rustc ".len()..]; - - let parts: Vec<&str> = s.split(".").collect(); - if parts.len() < 3 { - return Err(format!("not enough version parts: {:?}", parts)); - } - - let mut num = String::new(); - for c in parts[0].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let major = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[1].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let minor = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[2].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let patch = try!(num.parse::().map_err(|e| e.to_string())); - - Ok(Version { major: major, minor: minor, patch: patch }) - } -} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..aa37a21 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 79 +use_small_heuristics = "max" diff --git a/src/io.rs b/src/io.rs index ed6a848..ac41ff7 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,7 +1,9 @@ -use std::io::{self, Result}; -use std::slice; +use std::{ + io::{self, Result}, + slice, +}; -use ByteOrder; +use crate::ByteOrder; /// Extends [`Read`] with methods for reading numbers. (For `std::io`.) /// @@ -52,7 +54,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0]) } @@ -82,7 +84,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0] as i8) } @@ -109,7 +111,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u16(&buf)) } @@ -136,7 +138,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i16(&buf)) } @@ -162,7 +164,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u24(&buf)) } @@ -188,7 +190,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i24(&buf)) } @@ -214,7 +216,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u32(&buf)) } @@ -240,7 +242,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i32(&buf)) } @@ -266,7 +268,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u48(&buf)) } @@ -292,7 +294,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i48(&buf)) } @@ -318,7 +320,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u64(&buf)) } @@ -344,7 +346,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i64(&buf)) } @@ -370,11 +372,10 @@ pub trait ReadBytesExt: io::Read { /// ]); /// assert_eq!(16947640962301618749969007319746179, rdr.read_u128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u128(&buf)) } @@ -397,11 +398,10 @@ pub trait ReadBytesExt: io::Read { /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); /// assert_eq!(i128::min_value(), rdr.read_i128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i128(&buf)) } @@ -426,7 +426,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_uint(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint(&buf[..nbytes], nbytes)) } @@ -451,25 +451,23 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_int(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int(&buf[..nbytes], nbytes)) } /// Reads an unsigned n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_uint128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint128(&buf[..nbytes], nbytes)) } /// Reads a signed n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_int128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int128(&buf[..nbytes], nbytes)) } @@ -500,7 +498,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f32(&buf)) } @@ -531,7 +529,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f64(&buf)) } @@ -564,7 +562,7 @@ pub trait ReadBytesExt: io::Read { fn read_u16_into(&mut self, dst: &mut [u16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u16(dst); Ok(()) @@ -599,7 +597,7 @@ pub trait ReadBytesExt: io::Read { fn read_u32_into(&mut self, dst: &mut [u32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u32(dst); Ok(()) @@ -637,7 +635,7 @@ pub trait ReadBytesExt: io::Read { fn read_u64_into(&mut self, dst: &mut [u64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u64(dst); Ok(()) @@ -671,7 +669,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_u128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128_into( &mut self, @@ -679,7 +676,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u128(dst); Ok(()) @@ -750,7 +747,7 @@ pub trait ReadBytesExt: io::Read { fn read_i16_into(&mut self, dst: &mut [i16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i16(dst); Ok(()) @@ -785,7 +782,7 @@ pub trait ReadBytesExt: io::Read { fn read_i32_into(&mut self, dst: &mut [i32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i32(dst); Ok(()) @@ -823,7 +820,7 @@ pub trait ReadBytesExt: io::Read { fn read_i64_into(&mut self, dst: &mut [i64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i64(dst); Ok(()) @@ -857,7 +854,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_i128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into( &mut self, @@ -865,7 +861,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i128(dst); Ok(()) @@ -903,13 +899,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f32_into( - &mut self, - dst: &mut [f32], - ) -> Result<()> { + fn read_f32_into(&mut self, dst: &mut [f32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f32(dst); Ok(()) @@ -951,7 +944,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked( &mut self, dst: &mut [f32], @@ -991,13 +984,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f64_into( - &mut self, - dst: &mut [f64], - ) -> Result<()> { + fn read_f64_into(&mut self, dst: &mut [f64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f64(dst); Ok(()) @@ -1045,7 +1035,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked( &mut self, dst: &mut [f64], @@ -1408,7 +1398,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes an unsigned 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_u128(&mut self, n: u128) -> Result<()> { let mut buf = [0; 16]; @@ -1417,7 +1406,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes a signed 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_i128(&mut self, n: i128) -> Result<()> { let mut buf = [0; 16]; @@ -1501,7 +1489,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_uint128( &mut self, @@ -1517,7 +1504,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_int128( &mut self, diff --git a/src/lib.rs b/src/lib.rs index db4d24d..d56574d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,23 +70,12 @@ cases. #![deny(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "std")] -extern crate core; - -#[cfg(test)] -#[macro_use] -extern crate doc_comment; - -#[cfg(test)] -doctest!("../README.md"); - -use core::fmt::Debug; -use core::hash::Hash; -use core::ptr::copy_nonoverlapping; -use core::slice; +use core::{ + convert::TryInto, fmt::Debug, hash::Hash, ptr::copy_nonoverlapping, slice, +}; #[cfg(feature = "std")] -pub use io::{ReadBytesExt, WriteBytesExt}; +pub use crate::io::{ReadBytesExt, WriteBytesExt}; #[cfg(feature = "std")] mod io; @@ -97,7 +86,6 @@ fn extend_sign(val: u64, nbytes: usize) -> i64 { (val << shift) as i64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn extend_sign128(val: u128, nbytes: usize) -> i128 { let shift = (16 - nbytes) * 8; @@ -110,7 +98,6 @@ fn unextend_sign(val: i64, nbytes: usize) -> u64 { (val << shift) as u64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn unextend_sign128(val: i128, nbytes: usize) -> u128 { let shift = (16 - nbytes) * 8; @@ -138,7 +125,6 @@ fn pack_size(n: u64) -> usize { } } -#[cfg(byteorder_i128)] #[inline] fn pack_size128(n: u128) -> usize { if n < 1 << 8 { @@ -179,7 +165,7 @@ fn pack_size128(n: u128) -> usize { mod private { /// Sealed stops crates other than byteorder from implementing any traits /// that use it. - pub trait Sealed{} + pub trait Sealed {} impl Sealed for super::LittleEndian {} impl Sealed for super::BigEndian {} } @@ -219,8 +205,16 @@ mod private { /// /// [`BigEndian`]: enum.BigEndian.html /// [`LittleEndian`]: enum.LittleEndian.html -pub trait ByteOrder - : Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd +pub trait ByteOrder: + Clone + + Copy + + Debug + + Default + + Eq + + Hash + + Ord + + PartialEq + + PartialOrd + private::Sealed { /// Reads an unsigned 16 bit integer from `buf`. @@ -327,7 +321,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn read_u128(buf: &[u8]) -> u128; /// Reads an unsigned n-bytes integer from `buf`. @@ -368,7 +361,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn read_uint128(buf: &[u8], nbytes: usize) -> u128; /// Writes an unsigned 16 bit integer `n` to `buf`. @@ -487,7 +479,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn write_u128(buf: &mut [u8], n: u128); /// Writes an unsigned integer `n` to `buf` using only `nbytes`. @@ -528,7 +519,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize); /// Reads a signed 16 bit integer from `buf`. @@ -658,7 +648,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(buf: &[u8]) -> i128 { Self::read_u128(buf) as i128 @@ -705,7 +694,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_int128(buf: &[u8], nbytes: usize) -> i128 { extend_sign128(Self::read_uint128(buf, nbytes), nbytes) @@ -731,7 +719,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f32(buf: &[u8]) -> f32 { - unsafe { *(&Self::read_u32(buf) as *const u32 as *const f32) } + f32::from_bits(Self::read_u32(buf)) } /// Reads a IEEE754 double-precision (8 bytes) floating point number. @@ -754,7 +742,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f64(buf: &[u8]) -> f64 { - unsafe { *(&Self::read_u64(buf) as *const u64 as *const f64) } + f64::from_bits(Self::read_u64(buf)) } /// Writes a signed 16 bit integer `n` to `buf`. @@ -884,7 +872,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_i128(buf: &mut [u8], n: i128) { Self::write_u128(buf, n as u128) @@ -931,7 +918,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_int128(buf: &mut [u8], n: i128, nbytes: usize) { Self::write_uint128(buf, unextend_sign128(n, nbytes), nbytes) @@ -957,8 +943,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f32(buf: &mut [u8], n: f32) { - let n = unsafe { *(&n as *const f32 as *const u32) }; - Self::write_u32(buf, n) + Self::write_u32(buf, n.to_bits()) } /// Writes a IEEE754 double-precision (8 bytes) floating point number. @@ -981,8 +966,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f64(buf: &mut [u8], n: f64) { - let n = unsafe { *(&n as *const f64 as *const u64) }; - Self::write_u64(buf, n) + Self::write_u64(buf, n.to_bits()) } /// Reads unsigned 16 bit integers from `src` into `dst`. @@ -1075,7 +1059,6 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn read_u128_into(src: &[u8], dst: &mut [u128]); /// Reads signed 16 bit integers from `src` to `dst`. @@ -1186,7 +1169,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into(src: &[u8], dst: &mut [i128]) { let dst = unsafe { @@ -1251,7 +1233,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) { Self::read_f32_into(src, dst); } @@ -1313,7 +1295,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) { Self::read_f64_into(src, dst); } @@ -1408,9 +1390,42 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_u128_into(src: &[u128], dst: &mut [u8]); + /// Writes signed 8 bit integers from `src` into `dst`. + /// + /// Note that since each `i8` is a single byte, no byte order conversions + /// are used. This method is included because it provides a safe, simple + /// way for the caller to write from a `&[i8]` buffer. (Without this + /// method, the caller would have to either use `unsafe` code or convert + /// each byte to `u8` individually.) + /// + /// # Panics + /// + /// Panics when `buf.len() != src.len()`. + /// + /// # Examples + /// + /// Write and read `i8` numbers in little endian order: + /// + /// ```rust + /// use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; + /// + /// let mut bytes = [0; 4]; + /// let numbers_given = [1, 2, 0xf, 0xe]; + /// LittleEndian::write_i8_into(&numbers_given, &mut bytes); + /// + /// let mut numbers_got = [0; 4]; + /// bytes.as_ref().read_i8_into(&mut numbers_got); + /// assert_eq!(numbers_given, numbers_got); + /// ``` + fn write_i8_into(src: &[i8], dst: &mut [u8]) { + let src = unsafe { + slice::from_raw_parts(src.as_ptr() as *const u8, src.len()) + }; + dst.copy_from_slice(src); + } + /// Writes signed 16 bit integers from `src` into `dst`. /// /// # Panics @@ -1516,7 +1531,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_i128_into(src: &[i128], dst: &mut [u8]) { let src = unsafe { slice::from_raw_parts(src.as_ptr() as *const u128, src.len()) @@ -1660,7 +1674,6 @@ pub trait ByteOrder /// BigEndian::from_slice_u128(&mut numbers); /// assert_eq!(numbers, [5u128.to_be(), 65000u128.to_be()]); /// ``` - #[cfg(byteorder_i128)] fn from_slice_u128(numbers: &mut [u128]); /// Converts the given slice of signed 16 bit integers to a particular @@ -1755,7 +1768,6 @@ pub trait ByteOrder /// BigEndian::from_slice_i128(&mut numbers); /// assert_eq!(numbers, [5i128.to_be(), 65000i128.to_be()]); /// ``` - #[cfg(byteorder_i128)] #[inline] fn from_slice_i128(src: &mut [i128]) { let src = unsafe { @@ -1887,30 +1899,15 @@ pub type NativeEndian = LittleEndian; #[cfg(target_endian = "big")] pub type NativeEndian = BigEndian; -macro_rules! read_num_bytes { - ($ty:ty, $size:expr, $src:expr, $which:ident) => ({ - assert!($size == ::core::mem::size_of::<$ty>()); - assert!($size <= $src.len()); - let mut data: $ty = 0; - unsafe { - copy_nonoverlapping( - $src.as_ptr(), - &mut data as *mut $ty as *mut u8, - $size); - } - data.$which() - }); -} - macro_rules! write_num_bytes { - ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => ({ + ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => {{ assert!($size <= $dst.len()); unsafe { // N.B. https://github.com/rust-lang/rust/issues/22776 let bytes = *(&$n.$which() as *const _ as *const [u8; $size]); copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size); } - }); + }}; } macro_rules! read_slice { @@ -1921,7 +1918,8 @@ macro_rules! read_slice { copy_nonoverlapping( $src.as_ptr(), $dst.as_mut_ptr() as *mut u8, - $src.len()); + $src.len(), + ); } for v in $dst.iter_mut() { *v = v.$which(); @@ -1938,67 +1936,72 @@ macro_rules! write_slice_native { copy_nonoverlapping( $src.as_ptr() as *const u8, $dst.as_mut_ptr(), - $dst.len()); + $dst.len(), + ); } }}; } macro_rules! write_slice { - ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => ({ + ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => {{ assert!($size == ::core::mem::size_of::<$ty>()); assert_eq!($size * $src.len(), $dst.len()); for (&n, chunk) in $src.iter().zip($dst.chunks_mut($size)) { $write(chunk, n); } - }); + }}; } impl ByteOrder for BigEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_be) + u16::from_be_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_be) + u32::from_be_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_be) + u64::from_be_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_be) + u128::from_be_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((8 - nbytes) as isize), nbytes); - (*(ptr_out as *const u64)).to_be() + buf.as_ptr(), + ptr_out.offset((8 - nbytes) as isize), + nbytes, + ); } + out.to_be() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((16 - nbytes) as isize), nbytes); - (*(ptr_out as *const u128)).to_be() + buf.as_ptr(), + ptr_out.offset((16 - nbytes) as isize), + nbytes, + ); } + out.to_be() } #[inline] @@ -2016,7 +2019,6 @@ impl ByteOrder for BigEndian { write_num_bytes!(u64, 8, n, buf, to_be); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_be); @@ -2031,11 +2033,11 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((8 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n) <= nbytes && nbytes <= 16); @@ -2045,7 +2047,8 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((16 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } @@ -2064,7 +2067,6 @@ impl ByteOrder for BigEndian { read_slice!(src, dst, 8, to_be); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_be); @@ -2097,7 +2099,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "big") { @@ -2134,7 +2135,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "little") { @@ -2172,46 +2172,44 @@ impl ByteOrder for BigEndian { impl ByteOrder for LittleEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_le) + u16::from_le_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_le) + u32::from_le_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_le) + u64::from_le_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_le) + u128::from_le_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u64)).to_le() } + out.to_le() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u128)).to_le() } + out.to_le() } #[inline] @@ -2229,7 +2227,6 @@ impl ByteOrder for LittleEndian { write_num_bytes!(u64, 8, n, buf, to_le); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_le); @@ -2245,7 +2242,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n as u128) <= nbytes && nbytes <= 16); @@ -2271,7 +2267,6 @@ impl ByteOrder for LittleEndian { read_slice!(src, dst, 8, to_le); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_le); @@ -2304,7 +2299,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "little") { @@ -2341,7 +2335,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "big") { @@ -2378,15 +2371,8 @@ impl ByteOrder for LittleEndian { #[cfg(test)] mod test { - extern crate quickcheck; - extern crate rand; - - use self::quickcheck::{QuickCheck, StdGen, Testable}; - use self::rand::thread_rng; - #[cfg(byteorder_i128)] - use self::rand::Rng; - #[cfg(byteorder_i128)] - use self::quickcheck::{Arbitrary, Gen}; + use quickcheck::{Arbitrary, Gen, QuickCheck, StdGen, Testable}; + use rand::{thread_rng, Rng}; pub const U24_MAX: u32 = 16_777_215; pub const I24_MAX: i32 = 8_388_607; @@ -2397,7 +2383,9 @@ mod test { pub const I64_MAX: u64 = ::core::i64::MAX as u64; macro_rules! calc_max { - ($max:expr, $bytes:expr) => { calc_max!($max, $bytes, 8) }; + ($max:expr, $bytes:expr) => { + calc_max!($max, $bytes, 8) + }; ($max:expr, $bytes:expr, $maxbytes:expr) => { ($max - 1) >> (8 * ($maxbytes - $bytes)) }; @@ -2406,7 +2394,6 @@ mod test { #[derive(Clone, Debug)] pub struct Wi128(pub T); - #[cfg(byteorder_i128)] impl Wi128 { pub fn clone(&self) -> T { self.0.clone() @@ -2419,24 +2406,20 @@ mod test { } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::u128::MAX, gen.size(), 16); - let output = - (gen.gen::() as u128) | - ((gen.gen::() as u128) << 64); + let output = (gen.gen::() as u128) + | ((gen.gen::() as u128) << 64); Wi128(output & (max - 1)) } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::i128::MAX, gen.size(), 16); - let output = - (gen.gen::() as i128) | - ((gen.gen::() as i128) << 64); + let output = (gen.gen::() as i128) + | ((gen.gen::() as i128) << 64); Wi128(output & (max - 1)) } } @@ -2451,17 +2434,20 @@ mod test { macro_rules! qc_byte_order { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] fn big_endian() { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; BigEndian::$write(&mut buf, n.clone(), $bytes); - n == BigEndian::$read(&mut buf[..$bytes], $bytes) + n == BigEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2471,7 +2457,7 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; LittleEndian::$write(&mut buf, n.clone(), $bytes); - n == LittleEndian::$read(&mut buf[..$bytes], $bytes) + n == LittleEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2481,18 +2467,21 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; NativeEndian::$write(&mut buf, n.clone(), $bytes); - n == NativeEndian::$read(&mut buf[..$bytes], $bytes) + n == NativeEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); + }; ($name:ident, $ty_int:ty, $max:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use core::mem::size_of; - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; #[test] fn big_endian() { @@ -2500,7 +2489,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; BigEndian::$write(&mut buf[16 - bytes..], n.clone()); - n == BigEndian::$read(&mut buf[16 - bytes..]) + n == BigEndian::$read(&buf[16 - bytes..]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2511,7 +2500,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; LittleEndian::$write(&mut buf[..bytes], n.clone()); - n == LittleEndian::$read(&mut buf[..bytes]) + n == LittleEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2522,164 +2511,489 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; NativeEndian::$write(&mut buf[..bytes], n.clone()); - n == NativeEndian::$read(&mut buf[..bytes]) + n == NativeEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_byte_order!(prop_u16, u16, ::core::u16::MAX as u64, read_u16, write_u16); - qc_byte_order!(prop_i16, i16, ::core::i16::MAX as u64, read_i16, write_i16); - qc_byte_order!(prop_u24, u32, ::test::U24_MAX as u64, read_u24, write_u24); - qc_byte_order!(prop_i24, i32, ::test::I24_MAX as u64, read_i24, write_i24); - qc_byte_order!(prop_u32, u32, ::core::u32::MAX as u64, read_u32, write_u32); - qc_byte_order!(prop_i32, i32, ::core::i32::MAX as u64, read_i32, write_i32); - qc_byte_order!(prop_u48, u64, ::test::U48_MAX as u64, read_u48, write_u48); - qc_byte_order!(prop_i48, i64, ::test::I48_MAX as u64, read_i48, write_i48); - qc_byte_order!(prop_u64, u64, ::core::u64::MAX as u64, read_u64, write_u64); - qc_byte_order!(prop_i64, i64, ::core::i64::MAX as u64, read_i64, write_i64); - qc_byte_order!(prop_f32, f32, ::core::u64::MAX as u64, read_f32, write_f32); - qc_byte_order!(prop_f64, f64, ::core::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_byte_order!( + prop_u16, + u16, + ::core::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_byte_order!( + prop_i16, + i16, + ::core::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_byte_order!( + prop_u24, + u32, + crate::test::U24_MAX as u64, + read_u24, + write_u24 + ); + qc_byte_order!( + prop_i24, + i32, + crate::test::I24_MAX as u64, + read_i24, + write_i24 + ); + qc_byte_order!( + prop_u32, + u32, + ::core::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_byte_order!( + prop_i32, + i32, + ::core::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_byte_order!( + prop_u48, + u64, + crate::test::U48_MAX as u64, + read_u48, + write_u48 + ); + qc_byte_order!( + prop_i48, + i64, + crate::test::I48_MAX as u64, + read_i48, + write_i48 + ); + qc_byte_order!( + prop_u64, + u64, + ::core::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_byte_order!( + prop_i64, + i64, + ::core::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_byte_order!( + prop_f32, + f32, + ::core::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_byte_order!( + prop_f64, + f64, + ::core::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_byte_order!(prop_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_byte_order!(prop_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_byte_order!(prop_uint_1, - u64, calc_max!(super::U64_MAX, 1), 1, read_uint, write_uint); - qc_byte_order!(prop_uint_2, - u64, calc_max!(super::U64_MAX, 2), 2, read_uint, write_uint); - qc_byte_order!(prop_uint_3, - u64, calc_max!(super::U64_MAX, 3), 3, read_uint, write_uint); - qc_byte_order!(prop_uint_4, - u64, calc_max!(super::U64_MAX, 4), 4, read_uint, write_uint); - qc_byte_order!(prop_uint_5, - u64, calc_max!(super::U64_MAX, 5), 5, read_uint, write_uint); - qc_byte_order!(prop_uint_6, - u64, calc_max!(super::U64_MAX, 6), 6, read_uint, write_uint); - qc_byte_order!(prop_uint_7, - u64, calc_max!(super::U64_MAX, 7), 7, read_uint, write_uint); - qc_byte_order!(prop_uint_8, - u64, calc_max!(super::U64_MAX, 8), 8, read_uint, write_uint); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_1, - Wi128, 1, 1, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_2, - Wi128, 2, 2, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_3, - Wi128, 3, 3, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_4, - Wi128, 4, 4, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_5, - Wi128, 5, 5, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_6, - Wi128, 6, 6, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_7, - Wi128, 7, 7, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_8, - Wi128, 8, 8, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_9, - Wi128, 9, 9, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_10, - Wi128, 10, 10, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_11, - Wi128, 11, 11, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_12, - Wi128, 12, 12, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_13, - Wi128, 13, 13, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_14, - Wi128, 14, 14, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_15, - Wi128, 15, 15, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_16, - Wi128, 16, 16, read_uint128, write_uint128); - - qc_byte_order!(prop_int_1, - i64, calc_max!(super::I64_MAX, 1), 1, read_int, write_int); - qc_byte_order!(prop_int_2, - i64, calc_max!(super::I64_MAX, 2), 2, read_int, write_int); - qc_byte_order!(prop_int_3, - i64, calc_max!(super::I64_MAX, 3), 3, read_int, write_int); - qc_byte_order!(prop_int_4, - i64, calc_max!(super::I64_MAX, 4), 4, read_int, write_int); - qc_byte_order!(prop_int_5, - i64, calc_max!(super::I64_MAX, 5), 5, read_int, write_int); - qc_byte_order!(prop_int_6, - i64, calc_max!(super::I64_MAX, 6), 6, read_int, write_int); - qc_byte_order!(prop_int_7, - i64, calc_max!(super::I64_MAX, 7), 7, read_int, write_int); - qc_byte_order!(prop_int_8, - i64, calc_max!(super::I64_MAX, 8), 8, read_int, write_int); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_1, - Wi128, 1, 1, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_2, - Wi128, 2, 2, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_3, - Wi128, 3, 3, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_4, - Wi128, 4, 4, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_5, - Wi128, 5, 5, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_6, - Wi128, 6, 6, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_7, - Wi128, 7, 7, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_8, - Wi128, 8, 8, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_9, - Wi128, 9, 9, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_10, - Wi128, 10, 10, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_11, - Wi128, 11, 11, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_12, - Wi128, 12, 12, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_13, - Wi128, 13, 13, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_14, - Wi128, 14, 14, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_15, - Wi128, 15, 15, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_16, - Wi128, 16, 16, read_int128, write_int128); - + qc_byte_order!( + prop_uint_1, + u64, + calc_max!(super::U64_MAX, 1), + 1, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_2, + u64, + calc_max!(super::U64_MAX, 2), + 2, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_3, + u64, + calc_max!(super::U64_MAX, 3), + 3, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_4, + u64, + calc_max!(super::U64_MAX, 4), + 4, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_5, + u64, + calc_max!(super::U64_MAX, 5), + 5, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_6, + u64, + calc_max!(super::U64_MAX, 6), + 6, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_7, + u64, + calc_max!(super::U64_MAX, 7), + 7, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_8, + u64, + calc_max!(super::U64_MAX, 8), + 8, + read_uint, + write_uint + ); + + qc_byte_order!( + prop_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_uint128 + ); + + qc_byte_order!( + prop_int_1, + i64, + calc_max!(super::I64_MAX, 1), + 1, + read_int, + write_int + ); + qc_byte_order!( + prop_int_2, + i64, + calc_max!(super::I64_MAX, 2), + 2, + read_int, + write_int + ); + qc_byte_order!( + prop_int_3, + i64, + calc_max!(super::I64_MAX, 3), + 3, + read_int, + write_int + ); + qc_byte_order!( + prop_int_4, + i64, + calc_max!(super::I64_MAX, 4), + 4, + read_int, + write_int + ); + qc_byte_order!( + prop_int_5, + i64, + calc_max!(super::I64_MAX, 5), + 5, + read_int, + write_int + ); + qc_byte_order!( + prop_int_6, + i64, + calc_max!(super::I64_MAX, 6), + 6, + read_int, + write_int + ); + qc_byte_order!( + prop_int_7, + i64, + calc_max!(super::I64_MAX, 7), + 7, + read_int, + write_int + ); + qc_byte_order!( + prop_int_8, + i64, + calc_max!(super::I64_MAX, 8), + 8, + read_int, + write_int + ); + + qc_byte_order!( + prop_int128_1, + Wi128, + 1, + 1, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_2, + Wi128, + 2, + 2, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_3, + Wi128, + 3, + 3, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_4, + Wi128, + 4, + 4, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_5, + Wi128, + 5, + 5, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_6, + Wi128, + 6, + 6, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_7, + Wi128, + 7, + 7, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_8, + Wi128, + 8, + 8, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_9, + Wi128, + 9, + 9, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_10, + Wi128, + 10, + 10, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_11, + Wi128, + 11, + 11, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_12, + Wi128, + 12, + 12, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_13, + Wi128, + 13, + 13, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_14, + Wi128, + 14, + 14, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_15, + Wi128, + 15, + 15, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_16, + Wi128, + 16, + 16, + read_int128, + write_int128 + ); // Test that all of the byte conversion functions panic when given a // buffer that is too small. @@ -2688,9 +3002,11 @@ mod test { // with a buffer overflow. macro_rules! too_small { ($name:ident, $maximally_small:expr, $zero:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2734,10 +3050,12 @@ mod test { NativeEndian::$write(&mut buf, $zero); } } - ); - ($name:ident, $maximally_small:expr, $read:ident) => ( + }; + ($name:ident, $maximally_small:expr, $read:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2760,7 +3078,7 @@ mod test { NativeEndian::$read(&buf, $maximally_small + 1); } } - ); + }; } too_small!(small_u16, 1, 0, read_u16, write_u16); @@ -2771,9 +3089,7 @@ mod test { too_small!(small_i64, 7, 0, read_i64, write_i64); too_small!(small_f32, 3, 0.0, read_f32, write_f32); too_small!(small_f64, 7, 0.0, read_f64, write_f64); - #[cfg(byteorder_i128)] too_small!(small_u128, 15, 0, read_u128, write_u128); - #[cfg(byteorder_i128)] too_small!(small_i128, 15, 0, read_i128, write_i128); too_small!(small_uint_1, 1, read_uint); @@ -2784,35 +3100,20 @@ mod test { too_small!(small_uint_6, 6, read_uint); too_small!(small_uint_7, 7, read_uint); - #[cfg(byteorder_i128)] too_small!(small_uint128_1, 1, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_2, 2, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_3, 3, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_4, 4, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_5, 5, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_6, 6, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_7, 7, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_8, 8, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_9, 9, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_10, 10, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_11, 11, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_12, 12, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_13, 13, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_14, 14, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_15, 15, read_uint128); too_small!(small_int_1, 1, read_int); @@ -2823,35 +3124,20 @@ mod test { too_small!(small_int_6, 6, read_int); too_small!(small_int_7, 7, read_int); - #[cfg(byteorder_i128)] too_small!(small_int128_1, 1, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_2, 2, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_3, 3, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_4, 4, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_5, 5, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_6, 6, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_7, 7, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_8, 8, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_9, 9, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_10, 10, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_11, 11, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_12, 12, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_13, 13, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_14, 14, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_15, 15, read_int128); // Test that reading/writing slices enforces the correct lengths. @@ -2859,7 +3145,9 @@ mod test { ($name:ident, $read:ident, $write:ident, $num_bytes:expr, $numbers:expr) => { mod $name { - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2909,54 +3197,171 @@ mod test { NativeEndian::$write(&numbers, &mut bytes); } } - } + }; } slice_lengths!( - slice_len_too_small_u16, read_u16_into, write_u16_into, 3, [0, 0]); + slice_len_too_small_u16, + read_u16_into, + write_u16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u16, read_u16_into, write_u16_into, 5, [0, 0]); + slice_len_too_big_u16, + read_u16_into, + write_u16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i16, read_i16_into, write_i16_into, 3, [0, 0]); + slice_len_too_small_i16, + read_i16_into, + write_i16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i16, read_i16_into, write_i16_into, 5, [0, 0]); + slice_len_too_big_i16, + read_i16_into, + write_i16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u32, read_u32_into, write_u32_into, 7, [0, 0]); + slice_len_too_small_u32, + read_u32_into, + write_u32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u32, read_u32_into, write_u32_into, 9, [0, 0]); + slice_len_too_big_u32, + read_u32_into, + write_u32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i32, read_i32_into, write_i32_into, 7, [0, 0]); + slice_len_too_small_i32, + read_i32_into, + write_i32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i32, read_i32_into, write_i32_into, 9, [0, 0]); + slice_len_too_big_i32, + read_i32_into, + write_i32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u64, read_u64_into, write_u64_into, 15, [0, 0]); + slice_len_too_small_u64, + read_u64_into, + write_u64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u64, read_u64_into, write_u64_into, 17, [0, 0]); + slice_len_too_big_u64, + read_u64_into, + write_u64_into, + 17, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i64, read_i64_into, write_i64_into, 15, [0, 0]); + slice_len_too_small_i64, + read_i64_into, + write_i64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i64, read_i64_into, write_i64_into, 17, [0, 0]); + slice_len_too_big_i64, + read_i64_into, + write_i64_into, + 17, + [0, 0] + ); - #[cfg(byteorder_i128)] slice_lengths!( - slice_len_too_small_u128, read_u128_into, write_u128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_u128, + read_u128_into, + write_u128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u128, read_u128_into, write_u128_into, 33, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_big_u128, + read_u128_into, + write_u128_into, + 33, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i128, read_i128_into, write_i128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_i128, + read_i128_into, + write_i128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i128, read_i128_into, write_i128_into, 33, [0, 0]); + slice_len_too_big_i128, + read_i128_into, + write_i128_into, + 33, + [0, 0] + ); #[test] fn uint_bigger_buffer() { - use {ByteOrder, LittleEndian}; + use crate::{ByteOrder, LittleEndian}; let n = LittleEndian::read_uint(&[1, 2, 3, 4, 5, 6, 7, 8], 5); - assert_eq!(n, 0x0504030201); + assert_eq!(n, 0x05_0403_0201); + } + + #[test] + fn regression173_array_impl() { + use crate::{BigEndian, ByteOrder, LittleEndian}; + + let xs = [0; 100]; + + let x = BigEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i128(&xs); + assert_eq!(x, 0); + + let x = LittleEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i128(&xs); + assert_eq!(x, 0); } } @@ -2970,7 +3375,6 @@ mod stdtests { use self::rand::thread_rng; fn qc_unsized(f: A) { - QuickCheck::new() .gen(StdGen::new(thread_rng(), 16)) .tests(1_00) @@ -2979,19 +3383,22 @@ mod stdtests { } macro_rules! calc_max { - ($max:expr, $bytes:expr) => { ($max - 1) >> (8 * (8 - $bytes)) }; + ($max:expr, $bytes:expr) => { + ($max - 1) >> (8 * (8 - $bytes)) + }; } macro_rules! qc_bytes_ext { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3032,15 +3439,16 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); - ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => ( + }; + ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3075,188 +3483,484 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_bytes_ext!(prop_ext_u16, - u16, ::std::u16::MAX as u64, read_u16, write_u16); - qc_bytes_ext!(prop_ext_i16, - i16, ::std::i16::MAX as u64, read_i16, write_i16); - qc_bytes_ext!(prop_ext_u32, - u32, ::std::u32::MAX as u64, read_u32, write_u32); - qc_bytes_ext!(prop_ext_i32, - i32, ::std::i32::MAX as u64, read_i32, write_i32); - qc_bytes_ext!(prop_ext_u64, - u64, ::std::u64::MAX as u64, read_u64, write_u64); - qc_bytes_ext!(prop_ext_i64, - i64, ::std::i64::MAX as u64, read_i64, write_i64); - qc_bytes_ext!(prop_ext_f32, - f32, ::std::u64::MAX as u64, read_f32, write_f32); - qc_bytes_ext!(prop_ext_f64, - f64, ::std::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_bytes_ext!( + prop_ext_u16, + u16, + ::std::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_bytes_ext!( + prop_ext_i16, + i16, + ::std::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_bytes_ext!( + prop_ext_u32, + u32, + ::std::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_bytes_ext!( + prop_ext_i32, + i32, + ::std::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_bytes_ext!( + prop_ext_u64, + u64, + ::std::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_bytes_ext!( + prop_ext_i64, + i64, + ::std::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_bytes_ext!( + prop_ext_f32, + f32, + ::std::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_bytes_ext!( + prop_ext_f64, + f64, + ::std::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_bytes_ext!(prop_ext_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_bytes_ext!(prop_ext_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_bytes_ext!(prop_ext_uint_1, - u64, calc_max!(::test::U64_MAX, 1), 1, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_2, - u64, calc_max!(::test::U64_MAX, 2), 2, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_3, - u64, calc_max!(::test::U64_MAX, 3), 3, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_4, - u64, calc_max!(::test::U64_MAX, 4), 4, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_5, - u64, calc_max!(::test::U64_MAX, 5), 5, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_6, - u64, calc_max!(::test::U64_MAX, 6), 6, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_7, - u64, calc_max!(::test::U64_MAX, 7), 7, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_8, - u64, calc_max!(::test::U64_MAX, 8), 8, read_uint, write_u64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_1, - Wi128, 1, 1, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_2, - Wi128, 2, 2, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_3, - Wi128, 3, 3, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_4, - Wi128, 4, 4, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_5, - Wi128, 5, 5, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_6, - Wi128, 6, 6, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_7, - Wi128, 7, 7, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_8, - Wi128, 8, 8, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_9, - Wi128, 9, 9, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_10, - Wi128, 10, 10, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_11, - Wi128, 11, 11, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_12, - Wi128, 12, 12, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_13, - Wi128, 13, 13, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_14, - Wi128, 14, 14, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_15, - Wi128, 15, 15, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_16, - Wi128, 16, 16, read_uint128, write_u128); - - qc_bytes_ext!(prop_ext_int_1, - i64, calc_max!(::test::I64_MAX, 1), 1, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_2, - i64, calc_max!(::test::I64_MAX, 2), 2, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_3, - i64, calc_max!(::test::I64_MAX, 3), 3, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_4, - i64, calc_max!(::test::I64_MAX, 4), 4, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_5, - i64, calc_max!(::test::I64_MAX, 5), 5, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_6, - i64, calc_max!(::test::I64_MAX, 6), 6, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_7, - i64, calc_max!(::test::I64_MAX, 1), 7, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_8, - i64, calc_max!(::test::I64_MAX, 8), 8, read_int, write_i64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_1, - Wi128, 1, 1, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_2, - Wi128, 2, 2, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_3, - Wi128, 3, 3, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_4, - Wi128, 4, 4, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_5, - Wi128, 5, 5, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_6, - Wi128, 6, 6, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_7, - Wi128, 7, 7, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_8, - Wi128, 8, 8, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_9, - Wi128, 9, 9, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_10, - Wi128, 10, 10, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_11, - Wi128, 11, 11, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_12, - Wi128, 12, 12, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_13, - Wi128, 13, 13, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_14, - Wi128, 14, 14, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_15, - Wi128, 15, 15, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_16, - Wi128, 16, 16, read_int128, write_i128); + qc_bytes_ext!( + prop_ext_uint_1, + u64, + calc_max!(crate::test::U64_MAX, 1), + 1, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_2, + u64, + calc_max!(crate::test::U64_MAX, 2), + 2, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_3, + u64, + calc_max!(crate::test::U64_MAX, 3), + 3, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_4, + u64, + calc_max!(crate::test::U64_MAX, 4), + 4, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_5, + u64, + calc_max!(crate::test::U64_MAX, 5), + 5, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_6, + u64, + calc_max!(crate::test::U64_MAX, 6), + 6, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_7, + u64, + calc_max!(crate::test::U64_MAX, 7), + 7, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_8, + u64, + calc_max!(crate::test::U64_MAX, 8), + 8, + read_uint, + write_u64 + ); + + qc_bytes_ext!( + prop_ext_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_u128 + ); + + qc_bytes_ext!( + prop_ext_int_1, + i64, + calc_max!(crate::test::I64_MAX, 1), + 1, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_2, + i64, + calc_max!(crate::test::I64_MAX, 2), + 2, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_3, + i64, + calc_max!(crate::test::I64_MAX, 3), + 3, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_4, + i64, + calc_max!(crate::test::I64_MAX, 4), + 4, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_5, + i64, + calc_max!(crate::test::I64_MAX, 5), + 5, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_6, + i64, + calc_max!(crate::test::I64_MAX, 6), + 6, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_7, + i64, + calc_max!(crate::test::I64_MAX, 1), + 7, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_8, + i64, + calc_max!(crate::test::I64_MAX, 8), + 8, + read_int, + write_i64 + ); + + qc_bytes_ext!( + prop_ext_int128_1, + Wi128, + 1, + 1, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_2, + Wi128, + 2, + 2, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_3, + Wi128, + 3, + 3, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_4, + Wi128, + 4, + 4, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_5, + Wi128, + 5, + 5, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_6, + Wi128, + 6, + 6, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_7, + Wi128, + 7, + 7, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_8, + Wi128, + 8, + 8, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_9, + Wi128, + 9, + 9, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_10, + Wi128, + 10, + 10, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_11, + Wi128, + 11, + 11, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_12, + Wi128, + 12, + 12, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_13, + Wi128, + 13, + 13, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_14, + Wi128, + 14, + 14, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_15, + Wi128, + 15, + 15, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_16, + Wi128, + 16, + 16, + read_int128, + write_i128 + ); // Test slice serialization/deserialization. macro_rules! qc_slice { ($name:ident, $ty_int:ty, $read:ident, $write:ident, $zero:expr) => { mod $name { - use core::mem::size_of; - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; use super::qc_unsized; #[allow(unused_imports)] - use test::Wi128; + use crate::test::Wi128; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use core::mem::size_of; #[test] fn big_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; BigEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { BigEndian::$read(&bytes, &mut got); } + unsafe { + BigEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3267,17 +3971,17 @@ mod stdtests { fn little_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; LittleEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { LittleEndian::$read(&bytes, &mut got); } + unsafe { + LittleEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3288,24 +3992,24 @@ mod stdtests { fn native_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; NativeEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { NativeEndian::$read(&bytes, &mut got); } + unsafe { + NativeEndian::$read(&bytes, &mut got); + } numbers == got } qc_unsized(prop as fn(_) -> bool); } } - } + }; } qc_slice!(prop_slice_u16, u16, read_u16_into, write_u16_into, 0); @@ -3314,15 +4018,21 @@ mod stdtests { qc_slice!(prop_slice_i32, i32, read_i32_into, write_i32_into, 0); qc_slice!(prop_slice_u64, u64, read_u64_into, write_u64_into, 0); qc_slice!(prop_slice_i64, i64, read_i64_into, write_i64_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_u128, Wi128, read_u128_into, write_u128_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_i128, Wi128, read_i128_into, write_i128_into, 0); - qc_slice!( - prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + prop_slice_u128, + Wi128, + read_u128_into, + write_u128_into, + 0 + ); qc_slice!( - prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); + prop_slice_i128, + Wi128, + read_i128_into, + write_i128_into, + 0 + ); + + qc_slice!(prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + qc_slice!(prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); } -- cgit v1.2.3 From 908be7e9f6cd8b4107b9438b310b6eaacaccc9cc Mon Sep 17 00:00:00 2001 From: Yi Kong Date: Sun, 14 Feb 2021 22:04:08 +0800 Subject: Update to 1.4.2 Change-Id: I9b660b3276276d55040849a3cd6e5b70dbcd513b Merged-In: I9b660b3276276d55040849a3cd6e5b70dbcd513b (cherry picked from commit 6d7927fcf9f2566d106f4d23fa0d29bd830b9bb7) --- .cargo_vcs_info.json | 2 +- .github/workflows/ci.yml | 151 ++++ Android.bp | 10 +- CHANGELOG.md | 25 + Cargo.toml | 15 +- Cargo.toml.orig | 14 +- LICENSE | 1 + METADATA | 19 + MODULE_LICENSE_MIT | 0 OWNERS | 1 + README.md | 6 +- benches/bench.rs | 236 +++--- build.rs | 87 --- rustfmt.toml | 2 + src/io.rs | 92 +-- src/lib.rs | 1794 ++++++++++++++++++++++++++++++++-------------- 16 files changed, 1623 insertions(+), 832 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 120000 LICENSE create mode 100644 METADATA create mode 100644 MODULE_LICENSE_MIT create mode 100644 OWNERS delete mode 100644 build.rs create mode 100644 rustfmt.toml diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index e7e8b81..e260c24 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "058237661a88c2f610f280340310fce19d98d265" + "sha1": "ca8c10a3f85e2c66781f7d12e90196e6923592d7" } } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2c2c790 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,151 @@ +name: ci +on: + pull_request: + push: + branches: + - master + schedule: + - cron: '00 01 * * *' +jobs: + test: + name: test + env: + # For some builds, we use cross to test on 32-bit and big-endian + # systems. + CARGO: cargo + # When CARGO is set to CROSS, TARGET is set to `--target matrix.target`. + TARGET: + runs-on: ${{ matrix.os }} + strategy: + matrix: + build: + - pinned + - stable + - stable-32 + - stable-mips + - beta + - nightly + - macos + - win-msvc + - win-gnu + include: + - build: pinned + os: ubuntu-18.04 + rust: 1.41.1 + - build: stable + os: ubuntu-18.04 + rust: stable + - build: stable-32 + os: ubuntu-18.04 + rust: stable + target: i686-unknown-linux-gnu + - build: stable-mips + os: ubuntu-18.04 + rust: stable + target: mips64-unknown-linux-gnuabi64 + - build: beta + os: ubuntu-18.04 + rust: beta + - build: nightly + os: ubuntu-18.04 + rust: nightly + - build: macos + os: macos-latest + rust: stable + - build: win-msvc + os: windows-2019 + rust: stable + - build: win-gnu + os: windows-2019 + rust: stable-x86_64-gnu + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + profile: minimal + override: true + + - name: Use Cross + if: matrix.target != '' + run: | + # FIXME: to work around bugs in latest cross release, install master. + # See: https://github.com/rust-embedded/cross/issues/357 + cargo install --git https://github.com/rust-embedded/cross + echo "CARGO=cross" >> $GITHUB_ENV + echo "TARGET=--target ${{ matrix.target }}" >> $GITHUB_ENV + + - name: Show command used for Cargo + run: | + echo "cargo command is: ${{ env.CARGO }}" + echo "target flag is: ${{ env.TARGET }}" + + - name: Show CPU info for debugging + if: matrix.os == 'ubuntu-18.04' + run: lscpu + + - name: Build + run: ${{ env.CARGO }} build --verbose $TARGET + + - name: Build (no default) + run: ${{ env.CARGO }} build --verbose $TARGET --no-default-features + + - name: Build docs + run: ${{ env.CARGO }} doc --verbose $TARGET + + # Our dev dependencies evolve more rapidly than we'd like, so only run + # tests when we aren't pinning the Rust version. + - name: Tests + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose $TARGET + + - name: Tests (no default, lib only) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --lib $TARGET + + - name: Tests (i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --features i128 $TARGET + + - name: Tests (no default, lib only, i128) + if: matrix.build != 'pinned' + run: ${{ env.CARGO }} test --verbose --no-default-features --features i128 --lib $TARGET + + - name: Compile benchmarks + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run $TARGET + + - name: Compile benchmarks (no default) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features $TARGET + + - name: Compile benchmarks (i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --features i128 $TARGET + + - name: Compile benchmarks (no default, i128) + if: matrix.build == 'nightly' + run: cargo bench --verbose --no-run --no-default-features --features i128 $TARGET + + rustfmt: + name: rustfmt + runs-on: ubuntu-18.04 + steps: + - name: Checkout repository + uses: actions/checkout@v1 + with: + fetch-depth: 1 + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + profile: minimal + components: rustfmt + - name: Check formatting + run: cargo fmt -- --check diff --git a/Android.bp b/Android.bp index 72ef217..8f42c1a 100644 --- a/Android.bp +++ b/Android.bp @@ -1,17 +1,13 @@ -// This file is generated by cargo2android.py. +// This file is generated by cargo2android.py --run --device --dependencies. -rust_library_rlib { +rust_library { name: "libbyteorder", - deny_warnings: false, host_supported: true, crate_name: "byteorder", srcs: ["src/lib.rs"], - edition: "2015", + edition: "2018", features: [ "default", "std", ], - flags: [ - "--cfg byteorder_i128", - ], } diff --git a/CHANGELOG.md b/CHANGELOG.md index 020beb4..6b51eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,28 @@ +1.3.4 +===== +This patch release squashes deprecation warnings for the `try!` macro, in +accordance with byteorder's minimum supported Rust version (currently at Rust +1.12.0). + + +1.3.3 +===== +This patch release adds `ByteOrder::write_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.2 +===== +This patch release adds `ReadBytesExt::read_i8_into()` as a simple, safe interface +for ordinarily unsafe or tedious code. + + +1.3.1 +===== +This minor release performs mostly small internal changes. Going forward, these +are not going to be incorporated into the changelog. + + 1.3.0 ===== This new minor release now enables `i128` support automatically on Rust diff --git a/Cargo.toml b/Cargo.toml index fb2acad..ff60b60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,17 +11,17 @@ # will likely look very different (and much more reasonable) [package] +edition = "2018" name = "byteorder" -version = "1.3.2" +version = "1.4.2" authors = ["Andrew Gallant "] -build = "build.rs" exclude = ["/ci/*"] description = "Library for reading/writing numbers in big-endian and little-endian." homepage = "https://github.com/BurntSushi/byteorder" documentation = "https://docs.rs/byteorder" readme = "README.md" keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] license = "Unlicense OR MIT" repository = "https://github.com/BurntSushi/byteorder" [profile.bench] @@ -30,19 +30,14 @@ opt-level = 3 [lib] name = "byteorder" bench = false -[dev-dependencies.doc-comment] -version = "0.3" - [dev-dependencies.quickcheck] -version = "0.8" +version = "0.9.2" default-features = false [dev-dependencies.rand] -version = "0.6" +version = "0.7" [features] default = ["std"] i128 = [] std = [] -[badges.travis-ci] -repository = "BurntSushi/byteorder" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 91f2aa3..efeafb2 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,26 +1,25 @@ [package] name = "byteorder" -version = "1.3.2" #:version +version = "1.4.2" #:version authors = ["Andrew Gallant "] description = "Library for reading/writing numbers in big-endian and little-endian." documentation = "https://docs.rs/byteorder" homepage = "https://github.com/BurntSushi/byteorder" repository = "https://github.com/BurntSushi/byteorder" readme = "README.md" -categories = ["encoding", "parsing"] +categories = ["encoding", "parsing", "no-std"] keywords = ["byte", "endian", "big-endian", "little-endian", "binary"] license = "Unlicense OR MIT" exclude = ["/ci/*"] -build = "build.rs" +edition = "2018" [lib] name = "byteorder" bench = false [dev-dependencies] -quickcheck = { version = "0.8", default-features = false } -rand = "0.6" -doc-comment = "0.3" +quickcheck = { version = "0.9.2", default-features = false } +rand = "0.7" [features] default = ["std"] @@ -33,6 +32,3 @@ i128 = [] [profile.bench] opt-level = 3 - -[badges] -travis-ci = { repository = "BurntSushi/byteorder" } diff --git a/LICENSE b/LICENSE new file mode 120000 index 0000000..7f9a88e --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +LICENSE-MIT \ No newline at end of file diff --git a/METADATA b/METADATA new file mode 100644 index 0000000..95a62dd --- /dev/null +++ b/METADATA @@ -0,0 +1,19 @@ +name: "byteorder" +description: "Library for reading/writing numbers in big-endian and little-endian." +third_party { + url { + type: HOMEPAGE + value: "https://crates.io/crates/byteorder" + } + url { + type: ARCHIVE + value: "https://static.crates.io/crates/byteorder/byteorder-1.4.2.crate" + } + version: "1.4.2" + license_type: NOTICE + last_upgrade_date { + year: 2021 + month: 2 + day: 7 + } +} diff --git a/MODULE_LICENSE_MIT b/MODULE_LICENSE_MIT new file mode 100644 index 0000000..e69de29 diff --git a/OWNERS b/OWNERS new file mode 100644 index 0000000..46fc303 --- /dev/null +++ b/OWNERS @@ -0,0 +1 @@ +include platform/prebuilts/rust:/OWNERS diff --git a/README.md b/README.md index 8940b29..8b2ecc4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ +byteorder +========= This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order. -[![Build status](https://api.travis-ci.org/BurntSushi/byteorder.svg)](https://travis-ci.org/BurntSushi/byteorder) +[![Build status](https://github.com/BurntSushi/byteorder/workflows/ci/badge.svg)](https://github.com/BurntSushi/byteorder/actions) [![](http://meritbadge.herokuapp.com/byteorder)](https://crates.io/crates/byteorder) Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org). @@ -27,8 +29,6 @@ If you want to augment existing `Read` and `Write` traits, then import the extension methods like so: ```rust -extern crate byteorder; - use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian, LittleEndian}; ``` diff --git a/benches/bench.rs b/benches/bench.rs index d53d25e..bb00422 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,15 +1,15 @@ #![feature(test)] -extern crate byteorder; -extern crate rand; extern crate test; macro_rules! bench_num { - ($name:ident, $read:ident, $bytes:expr, $data:expr) => ( + ($name:ident, $read:ident, $bytes:expr, $data:expr) => { mod $name { - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -43,14 +43,16 @@ macro_rules! bench_num { }); } } - ); + }; ($ty:ident, $max:ident, - $read:ident, $write:ident, $size:expr, $data:expr) => ( + $read:ident, $write:ident, $size:expr, $data:expr) => { mod $ty { + use byteorder::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use std::$ty; - use byteorder::{ByteOrder, BigEndian, NativeEndian, LittleEndian}; - use super::test::Bencher; - use super::test::black_box as bb; + use test::black_box as bb; + use test::Bencher; const NITER: usize = 100_000; @@ -117,7 +119,7 @@ macro_rules! bench_num { }); } } - ); + }; } bench_num!(u16, MAX, read_u16, write_u16, 2, [1, 2]); @@ -127,8 +129,7 @@ bench_num!(i32, MAX, read_i32, write_i32, 4, [1, 2, 3, 4]); bench_num!(u64, MAX, read_u64, write_u64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(i64, MAX, read_i64, write_i64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(f32, MAX, read_f32, write_f32, 4, [1, 2, 3, 4]); -bench_num!(f64, MAX, read_f64, write_f64, 8, - [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(f64, MAX, read_f64, write_f64, 8, [1, 2, 3, 4, 5, 6, 7, 8]); bench_num!(uint_1, read_uint, 1, [1]); bench_num!(uint_2, read_uint, 2, [1, 2]); @@ -148,120 +149,115 @@ bench_num!(int_6, read_int, 6, [1, 2, 3, 4, 5, 6]); bench_num!(int_7, read_int, 7, [1, 2, 3, 4, 5, 6, 7]); bench_num!(int_8, read_int, 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(u128, MAX, read_u128, write_u128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); -#[cfg(byteorder_i128)] -bench_num!(i128, MAX, read_i128, write_i128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); - -#[cfg(byteorder_i128)] -bench_num!(uint128_1, read_uint128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(uint128_2, read_uint128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(uint128_3, read_uint128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(uint128_4, read_uint128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(uint128_5, read_uint128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(uint128_6, read_uint128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(uint128_7, read_uint128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(uint128_8, read_uint128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(uint128_9, read_uint128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(uint128_10, read_uint128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(uint128_11, read_uint128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(uint128_12, read_uint128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(uint128_13, read_uint128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(uint128_14, read_uint128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(uint128_15, read_uint128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(uint128_16, read_uint128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!( + u128, + MAX, + read_u128, + write_u128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!( + i128, + MAX, + read_i128, + write_i128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); -#[cfg(byteorder_i128)] -bench_num!(int128_1, read_int128, - 1, [1]); -#[cfg(byteorder_i128)] -bench_num!(int128_2, read_int128, - 2, [1, 2]); -#[cfg(byteorder_i128)] -bench_num!(int128_3, read_int128, - 3, [1, 2, 3]); -#[cfg(byteorder_i128)] -bench_num!(int128_4, read_int128, - 4, [1, 2, 3, 4]); -#[cfg(byteorder_i128)] -bench_num!(int128_5, read_int128, - 5, [1, 2, 3, 4, 5]); -#[cfg(byteorder_i128)] -bench_num!(int128_6, read_int128, - 6, [1, 2, 3, 4, 5, 6]); -#[cfg(byteorder_i128)] -bench_num!(int128_7, read_int128, - 7, [1, 2, 3, 4, 5, 6, 7]); -#[cfg(byteorder_i128)] -bench_num!(int128_8, read_int128, - 8, [1, 2, 3, 4, 5, 6, 7, 8]); -#[cfg(byteorder_i128)] -bench_num!(int128_9, read_int128, - 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); -#[cfg(byteorder_i128)] -bench_num!(int128_10, read_int128, - 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); -#[cfg(byteorder_i128)] -bench_num!(int128_11, read_int128, - 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); -#[cfg(byteorder_i128)] -bench_num!(int128_12, read_int128, - 12, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); -#[cfg(byteorder_i128)] -bench_num!(int128_13, read_int128, - 13, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); -#[cfg(byteorder_i128)] -bench_num!(int128_14, read_int128, - 14, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]); -#[cfg(byteorder_i128)] -bench_num!(int128_15, read_int128, - 15, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); -#[cfg(byteorder_i128)] -bench_num!(int128_16, read_int128, - 16, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); +bench_num!(uint128_1, read_uint128, 1, [1]); +bench_num!(uint128_2, read_uint128, 2, [1, 2]); +bench_num!(uint128_3, read_uint128, 3, [1, 2, 3]); +bench_num!(uint128_4, read_uint128, 4, [1, 2, 3, 4]); +bench_num!(uint128_5, read_uint128, 5, [1, 2, 3, 4, 5]); +bench_num!(uint128_6, read_uint128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(uint128_7, read_uint128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(uint128_8, read_uint128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(uint128_9, read_uint128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(uint128_10, read_uint128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(uint128_11, read_uint128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + uint128_12, + read_uint128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + uint128_13, + read_uint128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + uint128_14, + read_uint128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + uint128_15, + read_uint128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + uint128_16, + read_uint128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); +bench_num!(int128_1, read_int128, 1, [1]); +bench_num!(int128_2, read_int128, 2, [1, 2]); +bench_num!(int128_3, read_int128, 3, [1, 2, 3]); +bench_num!(int128_4, read_int128, 4, [1, 2, 3, 4]); +bench_num!(int128_5, read_int128, 5, [1, 2, 3, 4, 5]); +bench_num!(int128_6, read_int128, 6, [1, 2, 3, 4, 5, 6]); +bench_num!(int128_7, read_int128, 7, [1, 2, 3, 4, 5, 6, 7]); +bench_num!(int128_8, read_int128, 8, [1, 2, 3, 4, 5, 6, 7, 8]); +bench_num!(int128_9, read_int128, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]); +bench_num!(int128_10, read_int128, 10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); +bench_num!(int128_11, read_int128, 11, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); +bench_num!( + int128_12, + read_int128, + 12, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] +); +bench_num!( + int128_13, + read_int128, + 13, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] +); +bench_num!( + int128_14, + read_int128, + 14, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] +); +bench_num!( + int128_15, + read_int128, + 15, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] +); +bench_num!( + int128_16, + read_int128, + 16, + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] +); macro_rules! bench_slice { ($name:ident, $numty:ty, $read:ident, $write:ident) => { mod $name { use std::mem::size_of; - use byteorder::{ByteOrder, BigEndian, LittleEndian}; - use rand::{self, Rng}; + use byteorder::{BigEndian, ByteOrder, LittleEndian}; use rand::distributions; + use rand::{self, Rng}; use test::Bencher; #[bench] @@ -322,7 +318,7 @@ macro_rules! bench_slice { }); } } - } + }; } bench_slice!(slice_u64, u64, read_u64_into, write_u64_into); diff --git a/build.rs b/build.rs deleted file mode 100644 index 002135b..0000000 --- a/build.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::env; -use std::ffi::OsString; -use std::io::{self, Write}; -use std::process::Command; - -fn main() { - let version = match Version::read() { - Ok(version) => version, - Err(err) => { - writeln!( - &mut io::stderr(), - "failed to parse `rustc --version`: {}", - err - ).unwrap(); - return; - } - }; - enable_i128(version); -} - -fn enable_i128(version: Version) { - if version < (Version { major: 1, minor: 26, patch: 0 }) { - return; - } - - println!("cargo:rustc-cfg=byteorder_i128"); -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] -struct Version { - major: u32, - minor: u32, - patch: u32, -} - -impl Version { - fn read() -> Result { - let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); - let output = Command::new(&rustc) - .arg("--version") - .output() - .unwrap() - .stdout; - Version::parse(&String::from_utf8(output).unwrap()) - } - - fn parse(mut s: &str) -> Result { - if !s.starts_with("rustc ") { - return Err(format!("unrecognized version string: {}", s)); - } - s = &s["rustc ".len()..]; - - let parts: Vec<&str> = s.split(".").collect(); - if parts.len() < 3 { - return Err(format!("not enough version parts: {:?}", parts)); - } - - let mut num = String::new(); - for c in parts[0].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let major = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[1].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let minor = try!(num.parse::().map_err(|e| e.to_string())); - - num.clear(); - for c in parts[2].chars() { - if !c.is_digit(10) { - break; - } - num.push(c); - } - let patch = try!(num.parse::().map_err(|e| e.to_string())); - - Ok(Version { major: major, minor: minor, patch: patch }) - } -} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..aa37a21 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 79 +use_small_heuristics = "max" diff --git a/src/io.rs b/src/io.rs index ed6a848..ac41ff7 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,7 +1,9 @@ -use std::io::{self, Result}; -use std::slice; +use std::{ + io::{self, Result}, + slice, +}; -use ByteOrder; +use crate::ByteOrder; /// Extends [`Read`] with methods for reading numbers. (For `std::io`.) /// @@ -52,7 +54,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0]) } @@ -82,7 +84,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i8(&mut self) -> Result { let mut buf = [0; 1]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(buf[0] as i8) } @@ -109,7 +111,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u16(&buf)) } @@ -136,7 +138,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i16(&mut self) -> Result { let mut buf = [0; 2]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i16(&buf)) } @@ -162,7 +164,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u24(&buf)) } @@ -188,7 +190,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i24(&mut self) -> Result { let mut buf = [0; 3]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i24(&buf)) } @@ -214,7 +216,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u32(&buf)) } @@ -240,7 +242,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i32(&buf)) } @@ -266,7 +268,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u48(&buf)) } @@ -292,7 +294,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i48(&mut self) -> Result { let mut buf = [0; 6]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i48(&buf)) } @@ -318,7 +320,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_u64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u64(&buf)) } @@ -344,7 +346,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_i64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i64(&buf)) } @@ -370,11 +372,10 @@ pub trait ReadBytesExt: io::Read { /// ]); /// assert_eq!(16947640962301618749969007319746179, rdr.read_u128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_u128(&buf)) } @@ -397,11 +398,10 @@ pub trait ReadBytesExt: io::Read { /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); /// assert_eq!(i128::min_value(), rdr.read_i128::().unwrap()); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(&mut self) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_i128(&buf)) } @@ -426,7 +426,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_uint(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint(&buf[..nbytes], nbytes)) } @@ -451,25 +451,23 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_int(&mut self, nbytes: usize) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int(&buf[..nbytes], nbytes)) } /// Reads an unsigned n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_uint128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_uint128(&buf[..nbytes], nbytes)) } /// Reads a signed n-bytes integer from the underlying reader. - #[cfg(byteorder_i128)] #[inline] fn read_int128(&mut self, nbytes: usize) -> Result { let mut buf = [0; 16]; - try!(self.read_exact(&mut buf[..nbytes])); + self.read_exact(&mut buf[..nbytes])?; Ok(T::read_int128(&buf[..nbytes], nbytes)) } @@ -500,7 +498,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f32(&mut self) -> Result { let mut buf = [0; 4]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f32(&buf)) } @@ -531,7 +529,7 @@ pub trait ReadBytesExt: io::Read { #[inline] fn read_f64(&mut self) -> Result { let mut buf = [0; 8]; - try!(self.read_exact(&mut buf)); + self.read_exact(&mut buf)?; Ok(T::read_f64(&buf)) } @@ -564,7 +562,7 @@ pub trait ReadBytesExt: io::Read { fn read_u16_into(&mut self, dst: &mut [u16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u16(dst); Ok(()) @@ -599,7 +597,7 @@ pub trait ReadBytesExt: io::Read { fn read_u32_into(&mut self, dst: &mut [u32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u32(dst); Ok(()) @@ -637,7 +635,7 @@ pub trait ReadBytesExt: io::Read { fn read_u64_into(&mut self, dst: &mut [u64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u64(dst); Ok(()) @@ -671,7 +669,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_u128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_u128_into( &mut self, @@ -679,7 +676,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_u128(dst); Ok(()) @@ -750,7 +747,7 @@ pub trait ReadBytesExt: io::Read { fn read_i16_into(&mut self, dst: &mut [i16]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i16(dst); Ok(()) @@ -785,7 +782,7 @@ pub trait ReadBytesExt: io::Read { fn read_i32_into(&mut self, dst: &mut [i32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i32(dst); Ok(()) @@ -823,7 +820,7 @@ pub trait ReadBytesExt: io::Read { fn read_i64_into(&mut self, dst: &mut [i64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i64(dst); Ok(()) @@ -857,7 +854,6 @@ pub trait ReadBytesExt: io::Read { /// rdr.read_i128_into::(&mut dst).unwrap(); /// assert_eq!([517, 768], dst); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into( &mut self, @@ -865,7 +861,7 @@ pub trait ReadBytesExt: io::Read { ) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_i128(dst); Ok(()) @@ -903,13 +899,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f32_into( - &mut self, - dst: &mut [f32], - ) -> Result<()> { + fn read_f32_into(&mut self, dst: &mut [f32]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f32(dst); Ok(()) @@ -951,7 +944,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f32::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked( &mut self, dst: &mut [f32], @@ -991,13 +984,10 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - fn read_f64_into( - &mut self, - dst: &mut [f64], - ) -> Result<()> { + fn read_f64_into(&mut self, dst: &mut [f64]) -> Result<()> { { let buf = unsafe { slice_to_u8_mut(dst) }; - try!(self.read_exact(buf)); + self.read_exact(buf)?; } T::from_slice_f64(dst); Ok(()) @@ -1045,7 +1035,7 @@ pub trait ReadBytesExt: io::Read { /// assert_eq!([f64::consts::PI, 1.0], dst); /// ``` #[inline] - #[deprecated(since="1.2.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.2.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked( &mut self, dst: &mut [f64], @@ -1408,7 +1398,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes an unsigned 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_u128(&mut self, n: u128) -> Result<()> { let mut buf = [0; 16]; @@ -1417,7 +1406,6 @@ pub trait WriteBytesExt: io::Write { } /// Writes a signed 128 bit integer to the underlying writer. - #[cfg(byteorder_i128)] #[inline] fn write_i128(&mut self, n: i128) -> Result<()> { let mut buf = [0; 16]; @@ -1501,7 +1489,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_uint128( &mut self, @@ -1517,7 +1504,6 @@ pub trait WriteBytesExt: io::Write { /// /// If the given integer is not representable in the given number of bytes, /// this method panics. If `nbytes > 16`, this method panics. - #[cfg(byteorder_i128)] #[inline] fn write_int128( &mut self, diff --git a/src/lib.rs b/src/lib.rs index db4d24d..d56574d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,23 +70,12 @@ cases. #![deny(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "std")] -extern crate core; - -#[cfg(test)] -#[macro_use] -extern crate doc_comment; - -#[cfg(test)] -doctest!("../README.md"); - -use core::fmt::Debug; -use core::hash::Hash; -use core::ptr::copy_nonoverlapping; -use core::slice; +use core::{ + convert::TryInto, fmt::Debug, hash::Hash, ptr::copy_nonoverlapping, slice, +}; #[cfg(feature = "std")] -pub use io::{ReadBytesExt, WriteBytesExt}; +pub use crate::io::{ReadBytesExt, WriteBytesExt}; #[cfg(feature = "std")] mod io; @@ -97,7 +86,6 @@ fn extend_sign(val: u64, nbytes: usize) -> i64 { (val << shift) as i64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn extend_sign128(val: u128, nbytes: usize) -> i128 { let shift = (16 - nbytes) * 8; @@ -110,7 +98,6 @@ fn unextend_sign(val: i64, nbytes: usize) -> u64 { (val << shift) as u64 >> shift } -#[cfg(byteorder_i128)] #[inline] fn unextend_sign128(val: i128, nbytes: usize) -> u128 { let shift = (16 - nbytes) * 8; @@ -138,7 +125,6 @@ fn pack_size(n: u64) -> usize { } } -#[cfg(byteorder_i128)] #[inline] fn pack_size128(n: u128) -> usize { if n < 1 << 8 { @@ -179,7 +165,7 @@ fn pack_size128(n: u128) -> usize { mod private { /// Sealed stops crates other than byteorder from implementing any traits /// that use it. - pub trait Sealed{} + pub trait Sealed {} impl Sealed for super::LittleEndian {} impl Sealed for super::BigEndian {} } @@ -219,8 +205,16 @@ mod private { /// /// [`BigEndian`]: enum.BigEndian.html /// [`LittleEndian`]: enum.LittleEndian.html -pub trait ByteOrder - : Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd +pub trait ByteOrder: + Clone + + Copy + + Debug + + Default + + Eq + + Hash + + Ord + + PartialEq + + PartialOrd + private::Sealed { /// Reads an unsigned 16 bit integer from `buf`. @@ -327,7 +321,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn read_u128(buf: &[u8]) -> u128; /// Reads an unsigned n-bytes integer from `buf`. @@ -368,7 +361,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn read_uint128(buf: &[u8], nbytes: usize) -> u128; /// Writes an unsigned 16 bit integer `n` to `buf`. @@ -487,7 +479,6 @@ pub trait ByteOrder /// LittleEndian::write_u128(&mut buf, 1_000_000); /// assert_eq!(1_000_000, LittleEndian::read_u128(&buf)); /// ``` - #[cfg(byteorder_i128)] fn write_u128(buf: &mut [u8], n: u128); /// Writes an unsigned integer `n` to `buf` using only `nbytes`. @@ -528,7 +519,6 @@ pub trait ByteOrder /// LittleEndian::write_uint128(&mut buf, 1_000_000, 3); /// assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize); /// Reads a signed 16 bit integer from `buf`. @@ -658,7 +648,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128(buf: &[u8]) -> i128 { Self::read_u128(buf) as i128 @@ -705,7 +694,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_int128(buf: &[u8], nbytes: usize) -> i128 { extend_sign128(Self::read_uint128(buf, nbytes), nbytes) @@ -731,7 +719,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f32(buf: &[u8]) -> f32 { - unsafe { *(&Self::read_u32(buf) as *const u32 as *const f32) } + f32::from_bits(Self::read_u32(buf)) } /// Reads a IEEE754 double-precision (8 bytes) floating point number. @@ -754,7 +742,7 @@ pub trait ByteOrder /// ``` #[inline] fn read_f64(buf: &[u8]) -> f64 { - unsafe { *(&Self::read_u64(buf) as *const u64 as *const f64) } + f64::from_bits(Self::read_u64(buf)) } /// Writes a signed 16 bit integer `n` to `buf`. @@ -884,7 +872,6 @@ pub trait ByteOrder /// LittleEndian::write_i128(&mut buf, -1_000_000_000); /// assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_i128(buf: &mut [u8], n: i128) { Self::write_u128(buf, n as u128) @@ -931,7 +918,6 @@ pub trait ByteOrder /// LittleEndian::write_int128(&mut buf, -1_000, 3); /// assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3)); /// ``` - #[cfg(byteorder_i128)] #[inline] fn write_int128(buf: &mut [u8], n: i128, nbytes: usize) { Self::write_uint128(buf, unextend_sign128(n, nbytes), nbytes) @@ -957,8 +943,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f32(buf: &mut [u8], n: f32) { - let n = unsafe { *(&n as *const f32 as *const u32) }; - Self::write_u32(buf, n) + Self::write_u32(buf, n.to_bits()) } /// Writes a IEEE754 double-precision (8 bytes) floating point number. @@ -981,8 +966,7 @@ pub trait ByteOrder /// ``` #[inline] fn write_f64(buf: &mut [u8], n: f64) { - let n = unsafe { *(&n as *const f64 as *const u64) }; - Self::write_u64(buf, n) + Self::write_u64(buf, n.to_bits()) } /// Reads unsigned 16 bit integers from `src` into `dst`. @@ -1075,7 +1059,6 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn read_u128_into(src: &[u8], dst: &mut [u128]); /// Reads signed 16 bit integers from `src` to `dst`. @@ -1186,7 +1169,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] #[inline] fn read_i128_into(src: &[u8], dst: &mut [i128]) { let dst = unsafe { @@ -1251,7 +1233,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f32_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f32_into` instead")] fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) { Self::read_f32_into(src, dst); } @@ -1313,7 +1295,7 @@ pub trait ByteOrder /// assert_eq!(numbers_given, numbers_got); /// ``` #[inline] - #[deprecated(since="1.3.0", note="please use `read_f64_into` instead")] + #[deprecated(since = "1.3.0", note = "please use `read_f64_into` instead")] fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) { Self::read_f64_into(src, dst); } @@ -1408,9 +1390,42 @@ pub trait ByteOrder /// LittleEndian::read_u128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_u128_into(src: &[u128], dst: &mut [u8]); + /// Writes signed 8 bit integers from `src` into `dst`. + /// + /// Note that since each `i8` is a single byte, no byte order conversions + /// are used. This method is included because it provides a safe, simple + /// way for the caller to write from a `&[i8]` buffer. (Without this + /// method, the caller would have to either use `unsafe` code or convert + /// each byte to `u8` individually.) + /// + /// # Panics + /// + /// Panics when `buf.len() != src.len()`. + /// + /// # Examples + /// + /// Write and read `i8` numbers in little endian order: + /// + /// ```rust + /// use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; + /// + /// let mut bytes = [0; 4]; + /// let numbers_given = [1, 2, 0xf, 0xe]; + /// LittleEndian::write_i8_into(&numbers_given, &mut bytes); + /// + /// let mut numbers_got = [0; 4]; + /// bytes.as_ref().read_i8_into(&mut numbers_got); + /// assert_eq!(numbers_given, numbers_got); + /// ``` + fn write_i8_into(src: &[i8], dst: &mut [u8]) { + let src = unsafe { + slice::from_raw_parts(src.as_ptr() as *const u8, src.len()) + }; + dst.copy_from_slice(src); + } + /// Writes signed 16 bit integers from `src` into `dst`. /// /// # Panics @@ -1516,7 +1531,6 @@ pub trait ByteOrder /// LittleEndian::read_i128_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` - #[cfg(byteorder_i128)] fn write_i128_into(src: &[i128], dst: &mut [u8]) { let src = unsafe { slice::from_raw_parts(src.as_ptr() as *const u128, src.len()) @@ -1660,7 +1674,6 @@ pub trait ByteOrder /// BigEndian::from_slice_u128(&mut numbers); /// assert_eq!(numbers, [5u128.to_be(), 65000u128.to_be()]); /// ``` - #[cfg(byteorder_i128)] fn from_slice_u128(numbers: &mut [u128]); /// Converts the given slice of signed 16 bit integers to a particular @@ -1755,7 +1768,6 @@ pub trait ByteOrder /// BigEndian::from_slice_i128(&mut numbers); /// assert_eq!(numbers, [5i128.to_be(), 65000i128.to_be()]); /// ``` - #[cfg(byteorder_i128)] #[inline] fn from_slice_i128(src: &mut [i128]) { let src = unsafe { @@ -1887,30 +1899,15 @@ pub type NativeEndian = LittleEndian; #[cfg(target_endian = "big")] pub type NativeEndian = BigEndian; -macro_rules! read_num_bytes { - ($ty:ty, $size:expr, $src:expr, $which:ident) => ({ - assert!($size == ::core::mem::size_of::<$ty>()); - assert!($size <= $src.len()); - let mut data: $ty = 0; - unsafe { - copy_nonoverlapping( - $src.as_ptr(), - &mut data as *mut $ty as *mut u8, - $size); - } - data.$which() - }); -} - macro_rules! write_num_bytes { - ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => ({ + ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => {{ assert!($size <= $dst.len()); unsafe { // N.B. https://github.com/rust-lang/rust/issues/22776 let bytes = *(&$n.$which() as *const _ as *const [u8; $size]); copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size); } - }); + }}; } macro_rules! read_slice { @@ -1921,7 +1918,8 @@ macro_rules! read_slice { copy_nonoverlapping( $src.as_ptr(), $dst.as_mut_ptr() as *mut u8, - $src.len()); + $src.len(), + ); } for v in $dst.iter_mut() { *v = v.$which(); @@ -1938,67 +1936,72 @@ macro_rules! write_slice_native { copy_nonoverlapping( $src.as_ptr() as *const u8, $dst.as_mut_ptr(), - $dst.len()); + $dst.len(), + ); } }}; } macro_rules! write_slice { - ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => ({ + ($src:expr, $dst:expr, $ty:ty, $size:expr, $write:expr) => {{ assert!($size == ::core::mem::size_of::<$ty>()); assert_eq!($size * $src.len(), $dst.len()); for (&n, chunk) in $src.iter().zip($dst.chunks_mut($size)) { $write(chunk, n); } - }); + }}; } impl ByteOrder for BigEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_be) + u16::from_be_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_be) + u32::from_be_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_be) + u64::from_be_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_be) + u128::from_be_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((8 - nbytes) as isize), nbytes); - (*(ptr_out as *const u64)).to_be() + buf.as_ptr(), + ptr_out.offset((8 - nbytes) as isize), + nbytes, + ); } + out.to_be() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping( - buf.as_ptr(), ptr_out.offset((16 - nbytes) as isize), nbytes); - (*(ptr_out as *const u128)).to_be() + buf.as_ptr(), + ptr_out.offset((16 - nbytes) as isize), + nbytes, + ); } + out.to_be() } #[inline] @@ -2016,7 +2019,6 @@ impl ByteOrder for BigEndian { write_num_bytes!(u64, 8, n, buf, to_be); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_be); @@ -2031,11 +2033,11 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((8 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n) <= nbytes && nbytes <= 16); @@ -2045,7 +2047,8 @@ impl ByteOrder for BigEndian { copy_nonoverlapping( bytes.as_ptr().offset((16 - nbytes) as isize), buf.as_mut_ptr(), - nbytes); + nbytes, + ); } } @@ -2064,7 +2067,6 @@ impl ByteOrder for BigEndian { read_slice!(src, dst, 8, to_be); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_be); @@ -2097,7 +2099,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "big") { @@ -2134,7 +2135,6 @@ impl ByteOrder for BigEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "little") { @@ -2172,46 +2172,44 @@ impl ByteOrder for BigEndian { impl ByteOrder for LittleEndian { #[inline] fn read_u16(buf: &[u8]) -> u16 { - read_num_bytes!(u16, 2, buf, to_le) + u16::from_le_bytes(buf[..2].try_into().unwrap()) } #[inline] fn read_u32(buf: &[u8]) -> u32 { - read_num_bytes!(u32, 4, buf, to_le) + u32::from_le_bytes(buf[..4].try_into().unwrap()) } #[inline] fn read_u64(buf: &[u8]) -> u64 { - read_num_bytes!(u64, 8, buf, to_le) + u64::from_le_bytes(buf[..8].try_into().unwrap()) } - #[cfg(byteorder_i128)] #[inline] fn read_u128(buf: &[u8]) -> u128 { - read_num_bytes!(u128, 16, buf, to_le) + u128::from_le_bytes(buf[..16].try_into().unwrap()) } #[inline] fn read_uint(buf: &[u8], nbytes: usize) -> u64 { assert!(1 <= nbytes && nbytes <= 8 && nbytes <= buf.len()); - let mut out = [0u8; 8]; - let ptr_out = out.as_mut_ptr(); + let mut out = 0u64; + let ptr_out = &mut out as *mut u64 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u64)).to_le() } + out.to_le() } - #[cfg(byteorder_i128)] #[inline] fn read_uint128(buf: &[u8], nbytes: usize) -> u128 { assert!(1 <= nbytes && nbytes <= 16 && nbytes <= buf.len()); - let mut out = [0u8; 16]; - let ptr_out = out.as_mut_ptr(); + let mut out: u128 = 0; + let ptr_out = &mut out as *mut u128 as *mut u8; unsafe { copy_nonoverlapping(buf.as_ptr(), ptr_out, nbytes); - (*(ptr_out as *const u128)).to_le() } + out.to_le() } #[inline] @@ -2229,7 +2227,6 @@ impl ByteOrder for LittleEndian { write_num_bytes!(u64, 8, n, buf, to_le); } - #[cfg(byteorder_i128)] #[inline] fn write_u128(buf: &mut [u8], n: u128) { write_num_bytes!(u128, 16, n, buf, to_le); @@ -2245,7 +2242,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize) { assert!(pack_size128(n as u128) <= nbytes && nbytes <= 16); @@ -2271,7 +2267,6 @@ impl ByteOrder for LittleEndian { read_slice!(src, dst, 8, to_le); } - #[cfg(byteorder_i128)] #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { read_slice!(src, dst, 16, to_le); @@ -2304,7 +2299,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "little") { @@ -2341,7 +2335,6 @@ impl ByteOrder for LittleEndian { } } - #[cfg(byteorder_i128)] #[inline] fn from_slice_u128(numbers: &mut [u128]) { if cfg!(target_endian = "big") { @@ -2378,15 +2371,8 @@ impl ByteOrder for LittleEndian { #[cfg(test)] mod test { - extern crate quickcheck; - extern crate rand; - - use self::quickcheck::{QuickCheck, StdGen, Testable}; - use self::rand::thread_rng; - #[cfg(byteorder_i128)] - use self::rand::Rng; - #[cfg(byteorder_i128)] - use self::quickcheck::{Arbitrary, Gen}; + use quickcheck::{Arbitrary, Gen, QuickCheck, StdGen, Testable}; + use rand::{thread_rng, Rng}; pub const U24_MAX: u32 = 16_777_215; pub const I24_MAX: i32 = 8_388_607; @@ -2397,7 +2383,9 @@ mod test { pub const I64_MAX: u64 = ::core::i64::MAX as u64; macro_rules! calc_max { - ($max:expr, $bytes:expr) => { calc_max!($max, $bytes, 8) }; + ($max:expr, $bytes:expr) => { + calc_max!($max, $bytes, 8) + }; ($max:expr, $bytes:expr, $maxbytes:expr) => { ($max - 1) >> (8 * ($maxbytes - $bytes)) }; @@ -2406,7 +2394,6 @@ mod test { #[derive(Clone, Debug)] pub struct Wi128(pub T); - #[cfg(byteorder_i128)] impl Wi128 { pub fn clone(&self) -> T { self.0.clone() @@ -2419,24 +2406,20 @@ mod test { } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::u128::MAX, gen.size(), 16); - let output = - (gen.gen::() as u128) | - ((gen.gen::() as u128) << 64); + let output = (gen.gen::() as u128) + | ((gen.gen::() as u128) << 64); Wi128(output & (max - 1)) } } - #[cfg(byteorder_i128)] impl Arbitrary for Wi128 { fn arbitrary(gen: &mut G) -> Wi128 { let max = calc_max!(::core::i128::MAX, gen.size(), 16); - let output = - (gen.gen::() as i128) | - ((gen.gen::() as i128) << 64); + let output = (gen.gen::() as i128) + | ((gen.gen::() as i128) << 64); Wi128(output & (max - 1)) } } @@ -2451,17 +2434,20 @@ mod test { macro_rules! qc_byte_order { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] fn big_endian() { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; BigEndian::$write(&mut buf, n.clone(), $bytes); - n == BigEndian::$read(&mut buf[..$bytes], $bytes) + n == BigEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2471,7 +2457,7 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; LittleEndian::$write(&mut buf, n.clone(), $bytes); - n == LittleEndian::$read(&mut buf[..$bytes], $bytes) + n == LittleEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } @@ -2481,18 +2467,21 @@ mod test { fn prop(n: $ty_int) -> bool { let mut buf = [0; 16]; NativeEndian::$write(&mut buf, n.clone(), $bytes); - n == NativeEndian::$read(&mut buf[..$bytes], $bytes) + n == NativeEndian::$read(&buf[..$bytes], $bytes) } qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); + }; ($name:ident, $ty_int:ty, $max:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { + #[allow(unused_imports)] + use super::{qc_sized, Wi128}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; use core::mem::size_of; - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; - #[allow(unused_imports)] use super::{ qc_sized, Wi128 }; #[test] fn big_endian() { @@ -2500,7 +2489,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; BigEndian::$write(&mut buf[16 - bytes..], n.clone()); - n == BigEndian::$read(&mut buf[16 - bytes..]) + n == BigEndian::$read(&buf[16 - bytes..]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2511,7 +2500,7 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; LittleEndian::$write(&mut buf[..bytes], n.clone()); - n == LittleEndian::$read(&mut buf[..bytes]) + n == LittleEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } @@ -2522,164 +2511,489 @@ mod test { let bytes = size_of::<$ty_int>(); let mut buf = [0; 16]; NativeEndian::$write(&mut buf[..bytes], n.clone()); - n == NativeEndian::$read(&mut buf[..bytes]) + n == NativeEndian::$read(&buf[..bytes]) } qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_byte_order!(prop_u16, u16, ::core::u16::MAX as u64, read_u16, write_u16); - qc_byte_order!(prop_i16, i16, ::core::i16::MAX as u64, read_i16, write_i16); - qc_byte_order!(prop_u24, u32, ::test::U24_MAX as u64, read_u24, write_u24); - qc_byte_order!(prop_i24, i32, ::test::I24_MAX as u64, read_i24, write_i24); - qc_byte_order!(prop_u32, u32, ::core::u32::MAX as u64, read_u32, write_u32); - qc_byte_order!(prop_i32, i32, ::core::i32::MAX as u64, read_i32, write_i32); - qc_byte_order!(prop_u48, u64, ::test::U48_MAX as u64, read_u48, write_u48); - qc_byte_order!(prop_i48, i64, ::test::I48_MAX as u64, read_i48, write_i48); - qc_byte_order!(prop_u64, u64, ::core::u64::MAX as u64, read_u64, write_u64); - qc_byte_order!(prop_i64, i64, ::core::i64::MAX as u64, read_i64, write_i64); - qc_byte_order!(prop_f32, f32, ::core::u64::MAX as u64, read_f32, write_f32); - qc_byte_order!(prop_f64, f64, ::core::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_byte_order!( + prop_u16, + u16, + ::core::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_byte_order!( + prop_i16, + i16, + ::core::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_byte_order!( + prop_u24, + u32, + crate::test::U24_MAX as u64, + read_u24, + write_u24 + ); + qc_byte_order!( + prop_i24, + i32, + crate::test::I24_MAX as u64, + read_i24, + write_i24 + ); + qc_byte_order!( + prop_u32, + u32, + ::core::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_byte_order!( + prop_i32, + i32, + ::core::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_byte_order!( + prop_u48, + u64, + crate::test::U48_MAX as u64, + read_u48, + write_u48 + ); + qc_byte_order!( + prop_i48, + i64, + crate::test::I48_MAX as u64, + read_i48, + write_i48 + ); + qc_byte_order!( + prop_u64, + u64, + ::core::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_byte_order!( + prop_i64, + i64, + ::core::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_byte_order!( + prop_f32, + f32, + ::core::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_byte_order!( + prop_f64, + f64, + ::core::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_byte_order!(prop_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_byte_order!(prop_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_byte_order!(prop_uint_1, - u64, calc_max!(super::U64_MAX, 1), 1, read_uint, write_uint); - qc_byte_order!(prop_uint_2, - u64, calc_max!(super::U64_MAX, 2), 2, read_uint, write_uint); - qc_byte_order!(prop_uint_3, - u64, calc_max!(super::U64_MAX, 3), 3, read_uint, write_uint); - qc_byte_order!(prop_uint_4, - u64, calc_max!(super::U64_MAX, 4), 4, read_uint, write_uint); - qc_byte_order!(prop_uint_5, - u64, calc_max!(super::U64_MAX, 5), 5, read_uint, write_uint); - qc_byte_order!(prop_uint_6, - u64, calc_max!(super::U64_MAX, 6), 6, read_uint, write_uint); - qc_byte_order!(prop_uint_7, - u64, calc_max!(super::U64_MAX, 7), 7, read_uint, write_uint); - qc_byte_order!(prop_uint_8, - u64, calc_max!(super::U64_MAX, 8), 8, read_uint, write_uint); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_1, - Wi128, 1, 1, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_2, - Wi128, 2, 2, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_3, - Wi128, 3, 3, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_4, - Wi128, 4, 4, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_5, - Wi128, 5, 5, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_6, - Wi128, 6, 6, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_7, - Wi128, 7, 7, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_8, - Wi128, 8, 8, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_9, - Wi128, 9, 9, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_10, - Wi128, 10, 10, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_11, - Wi128, 11, 11, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_12, - Wi128, 12, 12, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_13, - Wi128, 13, 13, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_14, - Wi128, 14, 14, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_15, - Wi128, 15, 15, read_uint128, write_uint128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_uint128_16, - Wi128, 16, 16, read_uint128, write_uint128); - - qc_byte_order!(prop_int_1, - i64, calc_max!(super::I64_MAX, 1), 1, read_int, write_int); - qc_byte_order!(prop_int_2, - i64, calc_max!(super::I64_MAX, 2), 2, read_int, write_int); - qc_byte_order!(prop_int_3, - i64, calc_max!(super::I64_MAX, 3), 3, read_int, write_int); - qc_byte_order!(prop_int_4, - i64, calc_max!(super::I64_MAX, 4), 4, read_int, write_int); - qc_byte_order!(prop_int_5, - i64, calc_max!(super::I64_MAX, 5), 5, read_int, write_int); - qc_byte_order!(prop_int_6, - i64, calc_max!(super::I64_MAX, 6), 6, read_int, write_int); - qc_byte_order!(prop_int_7, - i64, calc_max!(super::I64_MAX, 7), 7, read_int, write_int); - qc_byte_order!(prop_int_8, - i64, calc_max!(super::I64_MAX, 8), 8, read_int, write_int); - - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_1, - Wi128, 1, 1, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_2, - Wi128, 2, 2, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_3, - Wi128, 3, 3, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_4, - Wi128, 4, 4, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_5, - Wi128, 5, 5, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_6, - Wi128, 6, 6, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_7, - Wi128, 7, 7, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_8, - Wi128, 8, 8, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_9, - Wi128, 9, 9, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_10, - Wi128, 10, 10, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_11, - Wi128, 11, 11, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_12, - Wi128, 12, 12, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_13, - Wi128, 13, 13, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_14, - Wi128, 14, 14, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_15, - Wi128, 15, 15, read_int128, write_int128); - #[cfg(byteorder_i128)] - qc_byte_order!(prop_int128_16, - Wi128, 16, 16, read_int128, write_int128); - + qc_byte_order!( + prop_uint_1, + u64, + calc_max!(super::U64_MAX, 1), + 1, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_2, + u64, + calc_max!(super::U64_MAX, 2), + 2, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_3, + u64, + calc_max!(super::U64_MAX, 3), + 3, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_4, + u64, + calc_max!(super::U64_MAX, 4), + 4, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_5, + u64, + calc_max!(super::U64_MAX, 5), + 5, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_6, + u64, + calc_max!(super::U64_MAX, 6), + 6, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_7, + u64, + calc_max!(super::U64_MAX, 7), + 7, + read_uint, + write_uint + ); + qc_byte_order!( + prop_uint_8, + u64, + calc_max!(super::U64_MAX, 8), + 8, + read_uint, + write_uint + ); + + qc_byte_order!( + prop_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_uint128 + ); + qc_byte_order!( + prop_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_uint128 + ); + + qc_byte_order!( + prop_int_1, + i64, + calc_max!(super::I64_MAX, 1), + 1, + read_int, + write_int + ); + qc_byte_order!( + prop_int_2, + i64, + calc_max!(super::I64_MAX, 2), + 2, + read_int, + write_int + ); + qc_byte_order!( + prop_int_3, + i64, + calc_max!(super::I64_MAX, 3), + 3, + read_int, + write_int + ); + qc_byte_order!( + prop_int_4, + i64, + calc_max!(super::I64_MAX, 4), + 4, + read_int, + write_int + ); + qc_byte_order!( + prop_int_5, + i64, + calc_max!(super::I64_MAX, 5), + 5, + read_int, + write_int + ); + qc_byte_order!( + prop_int_6, + i64, + calc_max!(super::I64_MAX, 6), + 6, + read_int, + write_int + ); + qc_byte_order!( + prop_int_7, + i64, + calc_max!(super::I64_MAX, 7), + 7, + read_int, + write_int + ); + qc_byte_order!( + prop_int_8, + i64, + calc_max!(super::I64_MAX, 8), + 8, + read_int, + write_int + ); + + qc_byte_order!( + prop_int128_1, + Wi128, + 1, + 1, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_2, + Wi128, + 2, + 2, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_3, + Wi128, + 3, + 3, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_4, + Wi128, + 4, + 4, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_5, + Wi128, + 5, + 5, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_6, + Wi128, + 6, + 6, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_7, + Wi128, + 7, + 7, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_8, + Wi128, + 8, + 8, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_9, + Wi128, + 9, + 9, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_10, + Wi128, + 10, + 10, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_11, + Wi128, + 11, + 11, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_12, + Wi128, + 12, + 12, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_13, + Wi128, + 13, + 13, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_14, + Wi128, + 14, + 14, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_15, + Wi128, + 15, + 15, + read_int128, + write_int128 + ); + qc_byte_order!( + prop_int128_16, + Wi128, + 16, + 16, + read_int128, + write_int128 + ); // Test that all of the byte conversion functions panic when given a // buffer that is too small. @@ -2688,9 +3002,11 @@ mod test { // with a buffer overflow. macro_rules! too_small { ($name:ident, $maximally_small:expr, $zero:expr, - $read:ident, $write:ident) => ( + $read:ident, $write:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2734,10 +3050,12 @@ mod test { NativeEndian::$write(&mut buf, $zero); } } - ); - ($name:ident, $maximally_small:expr, $read:ident) => ( + }; + ($name:ident, $maximally_small:expr, $read:ident) => { mod $name { - use {BigEndian, ByteOrder, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2760,7 +3078,7 @@ mod test { NativeEndian::$read(&buf, $maximally_small + 1); } } - ); + }; } too_small!(small_u16, 1, 0, read_u16, write_u16); @@ -2771,9 +3089,7 @@ mod test { too_small!(small_i64, 7, 0, read_i64, write_i64); too_small!(small_f32, 3, 0.0, read_f32, write_f32); too_small!(small_f64, 7, 0.0, read_f64, write_f64); - #[cfg(byteorder_i128)] too_small!(small_u128, 15, 0, read_u128, write_u128); - #[cfg(byteorder_i128)] too_small!(small_i128, 15, 0, read_i128, write_i128); too_small!(small_uint_1, 1, read_uint); @@ -2784,35 +3100,20 @@ mod test { too_small!(small_uint_6, 6, read_uint); too_small!(small_uint_7, 7, read_uint); - #[cfg(byteorder_i128)] too_small!(small_uint128_1, 1, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_2, 2, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_3, 3, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_4, 4, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_5, 5, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_6, 6, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_7, 7, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_8, 8, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_9, 9, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_10, 10, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_11, 11, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_12, 12, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_13, 13, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_14, 14, read_uint128); - #[cfg(byteorder_i128)] too_small!(small_uint128_15, 15, read_uint128); too_small!(small_int_1, 1, read_int); @@ -2823,35 +3124,20 @@ mod test { too_small!(small_int_6, 6, read_int); too_small!(small_int_7, 7, read_int); - #[cfg(byteorder_i128)] too_small!(small_int128_1, 1, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_2, 2, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_3, 3, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_4, 4, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_5, 5, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_6, 6, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_7, 7, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_8, 8, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_9, 9, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_10, 10, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_11, 11, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_12, 12, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_13, 13, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_14, 14, read_int128); - #[cfg(byteorder_i128)] too_small!(small_int128_15, 15, read_int128); // Test that reading/writing slices enforces the correct lengths. @@ -2859,7 +3145,9 @@ mod test { ($name:ident, $read:ident, $write:ident, $num_bytes:expr, $numbers:expr) => { mod $name { - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; #[test] #[should_panic] @@ -2909,54 +3197,171 @@ mod test { NativeEndian::$write(&numbers, &mut bytes); } } - } + }; } slice_lengths!( - slice_len_too_small_u16, read_u16_into, write_u16_into, 3, [0, 0]); + slice_len_too_small_u16, + read_u16_into, + write_u16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u16, read_u16_into, write_u16_into, 5, [0, 0]); + slice_len_too_big_u16, + read_u16_into, + write_u16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i16, read_i16_into, write_i16_into, 3, [0, 0]); + slice_len_too_small_i16, + read_i16_into, + write_i16_into, + 3, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i16, read_i16_into, write_i16_into, 5, [0, 0]); + slice_len_too_big_i16, + read_i16_into, + write_i16_into, + 5, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u32, read_u32_into, write_u32_into, 7, [0, 0]); + slice_len_too_small_u32, + read_u32_into, + write_u32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u32, read_u32_into, write_u32_into, 9, [0, 0]); + slice_len_too_big_u32, + read_u32_into, + write_u32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i32, read_i32_into, write_i32_into, 7, [0, 0]); + slice_len_too_small_i32, + read_i32_into, + write_i32_into, + 7, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i32, read_i32_into, write_i32_into, 9, [0, 0]); + slice_len_too_big_i32, + read_i32_into, + write_i32_into, + 9, + [0, 0] + ); slice_lengths!( - slice_len_too_small_u64, read_u64_into, write_u64_into, 15, [0, 0]); + slice_len_too_small_u64, + read_u64_into, + write_u64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u64, read_u64_into, write_u64_into, 17, [0, 0]); + slice_len_too_big_u64, + read_u64_into, + write_u64_into, + 17, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i64, read_i64_into, write_i64_into, 15, [0, 0]); + slice_len_too_small_i64, + read_i64_into, + write_i64_into, + 15, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i64, read_i64_into, write_i64_into, 17, [0, 0]); + slice_len_too_big_i64, + read_i64_into, + write_i64_into, + 17, + [0, 0] + ); - #[cfg(byteorder_i128)] slice_lengths!( - slice_len_too_small_u128, read_u128_into, write_u128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_u128, + read_u128_into, + write_u128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_u128, read_u128_into, write_u128_into, 33, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_big_u128, + read_u128_into, + write_u128_into, + 33, + [0, 0] + ); slice_lengths!( - slice_len_too_small_i128, read_i128_into, write_i128_into, 31, [0, 0]); - #[cfg(byteorder_i128)] + slice_len_too_small_i128, + read_i128_into, + write_i128_into, + 31, + [0, 0] + ); slice_lengths!( - slice_len_too_big_i128, read_i128_into, write_i128_into, 33, [0, 0]); + slice_len_too_big_i128, + read_i128_into, + write_i128_into, + 33, + [0, 0] + ); #[test] fn uint_bigger_buffer() { - use {ByteOrder, LittleEndian}; + use crate::{ByteOrder, LittleEndian}; let n = LittleEndian::read_uint(&[1, 2, 3, 4, 5, 6, 7, 8], 5); - assert_eq!(n, 0x0504030201); + assert_eq!(n, 0x05_0403_0201); + } + + #[test] + fn regression173_array_impl() { + use crate::{BigEndian, ByteOrder, LittleEndian}; + + let xs = [0; 100]; + + let x = BigEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = BigEndian::read_i128(&xs); + assert_eq!(x, 0); + + let x = LittleEndian::read_u16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_u128(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i16(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i32(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i64(&xs); + assert_eq!(x, 0); + let x = LittleEndian::read_i128(&xs); + assert_eq!(x, 0); } } @@ -2970,7 +3375,6 @@ mod stdtests { use self::rand::thread_rng; fn qc_unsized(f: A) { - QuickCheck::new() .gen(StdGen::new(thread_rng(), 16)) .tests(1_00) @@ -2979,19 +3383,22 @@ mod stdtests { } macro_rules! calc_max { - ($max:expr, $bytes:expr) => { ($max - 1) >> (8 * (8 - $bytes)) }; + ($max:expr, $bytes:expr) => { + ($max - 1) >> (8 * (8 - $bytes)) + }; } macro_rules! qc_bytes_ext { ($name:ident, $ty_int:ty, $max:expr, - $bytes:expr, $read:ident, $write:ident) => ( + $bytes:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3032,15 +3439,16 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max); } } - ); - ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => ( + }; + ($name:ident, $ty_int:ty, $max:expr, $read:ident, $write:ident) => { mod $name { - use std::io::Cursor; - use { - ReadBytesExt, WriteBytesExt, - BigEndian, NativeEndian, LittleEndian, + #[allow(unused_imports)] + use crate::test::{qc_sized, Wi128}; + use crate::{ + BigEndian, LittleEndian, NativeEndian, ReadBytesExt, + WriteBytesExt, }; - #[allow(unused_imports)] use test::{qc_sized, Wi128}; + use std::io::Cursor; #[test] fn big_endian() { @@ -3075,188 +3483,484 @@ mod stdtests { qc_sized(prop as fn($ty_int) -> bool, $max - 1); } } - ); - } - - qc_bytes_ext!(prop_ext_u16, - u16, ::std::u16::MAX as u64, read_u16, write_u16); - qc_bytes_ext!(prop_ext_i16, - i16, ::std::i16::MAX as u64, read_i16, write_i16); - qc_bytes_ext!(prop_ext_u32, - u32, ::std::u32::MAX as u64, read_u32, write_u32); - qc_bytes_ext!(prop_ext_i32, - i32, ::std::i32::MAX as u64, read_i32, write_i32); - qc_bytes_ext!(prop_ext_u64, - u64, ::std::u64::MAX as u64, read_u64, write_u64); - qc_bytes_ext!(prop_ext_i64, - i64, ::std::i64::MAX as u64, read_i64, write_i64); - qc_bytes_ext!(prop_ext_f32, - f32, ::std::u64::MAX as u64, read_f32, write_f32); - qc_bytes_ext!(prop_ext_f64, - f64, ::std::i64::MAX as u64, read_f64, write_f64); - - #[cfg(byteorder_i128)] + }; + } + + qc_bytes_ext!( + prop_ext_u16, + u16, + ::std::u16::MAX as u64, + read_u16, + write_u16 + ); + qc_bytes_ext!( + prop_ext_i16, + i16, + ::std::i16::MAX as u64, + read_i16, + write_i16 + ); + qc_bytes_ext!( + prop_ext_u32, + u32, + ::std::u32::MAX as u64, + read_u32, + write_u32 + ); + qc_bytes_ext!( + prop_ext_i32, + i32, + ::std::i32::MAX as u64, + read_i32, + write_i32 + ); + qc_bytes_ext!( + prop_ext_u64, + u64, + ::std::u64::MAX as u64, + read_u64, + write_u64 + ); + qc_bytes_ext!( + prop_ext_i64, + i64, + ::std::i64::MAX as u64, + read_i64, + write_i64 + ); + qc_bytes_ext!( + prop_ext_f32, + f32, + ::std::u64::MAX as u64, + read_f32, + write_f32 + ); + qc_bytes_ext!( + prop_ext_f64, + f64, + ::std::i64::MAX as u64, + read_f64, + write_f64 + ); + qc_bytes_ext!(prop_ext_u128, Wi128, 16 + 1, read_u128, write_u128); - #[cfg(byteorder_i128)] qc_bytes_ext!(prop_ext_i128, Wi128, 16 + 1, read_i128, write_i128); - qc_bytes_ext!(prop_ext_uint_1, - u64, calc_max!(::test::U64_MAX, 1), 1, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_2, - u64, calc_max!(::test::U64_MAX, 2), 2, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_3, - u64, calc_max!(::test::U64_MAX, 3), 3, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_4, - u64, calc_max!(::test::U64_MAX, 4), 4, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_5, - u64, calc_max!(::test::U64_MAX, 5), 5, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_6, - u64, calc_max!(::test::U64_MAX, 6), 6, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_7, - u64, calc_max!(::test::U64_MAX, 7), 7, read_uint, write_u64); - qc_bytes_ext!(prop_ext_uint_8, - u64, calc_max!(::test::U64_MAX, 8), 8, read_uint, write_u64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_1, - Wi128, 1, 1, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_2, - Wi128, 2, 2, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_3, - Wi128, 3, 3, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_4, - Wi128, 4, 4, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_5, - Wi128, 5, 5, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_6, - Wi128, 6, 6, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_7, - Wi128, 7, 7, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_8, - Wi128, 8, 8, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_9, - Wi128, 9, 9, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_10, - Wi128, 10, 10, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_11, - Wi128, 11, 11, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_12, - Wi128, 12, 12, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_13, - Wi128, 13, 13, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_14, - Wi128, 14, 14, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_15, - Wi128, 15, 15, read_uint128, write_u128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_uint128_16, - Wi128, 16, 16, read_uint128, write_u128); - - qc_bytes_ext!(prop_ext_int_1, - i64, calc_max!(::test::I64_MAX, 1), 1, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_2, - i64, calc_max!(::test::I64_MAX, 2), 2, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_3, - i64, calc_max!(::test::I64_MAX, 3), 3, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_4, - i64, calc_max!(::test::I64_MAX, 4), 4, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_5, - i64, calc_max!(::test::I64_MAX, 5), 5, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_6, - i64, calc_max!(::test::I64_MAX, 6), 6, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_7, - i64, calc_max!(::test::I64_MAX, 1), 7, read_int, write_i64); - qc_bytes_ext!(prop_ext_int_8, - i64, calc_max!(::test::I64_MAX, 8), 8, read_int, write_i64); - - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_1, - Wi128, 1, 1, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_2, - Wi128, 2, 2, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_3, - Wi128, 3, 3, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_4, - Wi128, 4, 4, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_5, - Wi128, 5, 5, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_6, - Wi128, 6, 6, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_7, - Wi128, 7, 7, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_8, - Wi128, 8, 8, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_9, - Wi128, 9, 9, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_10, - Wi128, 10, 10, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_11, - Wi128, 11, 11, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_12, - Wi128, 12, 12, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_13, - Wi128, 13, 13, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_14, - Wi128, 14, 14, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_15, - Wi128, 15, 15, read_int128, write_i128); - #[cfg(byteorder_i128)] - qc_bytes_ext!(prop_ext_int128_16, - Wi128, 16, 16, read_int128, write_i128); + qc_bytes_ext!( + prop_ext_uint_1, + u64, + calc_max!(crate::test::U64_MAX, 1), + 1, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_2, + u64, + calc_max!(crate::test::U64_MAX, 2), + 2, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_3, + u64, + calc_max!(crate::test::U64_MAX, 3), + 3, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_4, + u64, + calc_max!(crate::test::U64_MAX, 4), + 4, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_5, + u64, + calc_max!(crate::test::U64_MAX, 5), + 5, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_6, + u64, + calc_max!(crate::test::U64_MAX, 6), + 6, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_7, + u64, + calc_max!(crate::test::U64_MAX, 7), + 7, + read_uint, + write_u64 + ); + qc_bytes_ext!( + prop_ext_uint_8, + u64, + calc_max!(crate::test::U64_MAX, 8), + 8, + read_uint, + write_u64 + ); + + qc_bytes_ext!( + prop_ext_uint128_1, + Wi128, + 1, + 1, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_2, + Wi128, + 2, + 2, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_3, + Wi128, + 3, + 3, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_4, + Wi128, + 4, + 4, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_5, + Wi128, + 5, + 5, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_6, + Wi128, + 6, + 6, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_7, + Wi128, + 7, + 7, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_8, + Wi128, + 8, + 8, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_9, + Wi128, + 9, + 9, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_10, + Wi128, + 10, + 10, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_11, + Wi128, + 11, + 11, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_12, + Wi128, + 12, + 12, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_13, + Wi128, + 13, + 13, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_14, + Wi128, + 14, + 14, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_15, + Wi128, + 15, + 15, + read_uint128, + write_u128 + ); + qc_bytes_ext!( + prop_ext_uint128_16, + Wi128, + 16, + 16, + read_uint128, + write_u128 + ); + + qc_bytes_ext!( + prop_ext_int_1, + i64, + calc_max!(crate::test::I64_MAX, 1), + 1, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_2, + i64, + calc_max!(crate::test::I64_MAX, 2), + 2, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_3, + i64, + calc_max!(crate::test::I64_MAX, 3), + 3, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_4, + i64, + calc_max!(crate::test::I64_MAX, 4), + 4, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_5, + i64, + calc_max!(crate::test::I64_MAX, 5), + 5, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_6, + i64, + calc_max!(crate::test::I64_MAX, 6), + 6, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_7, + i64, + calc_max!(crate::test::I64_MAX, 1), + 7, + read_int, + write_i64 + ); + qc_bytes_ext!( + prop_ext_int_8, + i64, + calc_max!(crate::test::I64_MAX, 8), + 8, + read_int, + write_i64 + ); + + qc_bytes_ext!( + prop_ext_int128_1, + Wi128, + 1, + 1, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_2, + Wi128, + 2, + 2, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_3, + Wi128, + 3, + 3, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_4, + Wi128, + 4, + 4, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_5, + Wi128, + 5, + 5, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_6, + Wi128, + 6, + 6, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_7, + Wi128, + 7, + 7, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_8, + Wi128, + 8, + 8, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_9, + Wi128, + 9, + 9, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_10, + Wi128, + 10, + 10, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_11, + Wi128, + 11, + 11, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_12, + Wi128, + 12, + 12, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_13, + Wi128, + 13, + 13, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_14, + Wi128, + 14, + 14, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_15, + Wi128, + 15, + 15, + read_int128, + write_i128 + ); + qc_bytes_ext!( + prop_ext_int128_16, + Wi128, + 16, + 16, + read_int128, + write_i128 + ); // Test slice serialization/deserialization. macro_rules! qc_slice { ($name:ident, $ty_int:ty, $read:ident, $write:ident, $zero:expr) => { mod $name { - use core::mem::size_of; - use {ByteOrder, BigEndian, NativeEndian, LittleEndian}; use super::qc_unsized; #[allow(unused_imports)] - use test::Wi128; + use crate::test::Wi128; + use crate::{ + BigEndian, ByteOrder, LittleEndian, NativeEndian, + }; + use core::mem::size_of; #[test] fn big_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; BigEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { BigEndian::$read(&bytes, &mut got); } + unsafe { + BigEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3267,17 +3971,17 @@ mod stdtests { fn little_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; LittleEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { LittleEndian::$read(&bytes, &mut got); } + unsafe { + LittleEndian::$read(&bytes, &mut got); + } numbers == got } @@ -3288,24 +3992,24 @@ mod stdtests { fn native_endian() { #[allow(unused_unsafe)] fn prop(numbers: Vec<$ty_int>) -> bool { - let numbers: Vec<_> = numbers - .into_iter() - .map(|x| x.clone()) - .collect(); + let numbers: Vec<_> = + numbers.into_iter().map(|x| x.clone()).collect(); let num_bytes = size_of::<$ty_int>() * numbers.len(); let mut bytes = vec![0; num_bytes]; NativeEndian::$write(&numbers, &mut bytes); let mut got = vec![$zero; numbers.len()]; - unsafe { NativeEndian::$read(&bytes, &mut got); } + unsafe { + NativeEndian::$read(&bytes, &mut got); + } numbers == got } qc_unsized(prop as fn(_) -> bool); } } - } + }; } qc_slice!(prop_slice_u16, u16, read_u16_into, write_u16_into, 0); @@ -3314,15 +4018,21 @@ mod stdtests { qc_slice!(prop_slice_i32, i32, read_i32_into, write_i32_into, 0); qc_slice!(prop_slice_u64, u64, read_u64_into, write_u64_into, 0); qc_slice!(prop_slice_i64, i64, read_i64_into, write_i64_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_u128, Wi128, read_u128_into, write_u128_into, 0); - #[cfg(byteorder_i128)] - qc_slice!( - prop_slice_i128, Wi128, read_i128_into, write_i128_into, 0); - qc_slice!( - prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + prop_slice_u128, + Wi128, + read_u128_into, + write_u128_into, + 0 + ); qc_slice!( - prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); + prop_slice_i128, + Wi128, + read_i128_into, + write_i128_into, + 0 + ); + + qc_slice!(prop_slice_f32, f32, read_f32_into, write_f32_into, 0.0); + qc_slice!(prop_slice_f64, f64, read_f64_into, write_f64_into, 0.0); } -- cgit v1.2.3 From 5c6b61cbf6ceb9c01d1f040c63a3528d990d4d7b Mon Sep 17 00:00:00 2001 From: Bob Badour Date: Fri, 26 Feb 2021 02:50:47 -0800 Subject: [LSC] Add LOCAL_LICENSE_KINDS to external/rust/crates/byteorder Added SPDX-license-identifier-MIT SPDX-license-identifier-Unlicense to: Android.bp Bug: 68860345 Bug: 151177513 Bug: 151953481 Test: m all Exempt-From-Owner-Approval: janitorial work Change-Id: Ic813d475d40dc0307691c713f09b3244975c8211 --- Android.bp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Android.bp b/Android.bp index 8f42c1a..899071d 100644 --- a/Android.bp +++ b/Android.bp @@ -1,5 +1,42 @@ // This file is generated by cargo2android.py --run --device --dependencies. +package { + default_applicable_licenses: ["external_rust_crates_byteorder_license"], +} + +// Added automatically by a large-scale-change that took the approach of +// 'apply every license found to every target'. While this makes sure we respect +// every license restriction, it may not be entirely correct. +// +// e.g. GPL in an MIT project might only apply to the contrib/ directory. +// +// Please consider splitting the single license below into multiple licenses, +// taking care not to lose any license_kind information, and overriding the +// default license using the 'licenses: [...]' property on targets as needed. +// +// For unused files, consider creating a 'fileGroup' with "//visibility:private" +// to attach the license to, and including a comment whether the files may be +// used in the current project. +// +// large-scale-change included anything that looked like it might be a license +// text as a license_text. e.g. LICENSE, NOTICE, COPYING etc. +// +// Please consider removing redundant or irrelevant files from 'license_text:'. +// See: http://go/android-license-faq +license { + name: "external_rust_crates_byteorder_license", + visibility: [":__subpackages__"], + license_kinds: [ + "SPDX-license-identifier-MIT", + "SPDX-license-identifier-Unlicense", + ], + license_text: [ + "COPYING", + "LICENSE-MIT", + "UNLICENSE", + ], +} + rust_library { name: "libbyteorder", host_supported: true, -- cgit v1.2.3 From 578f4351d0a4d79e3637be7f3fcae507c7756328 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Thu, 1 Apr 2021 15:18:27 -0700 Subject: Upgrade rust/crates/byteorder to 1.4.3 Test: make Change-Id: Icad438801086a0af9e85384207cb395f702a555a --- .cargo_vcs_info.json | 2 +- .travis.yml | 15 --------- Android.bp | 1 + Cargo.toml | 2 +- Cargo.toml.orig | 2 +- METADATA | 8 ++--- README.md | 4 +-- src/io.rs | 3 +- src/lib.rs | 94 ++++++++++++++++++++++++++++++---------------------- 9 files changed, 66 insertions(+), 65 deletions(-) delete mode 100644 .travis.yml diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index e260c24..cd7856e 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "ca8c10a3f85e2c66781f7d12e90196e6923592d7" + "sha1": "abffade8232229db557e0a30c395963071624b2b" } } diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 082c5cc..0000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: rust -matrix: - include: - - rust: 1.12.0 - - rust: stable - - rust: beta - - rust: nightly - - env: CROSS_TARGET=mips64-unknown-linux-gnuabi64 - rust: stable - services: docker - sudo: required -script: ci/script.sh -branches: - only: - - master diff --git a/Android.bp b/Android.bp index 899071d..3efba93 100644 --- a/Android.bp +++ b/Android.bp @@ -1,4 +1,5 @@ // This file is generated by cargo2android.py --run --device --dependencies. +// Do not modify this file as changes will be overridden on upgrade. package { default_applicable_licenses: ["external_rust_crates_byteorder_license"], diff --git a/Cargo.toml b/Cargo.toml index ff60b60..c71f90b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ [package] edition = "2018" name = "byteorder" -version = "1.4.2" +version = "1.4.3" authors = ["Andrew Gallant "] exclude = ["/ci/*"] description = "Library for reading/writing numbers in big-endian and little-endian." diff --git a/Cargo.toml.orig b/Cargo.toml.orig index efeafb2..728f668 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "byteorder" -version = "1.4.2" #:version +version = "1.4.3" #:version authors = ["Andrew Gallant "] description = "Library for reading/writing numbers in big-endian and little-endian." documentation = "https://docs.rs/byteorder" diff --git a/METADATA b/METADATA index 95a62dd..c5a4b7d 100644 --- a/METADATA +++ b/METADATA @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/byteorder/byteorder-1.4.2.crate" + value: "https://static.crates.io/crates/byteorder/byteorder-1.4.3.crate" } - version: "1.4.2" + version: "1.4.3" license_type: NOTICE last_upgrade_date { year: 2021 - month: 2 - day: 7 + month: 4 + day: 1 } } diff --git a/README.md b/README.md index 8b2ecc4..d8461c5 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order. [![Build status](https://github.com/BurntSushi/byteorder/workflows/ci/badge.svg)](https://github.com/BurntSushi/byteorder/actions) -[![](http://meritbadge.herokuapp.com/byteorder)](https://crates.io/crates/byteorder) +[![](https://meritbadge.herokuapp.com/byteorder)](https://crates.io/crates/byteorder) -Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org). +Dual-licensed under MIT or the [UNLICENSE](https://unlicense.org/). ### Documentation diff --git a/src/io.rs b/src/io.rs index ac41ff7..dfad2ca 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1582,7 +1582,8 @@ impl WriteBytesExt for W {} /// representation. /// /// This function is wildly unsafe because it permits arbitrary modification of -/// the binary representation of any `Copy` type. Use with care. +/// the binary representation of any `Copy` type. Use with care. It's intended +/// to be called only where `T` is a numeric type. unsafe fn slice_to_u8_mut(slice: &mut [T]) -> &mut [u8] { use std::mem::size_of; diff --git a/src/lib.rs b/src/lib.rs index d56574d..cc37cca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1557,9 +1557,7 @@ pub trait ByteOrder: /// LittleEndian::write_f32_into(&numbers_given, &mut bytes); /// /// let mut numbers_got = [0.0; 4]; - /// unsafe { - /// LittleEndian::read_f32_into(&bytes, &mut numbers_got); - /// } + /// LittleEndian::read_f32_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` fn write_f32_into(src: &[f32], dst: &mut [u8]) { @@ -1588,9 +1586,7 @@ pub trait ByteOrder: /// LittleEndian::write_f64_into(&numbers_given, &mut bytes); /// /// let mut numbers_got = [0.0; 4]; - /// unsafe { - /// LittleEndian::read_f64_into(&bytes, &mut numbers_got); - /// } + /// LittleEndian::read_f64_into(&bytes, &mut numbers_got); /// assert_eq!(numbers_given, numbers_got); /// ``` fn write_f64_into(src: &[f64], dst: &mut [u8]) { @@ -1696,7 +1692,7 @@ pub trait ByteOrder: #[inline] fn from_slice_i16(src: &mut [i16]) { let src = unsafe { - slice::from_raw_parts_mut(src.as_ptr() as *mut u16, src.len()) + slice::from_raw_parts_mut(src.as_mut_ptr() as *mut u16, src.len()) }; Self::from_slice_u16(src); } @@ -1721,7 +1717,7 @@ pub trait ByteOrder: #[inline] fn from_slice_i32(src: &mut [i32]) { let src = unsafe { - slice::from_raw_parts_mut(src.as_ptr() as *mut u32, src.len()) + slice::from_raw_parts_mut(src.as_mut_ptr() as *mut u32, src.len()) }; Self::from_slice_u32(src); } @@ -1746,7 +1742,7 @@ pub trait ByteOrder: #[inline] fn from_slice_i64(src: &mut [i64]) { let src = unsafe { - slice::from_raw_parts_mut(src.as_ptr() as *mut u64, src.len()) + slice::from_raw_parts_mut(src.as_mut_ptr() as *mut u64, src.len()) }; Self::from_slice_u64(src); } @@ -1771,7 +1767,7 @@ pub trait ByteOrder: #[inline] fn from_slice_i128(src: &mut [i128]) { let src = unsafe { - slice::from_raw_parts_mut(src.as_ptr() as *mut u128, src.len()) + slice::from_raw_parts_mut(src.as_mut_ptr() as *mut u128, src.len()) }; Self::from_slice_u128(src); } @@ -1899,7 +1895,13 @@ pub type NativeEndian = LittleEndian; #[cfg(target_endian = "big")] pub type NativeEndian = BigEndian; -macro_rules! write_num_bytes { +/// Copies $size bytes from a number $n to a &mut [u8] $dst. $ty represents the +/// numeric type of $n and $which must be either to_be or to_le, depending on +/// which endianness one wants to use when writing to $dst. +/// +/// This macro is only safe to call when $ty is a numeric type and $size == +/// size_of::<$ty>() and where $dst is a &mut [u8]. +macro_rules! unsafe_write_num_bytes { ($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => {{ assert!($size <= $dst.len()); unsafe { @@ -1910,7 +1912,13 @@ macro_rules! write_num_bytes { }}; } -macro_rules! read_slice { +/// Copies a &[u8] $src into a &mut [] $dst for the endianness given +/// by $which (must be either to_be or to_le). +/// +/// This macro is only safe to call when $src and $dst are &[u8] and &mut [u8], +/// respectively. The macro will panic if $src.len() != $size * $dst.len(), +/// where $size represents the size of the integers encoded in $src. +macro_rules! unsafe_read_slice { ($src:expr, $dst:expr, $size:expr, $which:ident) => {{ assert_eq!($src.len(), $size * $dst.len()); @@ -1927,10 +1935,16 @@ macro_rules! read_slice { }}; } -macro_rules! write_slice_native { - ($src:expr, $dst:expr, $ty:ty, $size:expr) => {{ - assert!($size == ::core::mem::size_of::<$ty>()); - assert_eq!($size * $src.len(), $dst.len()); +/// Copies a &[$ty] $src into a &mut [u8] $dst, where $ty must be a numeric +/// type. This panics if size_of::<$ty>() * $src.len() != $dst.len(). +/// +/// This macro is only safe to call when $src is a slice of numeric types and +/// $dst is a &mut [u8] and where $ty represents the type of the integers in +/// $src. +macro_rules! unsafe_write_slice_native { + ($src:expr, $dst:expr, $ty:ty) => {{ + let size = core::mem::size_of::<$ty>(); + assert_eq!(size * $src.len(), $dst.len()); unsafe { copy_nonoverlapping( @@ -2006,22 +2020,22 @@ impl ByteOrder for BigEndian { #[inline] fn write_u16(buf: &mut [u8], n: u16) { - write_num_bytes!(u16, 2, n, buf, to_be); + unsafe_write_num_bytes!(u16, 2, n, buf, to_be); } #[inline] fn write_u32(buf: &mut [u8], n: u32) { - write_num_bytes!(u32, 4, n, buf, to_be); + unsafe_write_num_bytes!(u32, 4, n, buf, to_be); } #[inline] fn write_u64(buf: &mut [u8], n: u64) { - write_num_bytes!(u64, 8, n, buf, to_be); + unsafe_write_num_bytes!(u64, 8, n, buf, to_be); } #[inline] fn write_u128(buf: &mut [u8], n: u128) { - write_num_bytes!(u128, 16, n, buf, to_be); + unsafe_write_num_bytes!(u128, 16, n, buf, to_be); } #[inline] @@ -2054,28 +2068,28 @@ impl ByteOrder for BigEndian { #[inline] fn read_u16_into(src: &[u8], dst: &mut [u16]) { - read_slice!(src, dst, 2, to_be); + unsafe_read_slice!(src, dst, 2, to_be); } #[inline] fn read_u32_into(src: &[u8], dst: &mut [u32]) { - read_slice!(src, dst, 4, to_be); + unsafe_read_slice!(src, dst, 4, to_be); } #[inline] fn read_u64_into(src: &[u8], dst: &mut [u64]) { - read_slice!(src, dst, 8, to_be); + unsafe_read_slice!(src, dst, 8, to_be); } #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { - read_slice!(src, dst, 16, to_be); + unsafe_read_slice!(src, dst, 16, to_be); } #[inline] fn write_u16_into(src: &[u16], dst: &mut [u8]) { if cfg!(target_endian = "big") { - write_slice_native!(src, dst, u16, 2); + unsafe_write_slice_native!(src, dst, u16); } else { write_slice!(src, dst, u16, 2, Self::write_u16); } @@ -2084,7 +2098,7 @@ impl ByteOrder for BigEndian { #[inline] fn write_u32_into(src: &[u32], dst: &mut [u8]) { if cfg!(target_endian = "big") { - write_slice_native!(src, dst, u32, 4); + unsafe_write_slice_native!(src, dst, u32); } else { write_slice!(src, dst, u32, 4, Self::write_u32); } @@ -2093,7 +2107,7 @@ impl ByteOrder for BigEndian { #[inline] fn write_u64_into(src: &[u64], dst: &mut [u8]) { if cfg!(target_endian = "big") { - write_slice_native!(src, dst, u64, 8); + unsafe_write_slice_native!(src, dst, u64); } else { write_slice!(src, dst, u64, 8, Self::write_u64); } @@ -2102,7 +2116,7 @@ impl ByteOrder for BigEndian { #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "big") { - write_slice_native!(src, dst, u128, 16); + unsafe_write_slice_native!(src, dst, u128); } else { write_slice!(src, dst, u128, 16, Self::write_u128); } @@ -2214,22 +2228,22 @@ impl ByteOrder for LittleEndian { #[inline] fn write_u16(buf: &mut [u8], n: u16) { - write_num_bytes!(u16, 2, n, buf, to_le); + unsafe_write_num_bytes!(u16, 2, n, buf, to_le); } #[inline] fn write_u32(buf: &mut [u8], n: u32) { - write_num_bytes!(u32, 4, n, buf, to_le); + unsafe_write_num_bytes!(u32, 4, n, buf, to_le); } #[inline] fn write_u64(buf: &mut [u8], n: u64) { - write_num_bytes!(u64, 8, n, buf, to_le); + unsafe_write_num_bytes!(u64, 8, n, buf, to_le); } #[inline] fn write_u128(buf: &mut [u8], n: u128) { - write_num_bytes!(u128, 16, n, buf, to_le); + unsafe_write_num_bytes!(u128, 16, n, buf, to_le); } #[inline] @@ -2254,28 +2268,28 @@ impl ByteOrder for LittleEndian { #[inline] fn read_u16_into(src: &[u8], dst: &mut [u16]) { - read_slice!(src, dst, 2, to_le); + unsafe_read_slice!(src, dst, 2, to_le); } #[inline] fn read_u32_into(src: &[u8], dst: &mut [u32]) { - read_slice!(src, dst, 4, to_le); + unsafe_read_slice!(src, dst, 4, to_le); } #[inline] fn read_u64_into(src: &[u8], dst: &mut [u64]) { - read_slice!(src, dst, 8, to_le); + unsafe_read_slice!(src, dst, 8, to_le); } #[inline] fn read_u128_into(src: &[u8], dst: &mut [u128]) { - read_slice!(src, dst, 16, to_le); + unsafe_read_slice!(src, dst, 16, to_le); } #[inline] fn write_u16_into(src: &[u16], dst: &mut [u8]) { if cfg!(target_endian = "little") { - write_slice_native!(src, dst, u16, 2); + unsafe_write_slice_native!(src, dst, u16); } else { write_slice!(src, dst, u16, 2, Self::write_u16); } @@ -2284,7 +2298,7 @@ impl ByteOrder for LittleEndian { #[inline] fn write_u32_into(src: &[u32], dst: &mut [u8]) { if cfg!(target_endian = "little") { - write_slice_native!(src, dst, u32, 4); + unsafe_write_slice_native!(src, dst, u32); } else { write_slice!(src, dst, u32, 4, Self::write_u32); } @@ -2293,7 +2307,7 @@ impl ByteOrder for LittleEndian { #[inline] fn write_u64_into(src: &[u64], dst: &mut [u8]) { if cfg!(target_endian = "little") { - write_slice_native!(src, dst, u64, 8); + unsafe_write_slice_native!(src, dst, u64); } else { write_slice!(src, dst, u64, 8, Self::write_u64); } @@ -2302,7 +2316,7 @@ impl ByteOrder for LittleEndian { #[inline] fn write_u128_into(src: &[u128], dst: &mut [u8]) { if cfg!(target_endian = "little") { - write_slice_native!(src, dst, u128, 16); + unsafe_write_slice_native!(src, dst, u128); } else { write_slice!(src, dst, u128, 16, Self::write_u128); } -- cgit v1.2.3