Vendor things
This commit is contained in:
parent
5deceec006
commit
977e3c17e5
19434 changed files with 10682014 additions and 0 deletions
1
third-party/vendor/arrayref/.cargo-checksum.json
vendored
Normal file
1
third-party/vendor/arrayref/.cargo-checksum.json
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"files":{"Cargo.lock":"ff016e656e3e2fe749385004fe8fee304a9a4a82fbeaf7a47a9784cdee636129","Cargo.toml":"3db870621795ec0318455348aa94d649a08ee46737d3b8d2b05dc3d6c2b915ba","LICENSE":"1bc7e6f475b3ec99b7e2643411950ae2368c250dd4c5c325f80f9811362a94a1","README.md":"f77d8910fa16f9f0ad2d72d039a7e8ac2979834e0acf435b7c84744c90cb21ec","examples/array_refs.rs":"336b52f6ab31d78766bd1653cea60b9f183d80369f3feec55e454ccbcb3adaaa","examples/array_refs_with_const.rs":"9e49959de714c611bc2e48bb0dd51c3023abc2b3e6b6e0428e7d7b30be8900e4","examples/simple-case.rs":"12a7c596db0d8e89415dfd75a8fe390a7141b24771ad70aee73286857a37b5fb","src/lib.rs":"6ade4aa7e3ffb5960e0bc43fd6f2594a0ccd0332ee17f8278950140803470429"},"package":"6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"}
|
||||
205
third-party/vendor/arrayref/Cargo.lock
generated
vendored
Normal file
205
third-party/vendor/arrayref/Cargo.lock
generated
vendored
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.7"
|
||||
dependencies = [
|
||||
"quickcheck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.5.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"humantime",
|
||||
"log",
|
||||
"regex",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-cprng"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
|
||||
dependencies = [
|
||||
"quick-error",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.140"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "quick-error"
|
||||
version = "1.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
|
||||
|
||||
[[package]]
|
||||
name = "quickcheck"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c01babc5ffd48a2a83744b3024814bb46dfd4f2a4705ccb44b1b60e644fdcab7"
|
||||
dependencies = [
|
||||
"env_logger",
|
||||
"log",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
|
||||
dependencies = [
|
||||
"fuchsia-cprng",
|
||||
"libc",
|
||||
"rand_core 0.3.1",
|
||||
"rdrand",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
|
||||
dependencies = [
|
||||
"rand_core 0.4.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
|
||||
|
||||
[[package]]
|
||||
name = "rdrand"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
23
third-party/vendor/arrayref/Cargo.toml
vendored
Normal file
23
third-party/vendor/arrayref/Cargo.toml
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
name = "arrayref"
|
||||
version = "0.3.7"
|
||||
authors = ["David Roundy <roundyd@physics.oregonstate.edu>"]
|
||||
description = "Macros to take array references of slices"
|
||||
documentation = "https://docs.rs/arrayref"
|
||||
readme = "README.md"
|
||||
license = "BSD-2-Clause"
|
||||
repository = "https://github.com/droundy/arrayref"
|
||||
|
||||
[dev-dependencies.quickcheck]
|
||||
version = "0.6"
|
||||
26
third-party/vendor/arrayref/LICENSE
vendored
Normal file
26
third-party/vendor/arrayref/LICENSE
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
Copyright (c) 2015 David Roundy <roundyd@physics.oregonstate.edu>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
109
third-party/vendor/arrayref/README.md
vendored
Normal file
109
third-party/vendor/arrayref/README.md
vendored
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
# arrayref
|
||||
|
||||
[](https://travis-ci.org/droundy/arrayref)
|
||||
[](https://coveralls.io/github/droundy/arrayref?branch=master)
|
||||
|
||||
[Documentation](https://docs.rs/arrayref)
|
||||
|
||||
This is a very small rust module, which contains just four macros, for
|
||||
the taking of array references to slices of... sliceable things.
|
||||
These macros (which are awkwardly named) should be perfectly safe, and
|
||||
have seen just a tad of code review.
|
||||
|
||||
## Why would I want this?
|
||||
|
||||
The goal of arrayref is to enable the effective use of APIs that
|
||||
involve array references rather than slices, for situations where
|
||||
parameters must have a given size. As an example, consider the
|
||||
`byteorder` crate. This is a very nice crate with a simple API
|
||||
containing functions that look like:
|
||||
|
||||
```rust
|
||||
fn read_u16(buf: &[u8]) -> u16;
|
||||
fn write_u16(buf: &mut [u8], n: u16);
|
||||
```
|
||||
|
||||
Looking at this, you might wonder why they accept a slice reference as
|
||||
input. After all, they always want just two bytes. These functions
|
||||
must panic if given a slice that is too small, which means that unless
|
||||
they are inlined, then a runtime bounds-check is forced, even if it
|
||||
may be statically known that the input is the right size.
|
||||
|
||||
Wouldn't it be nicer if we had functions more like
|
||||
|
||||
```rust
|
||||
fn read_u16_array(buf: &[u8; 2]) -> u16;
|
||||
fn write_u16_array(buf: &mut [u8; 2], n: u16);
|
||||
```
|
||||
|
||||
The type signature would tell users precisely what size of input is
|
||||
required, and the compiler could check at compile time that the input
|
||||
is of the appropriate size: this sounds like the zero-cost
|
||||
abstractions rust is famous for! However, there is a catch, which
|
||||
arises when you try to *use* these nicer functions, which is that
|
||||
usually you are looking at two bytes in a stream. So, e.g. consider
|
||||
that we are working with a hypothetical (and simplified) ipv6 address.
|
||||
|
||||
Doing this with our array version (which looks so beautiful in terms
|
||||
of accurately describing what we want!) looks terrible:
|
||||
|
||||
```rust
|
||||
let addr: &[u8; 16] = ...;
|
||||
let mut segments = [0u16; 8];
|
||||
// array-based API
|
||||
for i in 0 .. 8 {
|
||||
let mut two_bytes = [addr[2*i], addr[2*i+1]];
|
||||
segments[i] = read_u16_array(&two_bytes);
|
||||
}
|
||||
// slice-based API
|
||||
for i in 0 .. 8 {
|
||||
segments[i] = read_u16(&addr[2*i..]);
|
||||
}
|
||||
```
|
||||
|
||||
The array-based approach looks way worse. We need to create a fresh
|
||||
copy of the bytes, just so it will be in an array of the proper size!
|
||||
Thus the whole "zero-cost abstraction" argument for using array
|
||||
references fails. The trouble is that there is no (safe) way (until
|
||||
[RFC 495][1] lands) to obtain an array reference to a portion of a
|
||||
larger array or slice. Doing so is the equivalent of taking a slice
|
||||
with a size known at compile time, and ought to be built into the
|
||||
language.
|
||||
|
||||
[1]: https://github.com/rust-lang/rfcs/blob/master/text/0495-array-pattern-changes.md
|
||||
|
||||
The arrayref crate allows you to do this kind of slicing. So the
|
||||
above (very contrived) example can be implemented with array
|
||||
references as:
|
||||
|
||||
```rust
|
||||
let addr: &[u8; 16] = ...;
|
||||
let mut segments = [0u16; 8];
|
||||
// array-based API with arrayref
|
||||
for i in 0 .. 8 {
|
||||
segments[i] = read_u16_array(array_ref![addr,2*i,2]);
|
||||
}
|
||||
```
|
||||
|
||||
Here the `array_ref![addr,2*i,2]` macro allows us to take an array
|
||||
reference to a slice consisting of two bytes starting at `2*i`. Apart
|
||||
from the syntax (less nice than slicing), it is essentially the same
|
||||
as the slice approach. However, this code makes explicit the
|
||||
need for precisely *two* bytes both in the caller, and in the function
|
||||
signature.
|
||||
|
||||
This module provides three other macros providing related
|
||||
functionality, which you can read about in the
|
||||
[documentation](https://droundy.github.io/arrayref).
|
||||
|
||||
For an example of how these macros can be used in an actual program,
|
||||
see [my rust translation of tweetnacl][2], which uses `arrayref`
|
||||
to almost exclusively accept array references in functions, with the
|
||||
only exception being those which truly expect data of arbitrary
|
||||
length. In my opinion, the result is code that is far more legible
|
||||
than the original C code, since the size of each argument is
|
||||
explicit. Moreover (although I have not tested this), the use of
|
||||
array references rather than slices *should* result in far fewer
|
||||
bounds checks, since almost all sizes are known at compile time.
|
||||
|
||||
[2]: https://github.com/droundy/onionsalt/blob/master/src/crypto.rs
|
||||
13
third-party/vendor/arrayref/examples/array_refs.rs
vendored
Normal file
13
third-party/vendor/arrayref/examples/array_refs.rs
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#[macro_use]
|
||||
extern crate arrayref;
|
||||
|
||||
fn main() {
|
||||
let x = [0,1,2,3,4,5,6,7,8,9];
|
||||
let (a,b,c) = array_refs!(&x, 2, 3, 5);
|
||||
assert_eq!(2, a.len());
|
||||
assert_eq!(3, b.len());
|
||||
assert_eq!(5, c.len());
|
||||
assert_eq!(*a, [0,1]);
|
||||
assert_eq!(*b, [2,3,4]);
|
||||
assert_eq!(*c, [5,6,7,8,9]);
|
||||
}
|
||||
18
third-party/vendor/arrayref/examples/array_refs_with_const.rs
vendored
Normal file
18
third-party/vendor/arrayref/examples/array_refs_with_const.rs
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#[macro_use]
|
||||
extern crate arrayref;
|
||||
|
||||
const SIZE: usize = 10;
|
||||
const SIZE_A: usize = 2;
|
||||
const SIZE_B: usize = 3;
|
||||
const SIZE_C: usize = 5;
|
||||
|
||||
fn main() {
|
||||
let x: [u8; SIZE] = [0,1,2,3,4,5,6,7,8,9];
|
||||
let (a,b,c) = array_refs!(&x, SIZE_A, SIZE_B, SIZE_C);
|
||||
assert_eq!(SIZE_A, a.len());
|
||||
assert_eq!(SIZE_B, b.len());
|
||||
assert_eq!(SIZE_C, c.len());
|
||||
assert_eq!(*a, [0,1]);
|
||||
assert_eq!(*b, [2,3,4]);
|
||||
assert_eq!(*c, [5,6,7,8,9]);
|
||||
}
|
||||
15
third-party/vendor/arrayref/examples/simple-case.rs
vendored
Normal file
15
third-party/vendor/arrayref/examples/simple-case.rs
vendored
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#[macro_use]
|
||||
extern crate arrayref;
|
||||
|
||||
fn add_three(a: &[u8; 3]) -> u8 {
|
||||
a[0] + a[1] + a[2]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut x = [0u8; 30];
|
||||
x[20] = 1;
|
||||
x[21] = 4;
|
||||
x[24] = 3;
|
||||
x[0] = add_three(array_mut_ref![x,20,3]);
|
||||
assert_eq!(x[0], 8);
|
||||
}
|
||||
502
third-party/vendor/arrayref/src/lib.rs
vendored
Normal file
502
third-party/vendor/arrayref/src/lib.rs
vendored
Normal file
|
|
@ -0,0 +1,502 @@
|
|||
//! This package contains just four macros, which enable the creation
|
||||
//! of array references to portions of arrays or slices (or things
|
||||
//! that can be sliced).
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! Here is a simple example of slicing and dicing a slice into array
|
||||
//! references with these macros. Here we implement a simple
|
||||
//! little-endian conversion from bytes to `u16`, and demonstrate code
|
||||
//! that uses `array_ref!` to extract an array reference from a larger
|
||||
//! array. Note that the documentation for each macro also has an
|
||||
//! example of its use.
|
||||
//!
|
||||
//! ```
|
||||
//! #[macro_use]
|
||||
//! extern crate arrayref;
|
||||
//!
|
||||
//! fn read_u16(bytes: &[u8; 2]) -> u16 {
|
||||
//! bytes[0] as u16 + ((bytes[1] as u16) << 8)
|
||||
//! }
|
||||
//! // ...
|
||||
//! # fn main() {
|
||||
//! let data = [0,1,2,3,4,0,6,7,8,9];
|
||||
//! assert_eq!(256, read_u16(array_ref![data,0,2]));
|
||||
//! assert_eq!(4, read_u16(array_ref![data,4,2]));
|
||||
//! # }
|
||||
//! ```
|
||||
#![deny(warnings)]
|
||||
#![no_std]
|
||||
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate std;
|
||||
|
||||
/// You can use `array_ref` to generate an array reference to a subset
|
||||
/// of a sliceable bit of data (which could be an array, or a slice,
|
||||
/// or a Vec).
|
||||
///
|
||||
/// **Panics** if the slice is out of bounds.
|
||||
///
|
||||
/// ```
|
||||
/// #[macro_use]
|
||||
/// extern crate arrayref;
|
||||
///
|
||||
/// fn read_u16(bytes: &[u8; 2]) -> u16 {
|
||||
/// bytes[0] as u16 + ((bytes[1] as u16) << 8)
|
||||
/// }
|
||||
/// // ...
|
||||
/// # fn main() {
|
||||
/// let data = [0,1,2,3,4,0,6,7,8,9];
|
||||
/// assert_eq!(256, read_u16(array_ref![data,0,2]));
|
||||
/// assert_eq!(4, read_u16(array_ref![data,4,2]));
|
||||
/// # }
|
||||
/// ```
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! array_ref {
|
||||
($arr:expr, $offset:expr, $len:expr) => {{
|
||||
{
|
||||
#[inline]
|
||||
unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] {
|
||||
&*(slice.as_ptr() as *const [_; $len])
|
||||
}
|
||||
let offset = $offset;
|
||||
let slice = & $arr[offset..offset + $len];
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
as_array(slice)
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
/// You can use `array_refs` to generate a series of array references
|
||||
/// to an input array reference. The idea is if you want to break an
|
||||
/// array into a series of contiguous and non-overlapping arrays.
|
||||
/// `array_refs` is a bit funny in that it insists on slicing up the
|
||||
/// *entire* array. This is intentional, as I find it handy to make
|
||||
/// me ensure that my sub-arrays add up to the entire array. This
|
||||
/// macro will *never* panic, since the sizes are all checked at
|
||||
/// compile time.
|
||||
///
|
||||
/// Note that unlike `array_ref!`, `array_refs` *requires* that the
|
||||
/// first argument be an array reference. The following arguments are
|
||||
/// the lengths of each subarray you wish a reference to. The total
|
||||
/// of these arguments *must* equal the size of the array itself.
|
||||
///
|
||||
/// ```
|
||||
/// #[macro_use]
|
||||
/// extern crate arrayref;
|
||||
///
|
||||
/// fn read_u16(bytes: &[u8; 2]) -> u16 {
|
||||
/// bytes[0] as u16 + ((bytes[1] as u16) << 8)
|
||||
/// }
|
||||
/// // ...
|
||||
/// # fn main() {
|
||||
/// let data = [0,1,2,3,4,0,6,7];
|
||||
/// let (a,b,c) = array_refs![&data,2,2,4];
|
||||
/// assert_eq!(read_u16(a), 256);
|
||||
/// assert_eq!(read_u16(b), 3*256+2);
|
||||
/// assert_eq!(*c, [4,0,6,7]);
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! array_refs {
|
||||
( $arr:expr, $( $pre:expr ),* ; .. ; $( $post:expr ),* ) => {{
|
||||
{
|
||||
use core::slice;
|
||||
#[inline]
|
||||
#[allow(unused_assignments)]
|
||||
#[allow(clippy::eval_order_dependence)]
|
||||
unsafe fn as_arrays<T>(a: &[T]) -> ( $( &[T; $pre], )* &[T], $( &[T; $post], )*) {
|
||||
let min_len = $( $pre + )* $( $post + )* 0;
|
||||
let var_len = a.len() - min_len;
|
||||
assert!(a.len() >= min_len);
|
||||
let mut p = a.as_ptr();
|
||||
( $( {
|
||||
let aref = & *(p as *const [T; $pre]);
|
||||
p = p.add($pre);
|
||||
aref
|
||||
}, )* {
|
||||
let sl = slice::from_raw_parts(p as *const T, var_len);
|
||||
p = p.add(var_len);
|
||||
sl
|
||||
}, $( {
|
||||
let aref = & *(p as *const [T; $post]);
|
||||
p = p.add($post);
|
||||
aref
|
||||
}, )*)
|
||||
}
|
||||
let input = $arr;
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
as_arrays(input)
|
||||
}
|
||||
}
|
||||
}};
|
||||
( $arr:expr, $( $len:expr ),* ) => {{
|
||||
{
|
||||
#[inline]
|
||||
#[allow(unused_assignments)]
|
||||
#[allow(clippy::eval_order_dependence)]
|
||||
unsafe fn as_arrays<T>(a: &[T; $( $len + )* 0 ]) -> ( $( &[T; $len], )* ) {
|
||||
let mut p = a.as_ptr();
|
||||
( $( {
|
||||
let aref = &*(p as *const [T; $len]);
|
||||
p = p.offset($len as isize);
|
||||
aref
|
||||
}, )* )
|
||||
}
|
||||
let input = $arr;
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
as_arrays(input)
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
|
||||
/// You can use `mut_array_refs` to generate a series of mutable array
|
||||
/// references to an input mutable array reference. The idea is if
|
||||
/// you want to break an array into a series of contiguous and
|
||||
/// non-overlapping mutable array references. Like `array_refs!`,
|
||||
/// `mut_array_refs!` is a bit funny in that it insists on slicing up
|
||||
/// the *entire* array. This is intentional, as I find it handy to
|
||||
/// make me ensure that my sub-arrays add up to the entire array.
|
||||
/// This macro will *never* panic, since the sizes are all checked at
|
||||
/// compile time.
|
||||
///
|
||||
/// Note that unlike `array_mut_ref!`, `mut_array_refs` *requires*
|
||||
/// that the first argument be a mutable array reference. The
|
||||
/// following arguments are the lengths of each subarray you wish a
|
||||
/// reference to. The total of these arguments *must* equal the size
|
||||
/// of the array itself. Also note that this macro allows you to take
|
||||
/// out multiple mutable references to a single object, which is both
|
||||
/// weird and powerful.
|
||||
///
|
||||
/// ```
|
||||
/// #[macro_use]
|
||||
/// extern crate arrayref;
|
||||
///
|
||||
/// fn write_u16(bytes: &mut [u8; 2], num: u16) {
|
||||
/// bytes[0] = num as u8;
|
||||
/// bytes[1] = (num >> 8) as u8;
|
||||
/// }
|
||||
/// fn write_u32(bytes: &mut [u8; 4], num: u32) {
|
||||
/// bytes[0] = num as u8;
|
||||
/// bytes[1] = (num >> 8) as u8; // this is buggy to save space...
|
||||
/// }
|
||||
/// // ...
|
||||
/// # fn main() {
|
||||
/// let mut data = [0,1,2,3,4,0,6,7];
|
||||
/// let (a,b,c) = mut_array_refs![&mut data,2,2,4];
|
||||
/// // let's write out some nice prime numbers!
|
||||
/// write_u16(a, 37);
|
||||
/// write_u16(b, 73);
|
||||
/// write_u32(c, 137); // approximate inverse of the fine structure constant!
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! mut_array_refs {
|
||||
( $arr:expr, $( $pre:expr ),* ; .. ; $( $post:expr ),* ) => {{
|
||||
{
|
||||
use core::slice;
|
||||
#[inline]
|
||||
#[allow(unused_assignments)]
|
||||
#[allow(clippy::eval_order_dependence)]
|
||||
unsafe fn as_arrays<T>(a: &mut [T]) -> ( $( &mut [T; $pre], )* &mut [T], $( &mut [T; $post], )*) {
|
||||
let min_len = $( $pre + )* $( $post + )* 0;
|
||||
let var_len = a.len() - min_len;
|
||||
assert!(a.len() >= min_len);
|
||||
let mut p = a.as_mut_ptr();
|
||||
( $( {
|
||||
let aref = &mut *(p as *mut [T; $pre]);
|
||||
p = p.add($pre);
|
||||
aref
|
||||
}, )* {
|
||||
let sl = slice::from_raw_parts_mut(p as *mut T, var_len);
|
||||
p = p.add(var_len);
|
||||
sl
|
||||
}, $( {
|
||||
let aref = &mut *(p as *mut [T; $post]);
|
||||
p = p.add($post);
|
||||
aref
|
||||
}, )*)
|
||||
}
|
||||
let input = $arr;
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
as_arrays(input)
|
||||
}
|
||||
}
|
||||
}};
|
||||
( $arr:expr, $( $len:expr ),* ) => {{
|
||||
{
|
||||
#[inline]
|
||||
#[allow(unused_assignments)]
|
||||
#[allow(clippy::eval_order_dependence)]
|
||||
unsafe fn as_arrays<T>(a: &mut [T; $( $len + )* 0 ]) -> ( $( &mut [T; $len], )* ) {
|
||||
let mut p = a.as_mut_ptr();
|
||||
( $( {
|
||||
let aref = &mut *(p as *mut [T; $len]);
|
||||
p = p.add($len);
|
||||
aref
|
||||
}, )* )
|
||||
}
|
||||
let input = $arr;
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
as_arrays(input)
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// You can use `array_mut_ref` to generate a mutable array reference
|
||||
/// to a subset of a sliceable bit of data (which could be an array,
|
||||
/// or a slice, or a Vec).
|
||||
///
|
||||
/// **Panics** if the slice is out of bounds.
|
||||
///
|
||||
/// ```
|
||||
/// #[macro_use]
|
||||
/// extern crate arrayref;
|
||||
///
|
||||
/// fn write_u16(bytes: &mut [u8; 2], num: u16) {
|
||||
/// bytes[0] = num as u8;
|
||||
/// bytes[1] = (num >> 8) as u8;
|
||||
/// }
|
||||
/// // ...
|
||||
/// # fn main() {
|
||||
/// let mut data = [0,1,2,3,4,0,6,7,8,9];
|
||||
/// write_u16(array_mut_ref![data,0,2], 1);
|
||||
/// write_u16(array_mut_ref![data,2,2], 5);
|
||||
/// assert_eq!(*array_ref![data,0,4], [1,0,5,0]);
|
||||
/// *array_mut_ref![data,4,5] = [4,3,2,1,0];
|
||||
/// assert_eq!(data, [1,0,5,0,4,3,2,1,0,9]);
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! array_mut_ref {
|
||||
($arr:expr, $offset:expr, $len:expr) => {{
|
||||
{
|
||||
#[inline]
|
||||
unsafe fn as_array<T>(slice: &mut [T]) -> &mut [T; $len] {
|
||||
&mut *(slice.as_mut_ptr() as *mut [_; $len])
|
||||
}
|
||||
let offset = $offset;
|
||||
let slice = &mut $arr[offset..offset + $len];
|
||||
#[allow(unused_unsafe)]
|
||||
unsafe {
|
||||
as_array(slice)
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
|
||||
#[allow(clippy::all)]
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
extern crate quickcheck;
|
||||
|
||||
use std::vec::Vec;
|
||||
|
||||
// use super::*;
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn checks_bounds() {
|
||||
let foo: [u8; 11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
let bar = array_ref!(foo, 1, 11);
|
||||
println!("I am checking that I can dereference bar[0] = {}", bar[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_case_works() {
|
||||
fn check(expected: [u8; 3], actual: &[u8; 3]) {
|
||||
for (e, a) in (&expected).iter().zip(actual.iter()) {
|
||||
assert_eq!(e, a)
|
||||
}
|
||||
}
|
||||
let mut foo: [u8; 11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
{
|
||||
let bar = array_ref!(foo, 2, 3);
|
||||
check([2, 3, 4], bar);
|
||||
}
|
||||
check([0, 1, 2], array_ref!(foo, 0, 3));
|
||||
fn zero2(x: &mut [u8; 2]) {
|
||||
x[0] = 0;
|
||||
x[1] = 0;
|
||||
}
|
||||
zero2(array_mut_ref!(foo, 8, 2));
|
||||
check([0, 0, 10], array_ref!(foo, 8, 3));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn check_array_ref_5() {
|
||||
fn f(data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
|
||||
if data.len() < offset + 5 {
|
||||
return quickcheck::TestResult::discard();
|
||||
}
|
||||
let out = array_ref!(data, offset, 5);
|
||||
quickcheck::TestResult::from_bool(out.len() == 5)
|
||||
}
|
||||
quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_array_ref_out_of_bounds_5() {
|
||||
fn f(data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
|
||||
if data.len() >= offset + 5 {
|
||||
return quickcheck::TestResult::discard();
|
||||
}
|
||||
quickcheck::TestResult::must_fail(move || {
|
||||
array_ref!(data, offset, 5);
|
||||
})
|
||||
}
|
||||
quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_array_mut_ref_7() {
|
||||
fn f(mut data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
|
||||
if data.len() < offset + 7 {
|
||||
return quickcheck::TestResult::discard();
|
||||
}
|
||||
let out = array_mut_ref!(data, offset, 7);
|
||||
out[6] = 3;
|
||||
quickcheck::TestResult::from_bool(out.len() == 7)
|
||||
}
|
||||
quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn check_array_mut_ref_out_of_bounds_32() {
|
||||
fn f(mut data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
|
||||
if data.len() >= offset + 32 {
|
||||
return quickcheck::TestResult::discard();
|
||||
}
|
||||
quickcheck::TestResult::must_fail(move || {
|
||||
array_mut_ref!(data, offset, 32);
|
||||
})
|
||||
}
|
||||
quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_5_array_refs() {
|
||||
let mut data: [usize; 128] = [0; 128];
|
||||
for i in 0..128 {
|
||||
data[i] = i;
|
||||
}
|
||||
let data = data;
|
||||
let (a,b,c,d,e) = array_refs!(&data, 1, 14, 3, 100, 10);
|
||||
assert_eq!(a.len(), 1 as usize);
|
||||
assert_eq!(b.len(), 14 as usize);
|
||||
assert_eq!(c.len(), 3 as usize);
|
||||
assert_eq!(d.len(), 100 as usize);
|
||||
assert_eq!(e.len(), 10 as usize);
|
||||
assert_eq!(a, array_ref![data, 0, 1]);
|
||||
assert_eq!(b, array_ref![data, 1, 14]);
|
||||
assert_eq!(c, array_ref![data, 15, 3]);
|
||||
assert_eq!(e, array_ref![data, 118, 10]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_5_array_refs_dotdot() {
|
||||
let mut data: [usize; 128] = [0; 128];
|
||||
for i in 0..128 {
|
||||
data[i] = i;
|
||||
}
|
||||
let data = data;
|
||||
let (a,b,c,d,e) = array_refs!(&data, 1, 14, 3; ..; 10);
|
||||
assert_eq!(a.len(), 1 as usize);
|
||||
assert_eq!(b.len(), 14 as usize);
|
||||
assert_eq!(c.len(), 3 as usize);
|
||||
assert_eq!(d.len(), 100 as usize);
|
||||
assert_eq!(e.len(), 10 as usize);
|
||||
assert_eq!(a, array_ref![data, 0, 1]);
|
||||
assert_eq!(b, array_ref![data, 1, 14]);
|
||||
assert_eq!(c, array_ref![data, 15, 3]);
|
||||
assert_eq!(e, array_ref![data, 118, 10]);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_5_mut_xarray_refs() {
|
||||
let mut data: [usize; 128] = [0; 128];
|
||||
{
|
||||
// temporarily borrow the data to modify it.
|
||||
let (a,b,c,d,e) = mut_array_refs!(&mut data, 1, 14, 3, 100, 10);
|
||||
assert_eq!(a.len(), 1 as usize);
|
||||
assert_eq!(b.len(), 14 as usize);
|
||||
assert_eq!(c.len(), 3 as usize);
|
||||
assert_eq!(d.len(), 100 as usize);
|
||||
assert_eq!(e.len(), 10 as usize);
|
||||
*a = [1; 1];
|
||||
*b = [14; 14];
|
||||
*c = [3; 3];
|
||||
*d = [100; 100];
|
||||
*e = [10; 10];
|
||||
}
|
||||
assert_eq!(&[1;1], array_ref![data, 0, 1]);
|
||||
assert_eq!(&[14;14], array_ref![data, 1, 14]);
|
||||
assert_eq!(&[3;3], array_ref![data, 15, 3]);
|
||||
assert_eq!(&[10;10], array_ref![data, 118, 10]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_5_mut_xarray_refs_with_dotdot() {
|
||||
let mut data: [usize; 128] = [0; 128];
|
||||
{
|
||||
// temporarily borrow the data to modify it.
|
||||
let (a,b,c,d,e) = mut_array_refs!(&mut data, 1, 14, 3; ..; 10);
|
||||
assert_eq!(a.len(), 1 as usize);
|
||||
assert_eq!(b.len(), 14 as usize);
|
||||
assert_eq!(c.len(), 3 as usize);
|
||||
assert_eq!(d.len(), 100 as usize);
|
||||
assert_eq!(e.len(), 10 as usize);
|
||||
*a = [1; 1];
|
||||
*b = [14; 14];
|
||||
*c = [3; 3];
|
||||
*e = [10; 10];
|
||||
}
|
||||
assert_eq!(&[1;1], array_ref![data, 0, 1]);
|
||||
assert_eq!(&[14;14], array_ref![data, 1, 14]);
|
||||
assert_eq!(&[3;3], array_ref![data, 15, 3]);
|
||||
assert_eq!(&[10;10], array_ref![data, 118, 10]);
|
||||
}
|
||||
|
||||
#[forbid(clippy::ptr_offset_with_cast)]
|
||||
#[test]
|
||||
fn forbidden_clippy_lints_do_not_fire() {
|
||||
let mut data = [0u8; 32];
|
||||
let _ = array_refs![&data, 8; .. ;];
|
||||
let _ = mut_array_refs![&mut data, 8; .. ; 10];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_arg_refs() {
|
||||
let mut data = [0u8; 8];
|
||||
let (_, ) = array_refs![&data, 8];
|
||||
let (_, ) = mut_array_refs![&mut data, 8];
|
||||
|
||||
let (_, _) = array_refs![&data, 4; ..;];
|
||||
let (_, _) = mut_array_refs![&mut data, 4; ..;];
|
||||
|
||||
let (_, _) = array_refs![&data,; ..; 4];
|
||||
let (_, _) = mut_array_refs![&mut data,; ..; 4];
|
||||
|
||||
let (_,) = array_refs![&data,; ..;];
|
||||
let (_,) = mut_array_refs![&mut data,; ..;];
|
||||
}
|
||||
|
||||
} // mod test
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue