diff options
Diffstat (limited to 'third_party/rust/arrayref')
-rw-r--r-- | third_party/rust/arrayref/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/arrayref/Cargo.lock | 256 | ||||
-rw-r--r-- | third_party/rust/arrayref/Cargo.toml | 22 | ||||
-rw-r--r-- | third_party/rust/arrayref/LICENSE | 26 | ||||
-rw-r--r-- | third_party/rust/arrayref/README.md | 109 | ||||
-rw-r--r-- | third_party/rust/arrayref/examples/array_refs.rs | 13 | ||||
-rw-r--r-- | third_party/rust/arrayref/examples/array_refs_with_const.rs | 18 | ||||
-rw-r--r-- | third_party/rust/arrayref/examples/simple-case.rs | 15 | ||||
-rw-r--r-- | third_party/rust/arrayref/src/lib.rs | 476 |
9 files changed, 936 insertions, 0 deletions
diff --git a/third_party/rust/arrayref/.cargo-checksum.json b/third_party/rust/arrayref/.cargo-checksum.json new file mode 100644 index 0000000000..5d57087e27 --- /dev/null +++ b/third_party/rust/arrayref/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"a089c4b179e72a02fac211ae23ff35cbb93703e975733b94eba13ac4b52fe5c4","Cargo.toml":"ff0e13c99f02212cb939518302c5110c6c5383fe4ddd3da01cea2fd656f0feee","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":"193dcf7ba66294353b3173047606381f390552a94f1d0472bd9207273bd347c4"},"package":"a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"}
\ No newline at end of file diff --git a/third_party/rust/arrayref/Cargo.lock b/third_party/rust/arrayref/Cargo.lock new file mode 100644 index 0000000000..7b43782c24 --- /dev/null +++ b/third_party/rust/arrayref/Cargo.lock @@ -0,0 +1,256 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "arrayref" +version = "0.3.6" +dependencies = [ + "quickcheck 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "env_logger" +version = "0.5.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "humantime" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quick-error" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "quickcheck" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "redox_syscall" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "termcolor" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "wincolor 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ucd-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "utf8-ranges" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "version_check" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "wincolor" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c1c6d463cbe7ed28720b5b489e7c083eeb8f90d08be2a0d6bb9e1ffea9ce1afa" +"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" +"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" +"checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3" +"checksum env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)" = "f4d7e69c283751083d53d01eac767407343b8b69c4bd70058e08adc2637cb257" +"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +"checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e" +"checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7" +"checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" +"checksum log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "61bd98ae7f7b754bc53dca7d44b604f733c6bba044ea6f41bc8d89272d8161d2" +"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" +"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" +"checksum quickcheck 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c01babc5ffd48a2a83744b3024814bb46dfd4f2a4705ccb44b1b60e644fdcab7" +"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +"checksum regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5bbbea44c5490a1e84357ff28b7d518b4619a159fed5d25f6c1de2d19cc42814" +"checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" +"checksum termcolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "722426c4a0539da2c4ffd9b419d90ad540b4cff4a053be9069c908d4d07e2836" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" +"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" +"checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051" +"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum wincolor 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b9dc3aa9dcda98b5a16150c54619c1ead22e3d3a5d458778ae914be760aa981a" diff --git a/third_party/rust/arrayref/Cargo.toml b/third_party/rust/arrayref/Cargo.toml new file mode 100644 index 0000000000..8a96168639 --- /dev/null +++ b/third_party/rust/arrayref/Cargo.toml @@ -0,0 +1,22 @@ +# 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 believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "arrayref" +version = "0.3.6" +authors = ["David Roundy <roundyd@physics.oregonstate.edu>"] +description = "Macros to take array references of slices" +documentation = "https://docs.rs/arrayref" +license = "BSD-2-Clause" +repository = "https://github.com/droundy/arrayref" +[dev-dependencies.quickcheck] +version = "0.6" diff --git a/third_party/rust/arrayref/LICENSE b/third_party/rust/arrayref/LICENSE new file mode 100644 index 0000000000..b9eb43a86c --- /dev/null +++ b/third_party/rust/arrayref/LICENSE @@ -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. diff --git a/third_party/rust/arrayref/README.md b/third_party/rust/arrayref/README.md new file mode 100644 index 0000000000..10603a089f --- /dev/null +++ b/third_party/rust/arrayref/README.md @@ -0,0 +1,109 @@ +# arrayref + +[![Build Status](https://travis-ci.org/droundy/arrayref.svg?branch=master)](https://travis-ci.org/droundy/arrayref) +[![Coverage Status](https://coveralls.io/repos/droundy/arrayref/badge.svg?branch=master&service=github)](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 diff --git a/third_party/rust/arrayref/examples/array_refs.rs b/third_party/rust/arrayref/examples/array_refs.rs new file mode 100644 index 0000000000..a3ff081a11 --- /dev/null +++ b/third_party/rust/arrayref/examples/array_refs.rs @@ -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]); +} diff --git a/third_party/rust/arrayref/examples/array_refs_with_const.rs b/third_party/rust/arrayref/examples/array_refs_with_const.rs new file mode 100644 index 0000000000..734ef46270 --- /dev/null +++ b/third_party/rust/arrayref/examples/array_refs_with_const.rs @@ -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]); +} diff --git a/third_party/rust/arrayref/examples/simple-case.rs b/third_party/rust/arrayref/examples/simple-case.rs new file mode 100644 index 0000000000..434e253ae6 --- /dev/null +++ b/third_party/rust/arrayref/examples/simple-case.rs @@ -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); +} diff --git a/third_party/rust/arrayref/src/lib.rs b/third_party/rust/arrayref/src/lib.rs new file mode 100644 index 0000000000..b59b2fcd9c --- /dev/null +++ b/third_party/rust/arrayref/src/lib.rs @@ -0,0 +1,476 @@ +//! 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 std::slice; + #[inline] + #[allow(unused_assignments)] + #[allow(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.offset($pre as isize); + aref + } ),* , { + let sl = slice::from_raw_parts(p as *const T, var_len); + p = p.offset(var_len as isize); + sl + }, $( { + let aref = & *(p as *const [T; $post]); + p = p.offset($post as isize); + aref + } ),*) + } + let input = $arr; + #[allow(unused_unsafe)] + unsafe { + as_arrays(input) + } + } + }}; + ( $arr:expr, $( $len:expr ),* ) => {{ + { + #[inline] + #[allow(unused_assignments)] + #[allow(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 std::slice; + #[inline] + #[allow(unused_assignments)] + #[allow(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.offset($pre as isize); + aref + } ),* , { + let sl = slice::from_raw_parts_mut(p as *mut T, var_len); + p = p.offset(var_len as isize); + sl + }, $( { + let aref = &mut *(p as *mut [T; $post]); + p = p.offset($post as isize); + aref + } ),*) + } + let input = $arr; + #[allow(unused_unsafe)] + unsafe { + as_arrays(input) + } + } + }}; + ( $arr:expr, $( $len:expr ),* ) => {{ + { + #[inline] + #[allow(unused_assignments)] + #[allow(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.offset($len as isize); + 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) + } + } + }} +} + + +#[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]); +} + +} // mod test |