diff options
| author | bors <bors@rust-lang.org> | 2019-07-26 16:57:54 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-07-26 16:57:54 +0000 |
| commit | c43753f910aae000f8bcb0a502407ea332afc74b (patch) | |
| tree | ad563761b27efd2cbab0ade95c5139aaccb93494 /src/libcore | |
| parent | 1a563362865e6051d4c350544131228e8eff5138 (diff) | |
| parent | 232d27c306d76d2f973c88b0e0d1883aac8717f4 (diff) | |
| download | rust-c43753f910aae000f8bcb0a502407ea332afc74b.tar.gz rust-c43753f910aae000f8bcb0a502407ea332afc74b.zip | |
Auto merge of #63015 - Centril:rollup-ydhpcas, r=Centril
Rollup of 22 pull requests Successful merges: - #62084 (allow clippy::unreadable_literal in unicode tables) - #62120 (Add missing type links in documentation) - #62310 (Add missing doc links in boxed module) - #62421 (Introduce `as_deref` to Option) - #62583 (Implement Unpin for all raw pointers) - #62692 (rustc: precompute the largest Niche and store it in LayoutDetails.) - #62801 (Remove support for -Zlower-128bit-ops) - #62828 (Remove vector fadd/fmul reduction workarounds) - #62862 (code cleanup) - #62904 (Disable d32 on armv6 hf targets) - #62907 (Initialize the MSP430 AsmParser) - #62956 (Implement slow-path for FirstSets::first) - #62963 (Allow lexer to recover from some homoglyphs) - #62964 (clarify and unify some type test names) - #62970 (ci: gate toolstate repo pushes on the TOOLSTATE_PUBLISH envvar) - #62980 (std: Add more accessors for `Metadata` on Windows) - #62983 (Remove needless indirection through Rc) - #62985 (librustc_errors: Support ui-testing flag in annotate-snippet emitter) - #63002 (error_index_generator should output stdout/stderr when it panics.) - #63004 (Add test for issue-54062) - #63007 (ci: debug network failures while downloading awscli from PyPI) - #63009 (Remove redundant `mut` from variable declaration.) Failed merges: r? @ghost
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cmp.rs | 2 | ||||
| -rw-r--r-- | src/libcore/marker.rs | 6 | ||||
| -rw-r--r-- | src/libcore/option.rs | 17 | ||||
| -rw-r--r-- | src/libcore/pin.rs | 144 | ||||
| -rw-r--r-- | src/libcore/result.rs | 65 | ||||
| -rw-r--r-- | src/libcore/tests/option.rs | 30 | ||||
| -rw-r--r-- | src/libcore/tests/result.rs | 185 | ||||
| -rw-r--r-- | src/libcore/unicode/tables.rs | 2 | ||||
| -rwxr-xr-x | src/libcore/unicode/unicode.py | 2 |
9 files changed, 331 insertions, 122 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 59088e43291..f9613556a1e 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -319,7 +319,7 @@ impl Ordering { /// This method can be used to reverse a comparison: /// /// ``` - /// let mut data: &mut [_] = &mut [2, 10, 5, 8]; + /// let data: &mut [_] = &mut [2, 10, 5, 8]; /// /// // sort the array from largest to smallest. /// data.sort_by(|a, b| a.cmp(b).reverse()); diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 39c390b4df6..79a188dbac9 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -655,6 +655,12 @@ impl<'a, T: ?Sized + 'a> Unpin for &'a T {} #[stable(feature = "pin", since = "1.33.0")] impl<'a, T: ?Sized + 'a> Unpin for &'a mut T {} +#[stable(feature = "pin_raw", since = "1.38.0")] +impl<T: ?Sized> Unpin for *const T {} + +#[stable(feature = "pin_raw", since = "1.38.0")] +impl<T: ?Sized> Unpin for *mut T {} + /// Implementations of `Copy` for primitive types. /// /// Implementations that cannot be described in Rust diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 33d6afdc975..abc8883d398 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -136,7 +136,7 @@ #![stable(feature = "rust1", since = "1.0.0")] use crate::iter::{FromIterator, FusedIterator, TrustedLen}; -use crate::{convert, fmt, hint, mem, ops::{self, Deref}}; +use crate::{convert, fmt, hint, mem, ops::{self, Deref, DerefMut}}; use crate::pin::Pin; // Note that this is not a lang item per se, but it has a hidden dependency on @@ -1104,17 +1104,28 @@ impl<T: Default> Option<T> { #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] impl<T: Deref> Option<T> { - /// Converts from `&Option<T>` to `Option<&T::Target>`. + /// Converts from `Option<T>` (or `&Option<T>`) to `Option<&T::Target>`. /// /// Leaves the original Option in-place, creating a new one with a reference /// to the original one, additionally coercing the contents via [`Deref`]. /// /// [`Deref`]: ../../std/ops/trait.Deref.html - pub fn deref(&self) -> Option<&T::Target> { + pub fn as_deref(&self) -> Option<&T::Target> { self.as_ref().map(|t| t.deref()) } } +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] +impl<T: DerefMut> Option<T> { + /// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`. + /// + /// Leaves the original `Option` in-place, creating a new one containing a mutable reference to + /// the inner type's `Deref::Target` type. + pub fn as_deref_mut(&mut self) -> Option<&mut T::Target> { + self.as_mut().map(|t| t.deref_mut()) + } +} + impl<T, E> Option<Result<T, E>> { /// Transposes an `Option` of a [`Result`] into a [`Result`] of an `Option`. /// diff --git a/src/libcore/pin.rs b/src/libcore/pin.rs index c063cee5227..2feaab7a09c 100644 --- a/src/libcore/pin.rs +++ b/src/libcore/pin.rs @@ -11,13 +11,13 @@ //! until it gets dropped. We say that the pointee is "pinned". //! //! By default, all types in Rust are movable. Rust allows passing all types by-value, -//! and common smart-pointer types such as `Box<T>` and `&mut T` allow replacing and -//! moving the values they contain: you can move out of a `Box<T>`, or you can use [`mem::swap`]. -//! [`Pin<P>`] wraps a pointer type `P`, so `Pin<Box<T>>` functions much like a regular `Box<T>`: -//! when a `Pin<Box<T>>` gets dropped, so do its contents, and the memory gets deallocated. -//! Similarly, `Pin<&mut T>` is a lot like `&mut T`. However, [`Pin<P>`] does not let clients -//! actually obtain a `Box<T>` or `&mut T` to pinned data, which implies that you cannot use -//! operations such as [`mem::swap`]: +//! and common smart-pointer types such as [`Box<T>`] and `&mut T` allow replacing and +//! moving the values they contain: you can move out of a [`Box<T>`], or you can use [`mem::swap`]. +//! [`Pin<P>`] wraps a pointer type `P`, so [`Pin`]`<`[`Box`]`<T>>` functions much like a regular +//! [`Box<T>`]: when a [`Pin`]`<`[`Box`]`<T>>` gets dropped, so do its contents, and the memory gets +//! deallocated. Similarly, [`Pin`]`<&mut T>` is a lot like `&mut T`. However, [`Pin<P>`] does +//! not let clients actually obtain a [`Box<T>`] or `&mut T` to pinned data, which implies that you +//! cannot use operations such as [`mem::swap`]: //! //! ``` //! use std::pin::Pin; @@ -30,15 +30,15 @@ //! ``` //! //! It is worth reiterating that [`Pin<P>`] does *not* change the fact that a Rust compiler -//! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, `Pin<P>` -//! prevents certain *values* (pointed to by pointers wrapped in `Pin<P>`) from being +//! considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, [`Pin<P>`] +//! prevents certain *values* (pointed to by pointers wrapped in [`Pin<P>`]) from being //! moved by making it impossible to call methods that require `&mut T` on them //! (like [`mem::swap`]). //! //! [`Pin<P>`] can be used to wrap any pointer type `P`, and as such it interacts with -//! [`Deref`] and [`DerefMut`]. A `Pin<P>` where `P: Deref` should be considered -//! as a "`P`-style pointer" to a pinned `P::Target` -- so, a `Pin<Box<T>>` is -//! an owned pointer to a pinned `T`, and a `Pin<Rc<T>>` is a reference-counted +//! [`Deref`] and [`DerefMut`]. A [`Pin<P>`] where `P: Deref` should be considered +//! as a "`P`-style pointer" to a pinned `P::Target` -- so, a [`Pin`]`<`[`Box`]`<T>>` is +//! an owned pointer to a pinned `T`, and a [`Pin`]`<`[`Rc`]`<T>>` is a reference-counted //! pointer to a pinned `T`. //! For correctness, [`Pin<P>`] relies on the implementations of [`Deref`] and //! [`DerefMut`] not to move out of their `self` parameter, and only ever to @@ -48,15 +48,15 @@ //! //! Many types are always freely movable, even when pinned, because they do not //! rely on having a stable address. This includes all the basic types (like -//! `bool`, `i32`, and references) as well as types consisting solely of these +//! [`bool`], [`i32`], and references) as well as types consisting solely of these //! types. Types that do not care about pinning implement the [`Unpin`] //! auto-trait, which cancels the effect of [`Pin<P>`]. For `T: Unpin`, -//! `Pin<Box<T>>` and `Box<T>` function identically, as do `Pin<&mut T>` and +//! [`Pin`]`<`[`Box`]`<T>>` and [`Box<T>`] function identically, as do [`Pin`]`<&mut T>` and //! `&mut T`. //! -//! Note that pinning and `Unpin` only affect the pointed-to type `P::Target`, not the pointer -//! type `P` itself that got wrapped in `Pin<P>`. For example, whether or not `Box<T>` is -//! `Unpin` has no effect on the behavior of `Pin<Box<T>>` (here, `T` is the +//! Note that pinning and [`Unpin`] only affect the pointed-to type `P::Target`, not the pointer +//! type `P` itself that got wrapped in [`Pin<P>`]. For example, whether or not [`Box<T>`] is +//! [`Unpin`] has no effect on the behavior of [`Pin`]`<`[`Box`]`<T>>` (here, `T` is the //! pointed-to type). //! //! # Example: self-referential struct @@ -122,15 +122,15 @@ //! //! To make this work, every element has pointers to its predecessor and successor in //! the list. Elements can only be added when they are pinned, because moving the elements -//! around would invalidate the pointers. Moreover, the `Drop` implementation of a linked +//! around would invalidate the pointers. Moreover, the [`Drop`] implementation of a linked //! list element will patch the pointers of its predecessor and successor to remove itself //! from the list. //! -//! Crucially, we have to be able to rely on `drop` being called. If an element -//! could be deallocated or otherwise invalidated without calling `drop`, the pointers into it +//! Crucially, we have to be able to rely on [`drop`] being called. If an element +//! could be deallocated or otherwise invalidated without calling [`drop`], the pointers into it //! from its neighbouring elements would become invalid, which would break the data structure. //! -//! Therefore, pinning also comes with a `drop`-related guarantee. +//! Therefore, pinning also comes with a [`drop`]-related guarantee. //! //! # `Drop` guarantee //! @@ -139,7 +139,7 @@ //! otherwise invalidating the memory used to store the data is restricted, too. //! Concretely, for pinned data you have to maintain the invariant //! that *its memory will not get invalidated or repurposed from the moment it gets pinned until -//! when `drop` is called*. Memory can be invalidated by deallocation, but also by +//! when [`drop`] is called*. Memory can be invalidated by deallocation, but also by //! replacing a [`Some(v)`] by [`None`], or calling [`Vec::set_len`] to "kill" some elements //! off of a vector. It can be repurposed by using [`ptr::write`] to overwrite it without //! calling the destructor first. @@ -148,26 +148,27 @@ //! section needs to function correctly. //! //! Notice that this guarantee does *not* mean that memory does not leak! It is still -//! completely okay not ever to call `drop` on a pinned element (e.g., you can still -//! call [`mem::forget`] on a `Pin<Box<T>>`). In the example of the doubly-linked +//! completely okay not ever to call [`drop`] on a pinned element (e.g., you can still +//! call [`mem::forget`] on a [`Pin`]`<`[`Box`]`<T>>`). In the example of the doubly-linked //! list, that element would just stay in the list. However you may not free or reuse the storage -//! *without calling `drop`*. +//! *without calling [`drop`]*. //! //! # `Drop` implementation //! //! If your type uses pinning (such as the two examples above), you have to be careful -//! when implementing `Drop`. The `drop` function takes `&mut self`, but this +//! when implementing [`Drop`]. The [`drop`] function takes `&mut self`, but this //! is called *even if your type was previously pinned*! It is as if the -//! compiler automatically called `get_unchecked_mut`. +//! compiler automatically called [`Pin::get_unchecked_mut`]. //! //! This can never cause a problem in safe code because implementing a type that //! relies on pinning requires unsafe code, but be aware that deciding to make //! use of pinning in your type (for example by implementing some operation on -//! `Pin<&Self>` or `Pin<&mut Self>`) has consequences for your `Drop` +//! [`Pin`]`<&Self>` or [`Pin`]`<&mut Self>`) has consequences for your [`Drop`] //! implementation as well: if an element of your type could have been pinned, -//! you must treat Drop as implicitly taking `Pin<&mut Self>`. +//! you must treat [`Drop`] as implicitly taking [`Pin`]`<&mut Self>`. //! //! For example, you could implement `Drop` as follows: +//! //! ```rust,no_run //! # use std::pin::Pin; //! # struct Type { } @@ -182,7 +183,8 @@ //! } //! } //! ``` -//! The function `inner_drop` has the type that `drop` *should* have, so this makes sure that +//! +//! The function `inner_drop` has the type that [`drop`] *should* have, so this makes sure that //! you do not accidentally use `self`/`this` in a way that is in conflict with pinning. //! //! Moreover, if your type is `#[repr(packed)]`, the compiler will automatically @@ -192,10 +194,10 @@ //! # Projections and Structural Pinning //! //! When working with pinned structs, the question arises how one can access the -//! fields of that struct in a method that takes just `Pin<&mut Struct>`. +//! fields of that struct in a method that takes just [`Pin`]`<&mut Struct>`. //! The usual approach is to write helper methods (so called *projections*) -//! that turn `Pin<&mut Struct>` into a reference to the field, but what -//! type should that reference have? Is it `Pin<&mut Field>` or `&mut Field`? +//! that turn [`Pin`]`<&mut Struct>` into a reference to the field, but what +//! type should that reference have? Is it [`Pin`]`<&mut Field>` or `&mut Field`? //! The same question arises with the fields of an `enum`, and also when considering //! container/wrapper types such as [`Vec<T>`], [`Box<T>`], or [`RefCell<T>`]. //! (This question applies to both mutable and shared references, we just @@ -203,7 +205,7 @@ //! //! It turns out that it is actually up to the author of the data structure //! to decide whether the pinned projection for a particular field turns -//! `Pin<&mut Struct>` into `Pin<&mut Field>` or `&mut Field`. There are some +//! [`Pin`]`<&mut Struct>` into [`Pin`]`<&mut Field>` or `&mut Field`. There are some //! constraints though, and the most important constraint is *consistency*: //! every field can be *either* projected to a pinned reference, *or* have //! pinning removed as part of the projection. If both are done for the same field, @@ -218,12 +220,13 @@ //! ## Pinning *is not* structural for `field` //! //! It may seem counter-intuitive that the field of a pinned struct might not be pinned, -//! but that is actually the easiest choice: if a `Pin<&mut Field>` is never created, +//! but that is actually the easiest choice: if a [`Pin`]`<&mut Field>` is never created, //! nothing can go wrong! So, if you decide that some field does not have structural pinning, //! all you have to ensure is that you never create a pinned reference to that field. //! //! Fields without structural pinning may have a projection method that turns -//! `Pin<&mut Struct>` into `&mut Field`: +//! [`Pin`]`<&mut Struct>` into `&mut Field`: +//! //! ```rust,no_run //! # use std::pin::Pin; //! # type Field = i32; @@ -237,16 +240,17 @@ //! ``` //! //! You may also `impl Unpin for Struct` *even if* the type of `field` -//! is not `Unpin`. What that type thinks about pinning is not relevant -//! when no `Pin<&mut Field>` is ever created. +//! is not [`Unpin`]. What that type thinks about pinning is not relevant +//! when no [`Pin`]`<&mut Field>` is ever created. //! //! ## Pinning *is* structural for `field` //! //! The other option is to decide that pinning is "structural" for `field`, //! meaning that if the struct is pinned then so is the field. //! -//! This allows writing a projection that creates a `Pin<&mut Field>`, thus +//! This allows writing a projection that creates a [`Pin`]`<&mut Field>`, thus //! witnessing that the field is pinned: +//! //! ```rust,no_run //! # use std::pin::Pin; //! # type Field = i32; @@ -262,30 +266,30 @@ //! However, structural pinning comes with a few extra requirements: //! //! 1. The struct must only be [`Unpin`] if all the structural fields are -//! `Unpin`. This is the default, but `Unpin` is a safe trait, so as the author of +//! [`Unpin`]. This is the default, but [`Unpin`] is a safe trait, so as the author of //! the struct it is your responsibility *not* to add something like //! `impl<T> Unpin for Struct<T>`. (Notice that adding a projection operation -//! requires unsafe code, so the fact that `Unpin` is a safe trait does not break +//! requires unsafe code, so the fact that [`Unpin`] is a safe trait does not break //! the principle that you only have to worry about any of this if you use `unsafe`.) //! 2. The destructor of the struct must not move structural fields out of its argument. This //! is the exact point that was raised in the [previous section][drop-impl]: `drop` takes //! `&mut self`, but the struct (and hence its fields) might have been pinned before. -//! You have to guarantee that you do not move a field inside your `Drop` implementation. +//! You have to guarantee that you do not move a field inside your [`Drop`] implementation. //! In particular, as explained previously, this means that your struct must *not* //! be `#[repr(packed)]`. -//! See that section for how to write `drop` in a way that the compiler can help you +//! See that section for how to write [`drop`] in a way that the compiler can help you //! not accidentally break pinning. //! 3. You must make sure that you uphold the [`Drop` guarantee][drop-guarantee]: //! once your struct is pinned, the memory that contains the //! content is not overwritten or deallocated without calling the content's destructors. -//! This can be tricky, as witnessed by [`VecDeque<T>`]: the destructor of `VecDeque<T>` -//! can fail to call `drop` on all elements if one of the destructors panics. This violates the -//! `Drop` guarantee, because it can lead to elements being deallocated without -//! their destructor being called. (`VecDeque` has no pinning projections, so this +//! This can be tricky, as witnessed by [`VecDeque<T>`]: the destructor of [`VecDeque<T>`] +//! can fail to call [`drop`] on all elements if one of the destructors panics. This violates +//! the [`Drop`] guarantee, because it can lead to elements being deallocated without +//! their destructor being called. ([`VecDeque<T>`] has no pinning projections, so this //! does not cause unsoundness.) //! 4. You must not offer any other operations that could lead to data being moved out of //! the structural fields when your type is pinned. For example, if the struct contains an -//! `Option<T>` and there is a `take`-like operation with type +//! [`Option<T>`] and there is a `take`-like operation with type //! `fn(Pin<&mut Struct<T>>) -> Option<T>`, //! that operation can be used to move a `T` out of a pinned `Struct<T>` -- which means //! pinning cannot be structural for the field holding this data. @@ -301,37 +305,39 @@ //! let content = &mut *b; // And here we have `&mut T` to the same data. //! } //! ``` -//! This is catastrophic, it means we can first pin the content of the `RefCell<T>` +//! This is catastrophic, it means we can first pin the content of the [`RefCell<T>`] //! (using `RefCell::get_pin_mut`) and then move that content using the mutable //! reference we got later. //! //! ## Examples //! //! For a type like [`Vec<T>`], both possibilites (structural pinning or not) make sense. -//! A `Vec<T>` with structural pinning could have `get_pin`/`get_pin_mut` methods to get +//! A [`Vec<T>`] with structural pinning could have `get_pin`/`get_pin_mut` methods to get //! pinned references to elements. However, it could *not* allow calling -//! `pop` on a pinned `Vec<T>` because that would move the (structurally pinned) contents! -//! Nor could it allow `push`, which might reallocate and thus also move the contents. -//! A `Vec<T>` without structural pinning could `impl<T> Unpin for Vec<T>`, because the contents -//! are never pinned and the `Vec<T>` itself is fine with being moved as well. +//! [`pop`][Vec::pop] on a pinned [`Vec<T>`] because that would move the (structurally pinned) +//! contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also move the +//! contents. +//! +//! A [`Vec<T>`] without structural pinning could `impl<T> Unpin for Vec<T>`, because the contents +//! are never pinned and the [`Vec<T>`] itself is fine with being moved as well. //! At that point pinning just has no effect on the vector at all. //! //! In the standard library, pointer types generally do not have structural pinning, //! and thus they do not offer pinning projections. This is why `Box<T>: Unpin` holds for all `T`. //! It makes sense to do this for pointer types, because moving the `Box<T>` -//! does not actually move the `T`: the `Box<T>` can be freely movable (aka `Unpin`) even if the `T` -//! is not. In fact, even `Pin<Box<T>>` and `Pin<&mut T>` are always `Unpin` themselves, -//! for the same reason: their contents (the `T`) are pinned, but the pointers themselves -//! can be moved without moving the pinned data. For both `Box<T>` and `Pin<Box<T>>`, -//! whether the content is pinned is entirely independent of whether the pointer is -//! pinned, meaning pinning is *not* structural. +//! does not actually move the `T`: the [`Box<T>`] can be freely movable (aka `Unpin`) even if +//! the `T` is not. In fact, even [`Pin`]`<`[`Box`]`<T>>` and [`Pin`]`<&mut T>` are always +//! [`Unpin`] themselves, for the same reason: their contents (the `T`) are pinned, but the +//! pointers themselves can be moved without moving the pinned data. For both [`Box<T>`] and +//! [`Pin`]`<`[`Box`]`<T>>`, whether the content is pinned is entirely independent of whether the +//! pointer is pinned, meaning pinning is *not* structural. //! //! When implementing a [`Future`] combinator, you will usually need structural pinning -//! for the nested futures, as you need to get pinned references to them to call `poll`. +//! for the nested futures, as you need to get pinned references to them to call [`poll`]. //! But if your combinator contains any other data that does not need to be pinned, //! you can make those fields not structural and hence freely access them with a -//! mutable reference even when you just have `Pin<&mut Self>` (such as in your own -//! `poll` implementation). +//! mutable reference even when you just have [`Pin`]`<&mut Self>` (such as in your own +//! [`poll`] implementation). //! //! [`Pin<P>`]: struct.Pin.html //! [`Unpin`]: ../marker/trait.Unpin.html @@ -342,6 +348,16 @@ //! [`Box<T>`]: ../../std/boxed/struct.Box.html //! [`Vec<T>`]: ../../std/vec/struct.Vec.html //! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len +//! [`Pin`]: struct.Pin.html +//! [`Box`]: ../../std/boxed/struct.Box.html +//! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop +//! [Vec::push]: ../../std/vec/struct.Vec.html#method.push +//! [`Rc`]: ../../std/rc/struct.Rc.html +//! [`RefCell<T>`]: ../../std/cell/struct.RefCell.html +//! [`Drop`]: ../../std/ops/trait.Drop.html +//! [`drop`]: ../../std/ops/trait.Drop.html#tymethod.drop +//! [`VecDeque<T>`]: ../../std/collections/struct.VecDeque.html +//! [`Option<T>`]: ../../std/option/enum.Option.html //! [`VecDeque<T>`]: ../../std/collections/struct.VecDeque.html //! [`RefCell<T>`]: ../cell/struct.RefCell.html //! [`None`]: ../option/enum.Option.html#variant.None @@ -350,6 +366,8 @@ //! [`Future`]: ../future/trait.Future.html //! [drop-impl]: #drop-implementation //! [drop-guarantee]: #drop-guarantee +//! [`poll`]: ../../std/future/trait.Future.html#tymethod.poll +//! [`Pin::get_unchecked_mut`]: struct.Pin.html#method.get_unchecked_mut #![stable(feature = "pin", since = "1.33.0")] diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 3a38b66ad01..cb6bc058730 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -232,7 +232,7 @@ use crate::fmt; use crate::iter::{FromIterator, FusedIterator, TrustedLen}; -use crate::ops::{self, Deref}; +use crate::ops::{self, Deref, DerefMut}; /// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]). /// @@ -981,24 +981,22 @@ impl<T: Default, E> Result<T, E> { #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] impl<T: Deref, E> Result<T, E> { - /// Converts from `&Result<T, E>` to `Result<&T::Target, &E>`. + /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`. /// - /// Leaves the original Result in-place, creating a new one with a reference - /// to the original one, additionally coercing the `Ok` arm of the Result via - /// `Deref`. - pub fn deref_ok(&self) -> Result<&T::Target, &E> { + /// Leaves the original `Result` in-place, creating a new one containing a reference to the + /// `Ok` type's `Deref::Target` type. + pub fn as_deref_ok(&self) -> Result<&T::Target, &E> { self.as_ref().map(|t| t.deref()) } } #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] impl<T, E: Deref> Result<T, E> { - /// Converts from `&Result<T, E>` to `Result<&T, &E::Target>`. + /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T, &E::Target>`. /// - /// Leaves the original Result in-place, creating a new one with a reference - /// to the original one, additionally coercing the `Err` arm of the Result via - /// `Deref`. - pub fn deref_err(&self) -> Result<&T, &E::Target> + /// Leaves the original `Result` in-place, creating a new one containing a reference to the + /// `Err` type's `Deref::Target` type. + pub fn as_deref_err(&self) -> Result<&T, &E::Target> { self.as_ref().map_err(|e| e.deref()) } @@ -1006,17 +1004,52 @@ impl<T, E: Deref> Result<T, E> { #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] impl<T: Deref, E: Deref> Result<T, E> { - /// Converts from `&Result<T, E>` to `Result<&T::Target, &E::Target>`. + /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E::Target>`. /// - /// Leaves the original Result in-place, creating a new one with a reference - /// to the original one, additionally coercing both the `Ok` and `Err` arms - /// of the Result via `Deref`. - pub fn deref(&self) -> Result<&T::Target, &E::Target> + /// Leaves the original `Result` in-place, creating a new one containing a reference to both + /// the `Ok` and `Err` types' `Deref::Target` types. + pub fn as_deref(&self) -> Result<&T::Target, &E::Target> { self.as_ref().map(|t| t.deref()).map_err(|e| e.deref()) } } +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] +impl<T: DerefMut, E> Result<T, E> { + /// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T::Target, &mut E>`. + /// + /// Leaves the original `Result` in-place, creating a new one containing a mutable reference to + /// the `Ok` type's `Deref::Target` type. + pub fn as_deref_mut_ok(&mut self) -> Result<&mut T::Target, &mut E> { + self.as_mut().map(|t| t.deref_mut()) + } +} + +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] +impl<T, E: DerefMut> Result<T, E> { + /// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T, &mut E::Target>`. + /// + /// Leaves the original `Result` in-place, creating a new one containing a mutable reference to + /// the `Err` type's `Deref::Target` type. + pub fn as_deref_mut_err(&mut self) -> Result<&mut T, &mut E::Target> + { + self.as_mut().map_err(|e| e.deref_mut()) + } +} + +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] +impl<T: DerefMut, E: DerefMut> Result<T, E> { + /// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to + /// `Result<&mut T::Target, &mut E::Target>`. + /// + /// Leaves the original `Result` in-place, creating a new one containing a mutable reference to + /// both the `Ok` and `Err` types' `Deref::Target` types. + pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E::Target> + { + self.as_mut().map(|t| t.deref_mut()).map_err(|e| e.deref_mut()) + } +} + impl<T, E> Result<Option<T>, E> { /// Transposes a `Result` of an `Option` into an `Option` of a `Result`. /// diff --git a/src/libcore/tests/option.rs b/src/libcore/tests/option.rs index b059b134868..ff43fc49f71 100644 --- a/src/libcore/tests/option.rs +++ b/src/libcore/tests/option.rs @@ -1,6 +1,8 @@ use core::option::*; use core::mem; use core::clone::Clone; +use core::array::FixedSizeArray; +use core::ops::DerefMut; #[test] fn test_get_ptr() { @@ -310,20 +312,38 @@ fn test_try() { } #[test] -fn test_option_deref() { +fn test_option_as_deref() { // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T) let ref_option = &Some(&42); - assert_eq!(ref_option.deref(), Some(&42)); + assert_eq!(ref_option.as_deref(), Some(&42)); let ref_option = &Some(String::from("a result")); - assert_eq!(ref_option.deref(), Some("a result")); + assert_eq!(ref_option.as_deref(), Some("a result")); let ref_option = &Some(vec![1, 2, 3, 4, 5]); - assert_eq!(ref_option.deref(), Some(&[1, 2, 3, 4, 5][..])); + assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice())); // None: &Option<T: Deref>>::None -> None let ref_option: &Option<&i32> = &None; - assert_eq!(ref_option.deref(), None); + assert_eq!(ref_option.as_deref(), None); +} + +#[test] +fn test_option_as_deref_mut() { + // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T) + let mut val = 42; + let ref_option = &mut Some(&mut val); + assert_eq!(ref_option.as_deref_mut(), Some(&mut 42)); + + let ref_option = &mut Some(String::from("a result")); + assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut())); + + let ref_option = &mut Some(vec![1, 2, 3, 4, 5]); + assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice())); + + // None: &mut Option<T: Deref>>::None -> None + let ref_option: &mut Option<&mut i32> = &mut None; + assert_eq!(ref_option.as_deref_mut(), None); } #[test] diff --git a/src/libcore/tests/result.rs b/src/libcore/tests/result.rs index 1fab07526a0..163f8d0ab37 100644 --- a/src/libcore/tests/result.rs +++ b/src/libcore/tests/result.rs @@ -1,4 +1,6 @@ use core::option::*; +use core::array::FixedSizeArray; +use core::ops::DerefMut; fn op1() -> Result<isize, &'static str> { Ok(666) } fn op2() -> Result<isize, &'static str> { Err("sadface") } @@ -225,94 +227,213 @@ fn test_try() { } #[test] -fn test_result_deref() { - // &Result<T: Deref, E>::Ok(T).deref_ok() -> +fn test_result_as_deref() { + // &Result<T: Deref, E>::Ok(T).as_deref_ok() -> // Result<&T::Deref::Target, &E>::Ok(&*T) let ref_ok = &Result::Ok::<&i32, u8>(&42); let expected_result = Result::Ok::<&i32, &u8>(&42); - assert_eq!(ref_ok.deref_ok(), expected_result); + assert_eq!(ref_ok.as_deref_ok(), expected_result); let ref_ok = &Result::Ok::<String, u32>(String::from("a result")); let expected_result = Result::Ok::<&str, &u32>("a result"); - assert_eq!(ref_ok.deref_ok(), expected_result); + assert_eq!(ref_ok.as_deref_ok(), expected_result); let ref_ok = &Result::Ok::<Vec<i32>, u32>(vec![1, 2, 3, 4, 5]); - let expected_result = Result::Ok::<&[i32], &u32>(&[1, 2, 3, 4, 5][..]); - assert_eq!(ref_ok.deref_ok(), expected_result); + let expected_result = Result::Ok::<&[i32], &u32>([1, 2, 3, 4, 5].as_slice()); + assert_eq!(ref_ok.as_deref_ok(), expected_result); - // &Result<T: Deref, E: Deref>::Ok(T).deref() -> + // &Result<T: Deref, E: Deref>::Ok(T).as_deref() -> // Result<&T::Deref::Target, &E::Deref::Target>::Ok(&*T) let ref_ok = &Result::Ok::<&i32, &u8>(&42); let expected_result = Result::Ok::<&i32, &u8>(&42); - assert_eq!(ref_ok.deref(), expected_result); + assert_eq!(ref_ok.as_deref(), expected_result); let ref_ok = &Result::Ok::<String, &u32>(String::from("a result")); let expected_result = Result::Ok::<&str, &u32>("a result"); - assert_eq!(ref_ok.deref(), expected_result); + assert_eq!(ref_ok.as_deref(), expected_result); let ref_ok = &Result::Ok::<Vec<i32>, &u32>(vec![1, 2, 3, 4, 5]); - let expected_result = Result::Ok::<&[i32], &u32>(&[1, 2, 3, 4, 5][..]); - assert_eq!(ref_ok.deref(), expected_result); + let expected_result = Result::Ok::<&[i32], &u32>([1, 2, 3, 4, 5].as_slice()); + assert_eq!(ref_ok.as_deref(), expected_result); - // &Result<T, E: Deref>::Err(T).deref_err() -> + // &Result<T, E: Deref>::Err(T).as_deref_err() -> // Result<&T, &E::Deref::Target>::Err(&*E) let ref_err = &Result::Err::<u8, &i32>(&41); let expected_result = Result::Err::<&u8, &i32>(&41); - assert_eq!(ref_err.deref_err(), expected_result); + assert_eq!(ref_err.as_deref_err(), expected_result); let ref_err = &Result::Err::<u32, String>(String::from("an error")); let expected_result = Result::Err::<&u32, &str>("an error"); - assert_eq!(ref_err.deref_err(), expected_result); + assert_eq!(ref_err.as_deref_err(), expected_result); let ref_err = &Result::Err::<u32, Vec<i32>>(vec![5, 4, 3, 2, 1]); - let expected_result = Result::Err::<&u32, &[i32]>(&[5, 4, 3, 2, 1][..]); - assert_eq!(ref_err.deref_err(), expected_result); + let expected_result = Result::Err::<&u32, &[i32]>([5, 4, 3, 2, 1].as_slice()); + assert_eq!(ref_err.as_deref_err(), expected_result); - // &Result<T: Deref, E: Deref>::Err(T).deref_err() -> + // &Result<T: Deref, E: Deref>::Err(T).as_deref_err() -> // Result<&T, &E::Deref::Target>::Err(&*E) let ref_err = &Result::Err::<&u8, &i32>(&41); let expected_result = Result::Err::<&u8, &i32>(&41); - assert_eq!(ref_err.deref(), expected_result); + assert_eq!(ref_err.as_deref(), expected_result); let ref_err = &Result::Err::<&u32, String>(String::from("an error")); let expected_result = Result::Err::<&u32, &str>("an error"); - assert_eq!(ref_err.deref(), expected_result); + assert_eq!(ref_err.as_deref(), expected_result); let ref_err = &Result::Err::<&u32, Vec<i32>>(vec![5, 4, 3, 2, 1]); - let expected_result = Result::Err::<&u32, &[i32]>(&[5, 4, 3, 2, 1][..]); - assert_eq!(ref_err.deref(), expected_result); + let expected_result = Result::Err::<&u32, &[i32]>([5, 4, 3, 2, 1].as_slice()); + assert_eq!(ref_err.as_deref(), expected_result); - // The following cases test calling deref_* with the wrong variant (i.e. - // `deref_ok()` with a `Result::Err()`, or `deref_err()` with a `Result::Ok()`. - // While unusual, these cases are supported to ensure that an `inner_deref` + // The following cases test calling `as_deref_*` with the wrong variant (i.e. + // `as_deref_ok()` with a `Result::Err()`, or `as_deref_err()` with a `Result::Ok()`. + // While uncommon, these cases are supported to ensure that an `as_deref_*` // call can still be made even when one of the Result types does not implement // `Deref` (for example, std::io::Error). - // &Result<T, E: Deref>::Ok(T).deref_err() -> + // &Result<T, E: Deref>::Ok(T).as_deref_err() -> // Result<&T, &E::Deref::Target>::Ok(&T) let ref_ok = &Result::Ok::<i32, &u8>(42); let expected_result = Result::Ok::<&i32, &u8>(&42); - assert_eq!(ref_ok.deref_err(), expected_result); + assert_eq!(ref_ok.as_deref_err(), expected_result); let ref_ok = &Result::Ok::<&str, &u32>("a result"); let expected_result = Result::Ok::<&&str, &u32>(&"a result"); - assert_eq!(ref_ok.deref_err(), expected_result); + assert_eq!(ref_ok.as_deref_err(), expected_result); let ref_ok = &Result::Ok::<[i32; 5], &u32>([1, 2, 3, 4, 5]); let expected_result = Result::Ok::<&[i32; 5], &u32>(&[1, 2, 3, 4, 5]); - assert_eq!(ref_ok.deref_err(), expected_result); + assert_eq!(ref_ok.as_deref_err(), expected_result); - // &Result<T: Deref, E>::Err(E).deref_ok() -> + // &Result<T: Deref, E>::Err(E).as_deref_ok() -> // Result<&T::Deref::Target, &E>::Err(&E) let ref_err = &Result::Err::<&u8, i32>(41); let expected_result = Result::Err::<&u8, &i32>(&41); - assert_eq!(ref_err.deref_ok(), expected_result); + assert_eq!(ref_err.as_deref_ok(), expected_result); let ref_err = &Result::Err::<&u32, &str>("an error"); let expected_result = Result::Err::<&u32, &&str>(&"an error"); - assert_eq!(ref_err.deref_ok(), expected_result); + assert_eq!(ref_err.as_deref_ok(), expected_result); let ref_err = &Result::Err::<&u32, [i32; 5]>([5, 4, 3, 2, 1]); let expected_result = Result::Err::<&u32, &[i32; 5]>(&[5, 4, 3, 2, 1]); - assert_eq!(ref_err.deref_ok(), expected_result); + assert_eq!(ref_err.as_deref_ok(), expected_result); +} + +#[test] +fn test_result_as_deref_mut() { + // &mut Result<T: Deref, E>::Ok(T).as_deref_mut_ok() -> + // Result<&mut T::Deref::Target, &mut E>::Ok(&mut *T) + let mut val = 42; + let mut expected_val = 42; + let mut_ok = &mut Result::Ok::<&mut i32, u8>(&mut val); + let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val); + assert_eq!(mut_ok.as_deref_mut_ok(), expected_result); + + let mut expected_string = String::from("a result"); + let mut_ok = &mut Result::Ok::<String, u32>(expected_string.clone()); + let expected_result = Result::Ok::<&mut str, &mut u32>(expected_string.deref_mut()); + assert_eq!(mut_ok.as_deref_mut_ok(), expected_result); + + let mut expected_vec = vec![1, 2, 3, 4, 5]; + let mut_ok = &mut Result::Ok::<Vec<i32>, u32>(expected_vec.clone()); + let expected_result = Result::Ok::<&mut [i32], &mut u32>(expected_vec.as_mut_slice()); + assert_eq!(mut_ok.as_deref_mut_ok(), expected_result); + + // &mut Result<T: Deref, E: Deref>::Ok(T).as_deref_mut() -> + // Result<&mut T::Deref::Target, &mut E::Deref::Target>::Ok(&mut *T) + let mut val = 42; + let mut expected_val = 42; + let mut_ok = &mut Result::Ok::<&mut i32, &mut u8>(&mut val); + let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val); + assert_eq!(mut_ok.as_deref_mut(), expected_result); + + let mut expected_string = String::from("a result"); + let mut_ok = &mut Result::Ok::<String, &mut u32>(expected_string.clone()); + let expected_result = Result::Ok::<&mut str, &mut u32>(expected_string.deref_mut()); + assert_eq!(mut_ok.as_deref_mut(), expected_result); + + let mut expected_vec = vec![1, 2, 3, 4, 5]; + let mut_ok = &mut Result::Ok::<Vec<i32>, &mut u32>(expected_vec.clone()); + let expected_result = Result::Ok::<&mut [i32], &mut u32>(expected_vec.as_mut_slice()); + assert_eq!(mut_ok.as_deref_mut(), expected_result); + + // &mut Result<T, E: Deref>::Err(T).as_deref_mut_err() -> + // Result<&mut T, &mut E::Deref::Target>::Err(&mut *E) + let mut val = 41; + let mut expected_val = 41; + let mut_err = &mut Result::Err::<u8, &mut i32>(&mut val); + let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val); + assert_eq!(mut_err.as_deref_mut_err(), expected_result); + + let mut expected_string = String::from("an error"); + let mut_err = &mut Result::Err::<u32, String>(expected_string.clone()); + let expected_result = Result::Err::<&mut u32, &mut str>(expected_string.deref_mut()); + assert_eq!(mut_err.as_deref_mut_err(), expected_result); + + let mut expected_vec = vec![5, 4, 3, 2, 1]; + let mut_err = &mut Result::Err::<u32, Vec<i32>>(expected_vec.clone()); + let expected_result = Result::Err::<&mut u32, &mut [i32]>(expected_vec.as_mut_slice()); + assert_eq!(mut_err.as_deref_mut_err(), expected_result); + + // &mut Result<T: Deref, E: Deref>::Err(T).as_deref_mut_err() -> + // Result<&mut T, &mut E::Deref::Target>::Err(&mut *E) + let mut val = 41; + let mut expected_val = 41; + let mut_err = &mut Result::Err::<&mut u8, &mut i32>(&mut val); + let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val); + assert_eq!(mut_err.as_deref_mut(), expected_result); + + let mut expected_string = String::from("an error"); + let mut_err = &mut Result::Err::<&mut u32, String>(expected_string.clone()); + let expected_result = Result::Err::<&mut u32, &mut str>(expected_string.as_mut_str()); + assert_eq!(mut_err.as_deref_mut(), expected_result); + + let mut expected_vec = vec![5, 4, 3, 2, 1]; + let mut_err = &mut Result::Err::<&mut u32, Vec<i32>>(expected_vec.clone()); + let expected_result = Result::Err::<&mut u32, &mut [i32]>(expected_vec.as_mut_slice()); + assert_eq!(mut_err.as_deref_mut(), expected_result); + + // The following cases test calling `as_deref_mut_*` with the wrong variant (i.e. + // `as_deref_mut_ok()` with a `Result::Err()`, or `as_deref_mut_err()` with a `Result::Ok()`. + // While uncommon, these cases are supported to ensure that an `as_deref_mut_*` + // call can still be made even when one of the Result types does not implement + // `Deref` (for example, std::io::Error). + + // &mut Result<T, E: Deref>::Ok(T).as_deref_mut_err() -> + // Result<&mut T, &mut E::Deref::Target>::Ok(&mut T) + let mut expected_val = 42; + let mut_ok = &mut Result::Ok::<i32, &mut u8>(expected_val.clone()); + let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val); + assert_eq!(mut_ok.as_deref_mut_err(), expected_result); + + let string = String::from("a result"); + let expected_string = string.clone(); + let mut ref_str = expected_string.as_ref(); + let mut_ok = &mut Result::Ok::<&str, &mut u32>(string.as_str()); + let expected_result = Result::Ok::<&mut &str, &mut u32>(&mut ref_str); + assert_eq!(mut_ok.as_deref_mut_err(), expected_result); + + let mut expected_arr = [1, 2, 3, 4, 5]; + let mut_ok = &mut Result::Ok::<[i32; 5], &mut u32>(expected_arr.clone()); + let expected_result = Result::Ok::<&mut [i32; 5], &mut u32>(&mut expected_arr); + assert_eq!(mut_ok.as_deref_mut_err(), expected_result); + + // &mut Result<T: Deref, E>::Err(E).as_deref_mut_ok() -> + // Result<&mut T::Deref::Target, &mut E>::Err(&mut E) + let mut expected_val = 41; + let mut_err = &mut Result::Err::<&mut u8, i32>(expected_val.clone()); + let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val); + assert_eq!(mut_err.as_deref_mut_ok(), expected_result); + + let string = String::from("an error"); + let expected_string = string.clone(); + let mut ref_str = expected_string.as_ref(); + let mut_err = &mut Result::Err::<&mut u32, &str>(string.as_str()); + let expected_result = Result::Err::<&mut u32, &mut &str>(&mut ref_str); + assert_eq!(mut_err.as_deref_mut_ok(), expected_result); + + let mut expected_arr = [5, 4, 3, 2, 1]; + let mut_err = &mut Result::Err::<&mut u32, [i32; 5]>(expected_arr.clone()); + let expected_result = Result::Err::<&mut u32, &mut [i32; 5]>(&mut expected_arr); + assert_eq!(mut_err.as_deref_mut_ok(), expected_result); } diff --git a/src/libcore/unicode/tables.rs b/src/libcore/unicode/tables.rs index a793ac3eb74..bfe784afaa4 100644 --- a/src/libcore/unicode/tables.rs +++ b/src/libcore/unicode/tables.rs @@ -1,6 +1,6 @@ // NOTE: The following code was generated by "./unicode.py", do not edit directly -#![allow(missing_docs, non_upper_case_globals, non_snake_case)] +#![allow(missing_docs, non_upper_case_globals, non_snake_case, clippy::unreadable_literal)] use crate::unicode::version::UnicodeVersion; use crate::unicode::bool_trie::{BoolTrie, SmallBoolTrie}; diff --git a/src/libcore/unicode/unicode.py b/src/libcore/unicode/unicode.py index 3a20d0548c1..5389d1cf803 100755 --- a/src/libcore/unicode/unicode.py +++ b/src/libcore/unicode/unicode.py @@ -79,7 +79,7 @@ FETCH_URL_VERSION = "ftp://ftp.unicode.org/Public/{version}/ucd/{filename}" PREAMBLE = """\ // NOTE: The following code was generated by "./unicode.py", do not edit directly -#![allow(missing_docs, non_upper_case_globals, non_snake_case)] +#![allow(missing_docs, non_upper_case_globals, non_snake_case, clippy::unreadable_literal)] use crate::unicode::version::UnicodeVersion; use crate::unicode::bool_trie::{{BoolTrie, SmallBoolTrie}}; |
