gecko-dev/third_party/rust/arrayref
Bastien Orivel faf6cad78c Bug 1580908 - Part 10: Revendor dependencies. r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D45719

--HG--
rename : third_party/rust/regex-0.2.2/src/freqs.rs => third_party/rust/aho-corasick/src/byte_frequencies.rs
rename : third_party/rust/crc/Cargo.toml => third_party/rust/blake2b_simd/Cargo.toml
rename : third_party/rust/miniz_oxide_c_api/LICENSE => third_party/rust/miniz_oxide/LICENSE
rename : third_party/rust/redox_users/tests/group => third_party/rust/redox_users/tests/etc/group
rename : third_party/rust/redox_users/tests/passwd => third_party/rust/redox_users/tests/etc/passwd
rename : third_party/rust/redox_users/tests/shadow => third_party/rust/redox_users/tests/etc/shadow
rename : third_party/rust/utf8-ranges/src/lib.rs => third_party/rust/regex-syntax/src/utf8.rs
rename : third_party/rust/crossbeam-channel/LICENSE-APACHE => third_party/rust/rust-argon2/LICENSE-APACHE
rename : third_party/rust/memchr-1.0.2/COPYING => third_party/rust/winapi-util/COPYING
rename : third_party/rust/ucd-util/Cargo.toml => third_party/rust/winapi-util/Cargo.toml
rename : third_party/rust/memchr-1.0.2/LICENSE-MIT => third_party/rust/winapi-util/LICENSE-MIT
rename : third_party/rust/memchr-1.0.2/UNLICENSE => third_party/rust/winapi-util/UNLICENSE
extra : moz-landing-system : lando
2019-09-12 21:46:32 +00:00
..
examples
src Bug 1580908 - Part 10: Revendor dependencies. r=froydnj 2019-09-12 21:46:32 +00:00
.cargo-checksum.json Bug 1580908 - Part 10: Revendor dependencies. r=froydnj 2019-09-12 21:46:32 +00:00
Cargo.toml Bug 1580908 - Part 10: Revendor dependencies. r=froydnj 2019-09-12 21:46:32 +00:00
LICENSE
README.md

README.md

arrayref

Build Status Coverage Status

Documentation

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:

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

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:

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 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.

The arrayref crate allows you to do this kind of slicing. So the above (very contrived) example can be implemented with array references as:

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.

For an example of how these macros can be used in an actual program, see my rust translation of tweetnacl, 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.