diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/arrayref | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
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.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 | 472 |
8 files changed, 676 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..31fc317632 --- /dev/null +++ b/third_party/rust/arrayref/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"69f44c00a5d11817c9c1f255d135332b0167c5b1ed498c09b1bcfae500db37b8","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":"5e16b06cc5f532a557876002923a58881500beda02e2e35bd46f3a547e8549b0"},"package":"0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee"}
\ No newline at end of file diff --git a/third_party/rust/arrayref/Cargo.toml b/third_party/rust/arrayref/Cargo.toml new file mode 100644 index 0000000000..deba7688bc --- /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.5" +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..fba08f3b7a --- /dev/null +++ b/third_party/rust/arrayref/src/lib.rs @@ -0,0 +1,472 @@ +//! 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)] + 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)] + 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)] + 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)] + 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 |