rust/library/core/src
bors 5423745db8 Auto merge of #105871 - llogiq:option-as-slice, r=scottmcm
Add `Option::as_`(`mut_`)`slice`

This adds the following functions:

* `Option<T>::as_slice(&self) -> &[T]`
* `Option<T>::as_mut_slice(&mut self) -> &[T]`

The `as_slice` and `as_mut_slice_mut` functions benefit from an optimization that makes them completely branch-free. ~~Unfortunately, this optimization is not available on by-value Options, therefore the `into_slice` implementations use the plain `match` + `slice::from_ref` approach.~~

Note that the optimization's soundness hinges on the fact that either the niche optimization makes the offset of the `Some(_)` contents zero or the mempory layout of `Option<T>` is equal to that of `Option<MaybeUninit<T>>`.

The idea has been discussed on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Option.3A.3Aas_slice). Notably the idea for the `as_slice_mut` and `into_slice´ methods came from `@cuviper` and `@Sp00ph` hardened the optimization against niche-optimized Options.

The [rust playground](https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=74f8e4239a19f454c183aaf7b4a969e0) shows that the generated assembly of the optimized method is basically only a copy while the naive method generates code containing a `test dx, dx` on x86_64.

---

EDIT from reviewer: ACP is https://github.com/rust-lang/libs-team/issues/150
2023-03-01 12:32:57 +00:00
..
alloc Clarify new_size for realloc means bytes 2023-02-09 23:56:20 -08:00
array Auto merge of #107634 - scottmcm:array-drain, r=thomcc 2023-02-13 10:18:48 +00:00
async_iter use consistent terminology 2022-10-29 09:23:12 +02:00
cell Add OnceCell<T>: !Sync impl for diagnostics 2023-01-19 20:14:21 +01:00
char Auto merge of #105671 - lukas-code:depreciate-char, r=scottmcm 2023-02-12 11:09:06 +00:00
convert Remove from_fn lang item 2023-02-26 09:15:54 +00:00
ffi Document that CStr::as_ptr returns a type alias 2023-02-16 14:22:08 +00:00
fmt Auto merge of #106745 - m-ou-se:format-args-ast, r=oli-obk 2023-01-26 12:44:47 +00:00
future Remove GenFuture from core 2023-01-29 15:20:03 +01:00
hash Fix some ~const usage in libcore 2022-12-20 15:01:37 +00:00
intrinsics Auto merge of #107297 - Mark-Simulacrum:bump-bootstrap, r=pietroalbini 2023-01-31 19:24:29 +00:00
iter Rollup merge of #107890 - obeis:mapping-to-unit, r=cjgillot 2023-02-26 00:46:25 +01:00
macros Remove HTML tags around warning 2023-01-06 13:20:58 +01:00
mem stage-step cfgs 2023-01-30 13:09:09 -05:00
net Add tracking issue 2023-02-26 13:50:10 +01:00
num Require literals for some (u)int_impl! parameters 2023-02-22 23:26:22 -08:00
ops Constify RangeBounds, RangeX::contains and RangeX::is_empty. 2023-02-15 15:50:54 +01:00
panic Replace libstd, libcore, liballoc in line comments. 2022-12-30 14:00:42 +01:00
prelude Replace libstd, libcore, liballoc in docs. 2022-12-30 14:00:40 +01:00
ptr "Basic usage" is redundant for there is just one example 2023-02-14 01:43:41 +02:00
slice Optimize break patterns 2023-02-22 16:02:35 +00:00
str Use associated items of char instead of freestanding items in core::char 2023-01-14 11:58:41 +01:00
sync Rename atomic 'as_mut_ptr' to 'as_ptr' to match Cell (ref #66893) 2023-02-10 20:46:14 -05:00
task Inline Poll methods 2023-02-13 14:17:45 +08:00
unicode Replace libstd, libcore, liballoc in line comments. 2022-12-30 14:00:42 +01:00
any.rs Constify TypeId ordering impls 2023-01-16 21:26:03 +01:00
arch.rs move core::arch into separate file 2022-11-20 10:28:14 +01:00
ascii.rs Inline <EscapeDefault as Iterator>::next 2022-03-10 15:35:22 +01:00
asserting.rs [RFC 2011] Library code 2022-05-22 07:18:32 -03:00
bool.rs Add missing assertion 2022-09-22 02:12:06 -04:00
borrow.rs Minor grammar nit. 2022-12-12 16:22:01 -07:00
cell.rs impl DispatchFromDyn for Cell and UnsafeCell 2023-01-24 12:06:12 +01:00
clone.rs Make some trivial functions #[inline(always)] 2022-12-07 17:11:17 +01:00
cmp.rs Constify RangeBounds, RangeX::contains and RangeX::is_empty. 2023-02-15 15:50:54 +01:00
default.rs cfg-step code 2022-11-06 17:21:21 -05:00
error.md Small round of typo fixes 2022-11-04 20:06:18 -07:00
error.rs Remove a couple of #[doc(hidden)] pub fn and their #[feature] gates 2023-02-10 08:06:35 +01:00
hint.rs clarify correctness of black_box 2023-02-15 16:22:08 +00:00
internal_macros.rs ignore a doctest for the non-exported macro 2022-05-03 18:33:56 +09:00
intrinsics.rs stage-step cfgs 2023-01-30 13:09:09 -05:00
lib.rs Auto merge of #105871 - llogiq:option-as-slice, r=scottmcm 2023-03-01 12:32:57 +00:00
marker.rs add a #[rustc_coinductive] attribute 2023-02-14 11:53:22 +01:00
option.rs Add Option::as_slice(_mut) 2023-03-01 00:05:31 +01:00
panic.rs Replace libstd, libcore, liballoc in docs. 2022-12-30 14:00:40 +01:00
panicking.rs stage-step cfgs 2023-01-30 13:09:09 -05:00
pin.rs Set version placeholders to 1.68 2023-01-25 09:44:29 -05:00
primitive.rs mv std libs to library/ 2020-07-27 19:51:13 -05:00
primitive_docs.rs Clarify that Copy is a trait in array docs 2023-02-27 15:16:39 +00:00
result.rs "Basic usage" is redundant for there is just one example 2023-02-16 19:49:31 +02:00
time.rs Bump version placeholders to release 2022-11-06 17:11:02 -05:00
tuple.rs const Compare Tuples 2022-11-09 09:52:04 +01:00
unit.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00