|
232 | 232 |
|
233 | 233 | use fmt; |
234 | 234 | use iter::{FromIterator, FusedIterator, TrustedLen}; |
235 | | -use ops::{self, Deref}; |
| 235 | +use ops::{self, Deref, DerefMut}; |
236 | 236 |
|
237 | 237 | /// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]). |
238 | 238 | /// |
@@ -927,42 +927,75 @@ impl<T: Default, E> Result<T, E> { |
927 | 927 |
|
928 | 928 | #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] |
929 | 929 | impl<T: Deref, E> Result<T, E> { |
930 | | - /// Converts from `&Result<T, E>` to `Result<&T::Target, &E>`. |
| 930 | + /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`. |
931 | 931 | /// |
932 | | - /// Leaves the original Result in-place, creating a new one with a reference |
933 | | - /// to the original one, additionally coercing the `Ok` arm of the Result via |
934 | | - /// `Deref`. |
935 | | - pub fn deref_ok(&self) -> Result<&T::Target, &E> { |
| 932 | + /// Leaves the original `Result` in-place, creating a new one containing a reference to the |
| 933 | + /// `Ok` type's `Deref::Target` type. |
| 934 | + pub fn as_deref_ok(&self) -> Result<&T::Target, &E> { |
936 | 935 | self.as_ref().map(|t| t.deref()) |
937 | 936 | } |
938 | 937 | } |
939 | 938 |
|
940 | 939 | #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] |
941 | 940 | impl<T, E: Deref> Result<T, E> { |
942 | | - /// Converts from `&Result<T, E>` to `Result<&T, &E::Target>`. |
| 941 | + /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T, &E::Target>`. |
943 | 942 | /// |
944 | | - /// Leaves the original Result in-place, creating a new one with a reference |
945 | | - /// to the original one, additionally coercing the `Err` arm of the Result via |
946 | | - /// `Deref`. |
947 | | - pub fn deref_err(&self) -> Result<&T, &E::Target> |
| 943 | + /// Leaves the original `Result` in-place, creating a new one containing a reference to the |
| 944 | + /// `Err` type's `Deref::Target` type. |
| 945 | + pub fn as_deref_err(&self) -> Result<&T, &E::Target> |
948 | 946 | { |
949 | 947 | self.as_ref().map_err(|e| e.deref()) |
950 | 948 | } |
951 | 949 | } |
952 | 950 |
|
953 | 951 | #[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] |
954 | 952 | impl<T: Deref, E: Deref> Result<T, E> { |
955 | | - /// Converts from `&Result<T, E>` to `Result<&T::Target, &E::Target>`. |
| 953 | + /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E::Target>`. |
956 | 954 | /// |
957 | | - /// Leaves the original Result in-place, creating a new one with a reference |
958 | | - /// to the original one, additionally coercing both the `Ok` and `Err` arms |
959 | | - /// of the Result via `Deref`. |
960 | | - pub fn deref(&self) -> Result<&T::Target, &E::Target> |
| 955 | + /// Leaves the original `Result` in-place, creating a new one containing a reference to both |
| 956 | + /// the `Ok` and `Err` types' `Deref::Target` types. |
| 957 | + pub fn as_deref(&self) -> Result<&T::Target, &E::Target> |
961 | 958 | { |
962 | 959 | self.as_ref().map(|t| t.deref()).map_err(|e| e.deref()) |
963 | 960 | } |
964 | 961 | } |
965 | 962 |
|
| 963 | +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] |
| 964 | +impl<T: DerefMut, E> Result<T, E> { |
| 965 | + /// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T::Target, &mut E>`. |
| 966 | + /// |
| 967 | + /// Leaves the original `Result` in-place, creating a new one containing a mutable reference to |
| 968 | + /// the `Ok` type's `Deref::Target` type. |
| 969 | + pub fn as_deref_mut_ok(&mut self) -> Result<&mut T::Target, &mut E> { |
| 970 | + self.as_mut().map(|t| t.deref_mut()) |
| 971 | + } |
| 972 | +} |
| 973 | + |
| 974 | +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] |
| 975 | +impl<T, E: DerefMut> Result<T, E> { |
| 976 | + /// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T, &mut E::Target>`. |
| 977 | + /// |
| 978 | + /// Leaves the original `Result` in-place, creating a new one containing a mutable reference to |
| 979 | + /// the `Err` type's `Deref::Target` type. |
| 980 | + pub fn as_deref_mut_err(&mut self) -> Result<&mut T, &mut E::Target> |
| 981 | + { |
| 982 | + self.as_mut().map_err(|e| e.deref_mut()) |
| 983 | + } |
| 984 | +} |
| 985 | + |
| 986 | +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] |
| 987 | +impl<T: DerefMut, E: DerefMut> Result<T, E> { |
| 988 | + /// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to |
| 989 | + /// `Result<&mut T::Target, &mut E::Target>`. |
| 990 | + /// |
| 991 | + /// Leaves the original `Result` in-place, creating a new one containing a mutable reference to |
| 992 | + /// both the `Ok` and `Err` types' `Deref::Target` types. |
| 993 | + pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E::Target> |
| 994 | + { |
| 995 | + self.as_mut().map(|t| t.deref_mut()).map_err(|e| e.deref_mut()) |
| 996 | + } |
| 997 | +} |
| 998 | + |
966 | 999 | impl<T, E> Result<Option<T>, E> { |
967 | 1000 | /// Transposes a `Result` of an `Option` into an `Option` of a `Result`. |
968 | 1001 | /// |
|
0 commit comments