Skip to content

Commit 0af9eca

Browse files
committed
Remove unnecessary abstractions from option and result impls
Change the implementation of a option and result methods to avoid code generation of additional functions, where a simple match is sufficient.
1 parent 195ad48 commit 0af9eca

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

library/core/src/option.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,10 @@ impl<T: Copy> Option<&T> {
10601060
/// ```
10611061
#[stable(feature = "copied", since = "1.35.0")]
10621062
pub fn copied(self) -> Option<T> {
1063-
self.map(|&t| t)
1063+
match self {
1064+
None => None,
1065+
Some(t) => Some(*t),
1066+
}
10641067
}
10651068
}
10661069

@@ -1079,7 +1082,10 @@ impl<T: Copy> Option<&mut T> {
10791082
/// ```
10801083
#[stable(feature = "copied", since = "1.35.0")]
10811084
pub fn copied(self) -> Option<T> {
1082-
self.map(|&mut t| t)
1085+
match self {
1086+
None => None,
1087+
Some(t) => Some(*t),
1088+
}
10831089
}
10841090
}
10851091

@@ -1098,7 +1104,10 @@ impl<T: Clone> Option<&T> {
10981104
/// ```
10991105
#[stable(feature = "rust1", since = "1.0.0")]
11001106
pub fn cloned(self) -> Option<T> {
1101-
self.map(|t| t.clone())
1107+
match self {
1108+
None => None,
1109+
Some(t) => Some(t.clone()),
1110+
}
11021111
}
11031112
}
11041113

@@ -1117,7 +1126,10 @@ impl<T: Clone> Option<&mut T> {
11171126
/// ```
11181127
#[stable(since = "1.26.0", feature = "option_ref_mut_cloned")]
11191128
pub fn cloned(self) -> Option<T> {
1120-
self.map(|t| t.clone())
1129+
match self {
1130+
None => None,
1131+
Some(t) => Some(t.clone()),
1132+
}
11211133
}
11221134
}
11231135

@@ -1259,7 +1271,10 @@ impl<T: Deref> Option<T> {
12591271
/// ```
12601272
#[stable(feature = "option_deref", since = "1.40.0")]
12611273
pub fn as_deref(&self) -> Option<&T::Target> {
1262-
self.as_ref().map(|t| t.deref())
1274+
match self {
1275+
None => None,
1276+
Some(t) => Some(t.deref()),
1277+
}
12631278
}
12641279
}
12651280

@@ -1280,7 +1295,10 @@ impl<T: DerefMut> Option<T> {
12801295
/// ```
12811296
#[stable(feature = "option_deref", since = "1.40.0")]
12821297
pub fn as_deref_mut(&mut self) -> Option<&mut T::Target> {
1283-
self.as_mut().map(|t| t.deref_mut())
1298+
match self {
1299+
None => None,
1300+
Some(t) => Some(t.deref_mut()),
1301+
}
12841302
}
12851303
}
12861304

library/core/src/result.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229

230230
use crate::iter::{self, FromIterator, FusedIterator, TrustedLen};
231231
use crate::ops::{self, Deref, DerefMut};
232-
use crate::{convert, fmt, hint};
232+
use crate::{fmt, hint};
233233

234234
/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
235235
///
@@ -298,7 +298,7 @@ impl<T, E> Result<T, E> {
298298
#[inline]
299299
#[stable(feature = "rust1", since = "1.0.0")]
300300
pub const fn is_err(&self) -> bool {
301-
!self.is_ok()
301+
matches!(*self, Err(_))
302302
}
303303

304304
/// Returns `true` if the result is an [`Ok`] value containing the given value.
@@ -907,7 +907,10 @@ impl<T: Copy, E> Result<&T, E> {
907907
/// ```
908908
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
909909
pub fn copied(self) -> Result<T, E> {
910-
self.map(|&t| t)
910+
match self {
911+
Ok(x) => Ok(*x),
912+
Err(e) => Err(e),
913+
}
911914
}
912915
}
913916

@@ -927,7 +930,10 @@ impl<T: Copy, E> Result<&mut T, E> {
927930
/// ```
928931
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
929932
pub fn copied(self) -> Result<T, E> {
930-
self.map(|&mut t| t)
933+
match self {
934+
Ok(x) => Ok(*x),
935+
Err(e) => Err(e),
936+
}
931937
}
932938
}
933939

@@ -947,7 +953,10 @@ impl<T: Clone, E> Result<&T, E> {
947953
/// ```
948954
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
949955
pub fn cloned(self) -> Result<T, E> {
950-
self.map(|t| t.clone())
956+
match self {
957+
Ok(x) => Ok(x.clone()),
958+
Err(e) => Err(e),
959+
}
951960
}
952961
}
953962

@@ -967,7 +976,10 @@ impl<T: Clone, E> Result<&mut T, E> {
967976
/// ```
968977
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
969978
pub fn cloned(self) -> Result<T, E> {
970-
self.map(|t| t.clone())
979+
match self {
980+
Ok(x) => Ok(x.clone()),
981+
Err(e) => Err(e),
982+
}
971983
}
972984
}
973985

@@ -1186,7 +1198,10 @@ impl<T: Deref, E> Result<T, E> {
11861198
/// ```
11871199
#[stable(feature = "inner_deref", since = "1.47.0")]
11881200
pub fn as_deref(&self) -> Result<&T::Target, &E> {
1189-
self.as_ref().map(|t| t.deref())
1201+
match self {
1202+
Ok(x) => Ok(x.deref()),
1203+
Err(e) => Err(e),
1204+
}
11901205
}
11911206
}
11921207

@@ -1211,7 +1226,10 @@ impl<T: DerefMut, E> Result<T, E> {
12111226
/// ```
12121227
#[stable(feature = "inner_deref", since = "1.47.0")]
12131228
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E> {
1214-
self.as_mut().map(|t| t.deref_mut())
1229+
match self {
1230+
Ok(x) => Ok(x.deref_mut()),
1231+
Err(e) => Err(e),
1232+
}
12151233
}
12161234
}
12171235

@@ -1273,7 +1291,10 @@ impl<T, E> Result<Result<T, E>, E> {
12731291
#[inline]
12741292
#[unstable(feature = "result_flattening", issue = "70142")]
12751293
pub fn flatten(self) -> Result<T, E> {
1276-
self.and_then(convert::identity)
1294+
match self {
1295+
Ok(x) => x,
1296+
Err(e) => Err(e),
1297+
}
12771298
}
12781299
}
12791300

0 commit comments

Comments
 (0)