]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #50319 - nagisa:align_to, r=alexcrichton
authorbors <bors@rust-lang.org>
Fri, 18 May 2018 21:49:38 +0000 (21:49 +0000)
committerbors <bors@rust-lang.org>
Fri, 18 May 2018 21:49:38 +0000 (21:49 +0000)
Implement [T]::align_to

Note that this PR deviates from what is accepted by RFC slightly by making `align_offset` to return an offset in elements, rather than bytes. This is necessary to sanely support `[T]::align_to` and also simply makes more sense™. The caveat is that trying to align a pointer of ZST is now an equivalent to `is_aligned` check, rather than anything else (as no number of ZST elements will align a misaligned ZST pointer).

It also implements the `align_to` slightly differently than proposed in the RFC to properly handle cases where size of T and U aren’t co-prime.

Furthermore, a promise is made that the slice containing `U`s will be as large as possible (contrary to the RFC) – otherwise the function is quite useless.

The implementation uses quite a few underhanded tricks and takes advantage of the fact that alignment is a power-of-two quite heavily to optimise the machine code down to something that results in as few known-expensive instructions as possible. Currently calling `ptr.align_offset` with an unknown-at-compile-time `align` results in code that has just a single "expensive" modulo operation; the rest is "cheap" arithmetic and bitwise ops.

cc https://github.com/rust-lang/rust/issues/44488 @oli-obk

As mentioned in the commit message for align_offset, many thanks go to Chris McDonald.

1  2 
src/libcore/intrinsics.rs
src/libcore/ptr.rs
src/libcore/tests/lib.rs

Simple merge
Simple merge
index cd6b5c6a4ad4cf43c78d9573c2a9e7e33b51ba66,dbd26b2c7183673a4e1f0739e5c0afca0a73d3df..13189d532aba1c92708cca8a32fb92f86e25b090
  #![feature(try_from)]
  #![feature(try_trait)]
  #![feature(exact_chunks)]
+ #![feature(slice_align_to)]
+ #![feature(align_offset)]
  #![feature(reverse_bits)]
 -#![feature(inclusive_range_methods)]
  #![feature(iterator_find_map)]
  #![feature(slice_internals)]