Skip to content

Commit 6c7ea4c

Browse files
committed
refactored to implement without trait
1 parent 4c2e314 commit 6c7ea4c

File tree

2 files changed

+45
-75
lines changed

2 files changed

+45
-75
lines changed

src/libcore/option.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,17 @@ impl<T: Default> Option<T> {
878878
}
879879
}
880880

881+
# [unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
882+
impl<T: Deref> Option<T> {
883+
/// Converts from `&Option<T>` to `Option<&T::Target>`.
884+
///
885+
/// Leaves the original Option in-place, creating a new one with a reference
886+
/// to the original one, additionally coercing the contents via `Deref`.
887+
pub fn deref(&self) -> Option<&T::Target> {
888+
self.as_ref().map(|t| t.deref())
889+
}
890+
}
891+
881892
impl<T, E> Option<Result<T, E>> {
882893
/// Transposes an `Option` of a `Result` into a `Result` of an `Option`.
883894
///
@@ -978,24 +989,6 @@ impl<T> From<T> for Option<T> {
978989
}
979990
}
980991

981-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
982-
/// Extension trait to get a reference of an Option via the Deref trait.
983-
pub trait OptionDeref<T: Deref> {
984-
/// Converts from `&Option<T>` to `Option<&T::Target>`.
985-
///
986-
/// Leaves the original Option in-place, creating a new one with a reference
987-
/// to the original one, additionally coercing the contents via `Deref`.
988-
fn deref(&self) -> Option<&T::Target>;
989-
}
990-
991-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
992-
impl<T: Deref> OptionDeref<T> for Option<T>
993-
{
994-
fn deref(&self) -> Option<&T::Target> {
995-
self.as_ref().map(|t| t.deref())
996-
}
997-
}
998-
999992
/////////////////////////////////////////////////////////////////////////////
1000993
// The Option Iterators
1001994
/////////////////////////////////////////////////////////////////////////////

src/libcore/result.rs

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,40 @@ impl<T: Default, E> Result<T, E> {
909909
}
910910
}
911911

912+
impl<T: Deref, E: Deref> Result<T, E> {
913+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
914+
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E>`.
915+
///
916+
/// Leaves the original Result in-place, creating a new one with a reference
917+
/// to the original one, additionally coercing the `Ok` arm of the Result via
918+
/// `Deref`.
919+
pub fn deref_ok(&self) -> Result<&T::Target, &E> {
920+
self.as_ref().map(|t| t.deref())
921+
}
922+
923+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
924+
/// Converts from `&Result<T, E>` to `Result<&T, &E::Target>`.
925+
///
926+
/// Leaves the original Result in-place, creating a new one with a reference
927+
/// to the original one, additionally coercing the `Err` arm of the Result via
928+
/// `Deref`.
929+
pub fn deref_err(&self) -> Result<&T, &E::Target>
930+
{
931+
self.as_ref().map_err(|e| e.deref())
932+
}
933+
934+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
935+
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E::Target>`.
936+
///
937+
/// Leaves the original Result in-place, creating a new one with a reference
938+
/// to the original one, additionally coercing both the `Ok` and `Err` arms
939+
/// of the Result via `Deref`.
940+
pub fn deref(&self) -> Result<&T::Target, &E::Target>
941+
{
942+
self.as_ref().map(|t| t.deref()).map_err(|e| e.deref())
943+
}
944+
}
945+
912946
impl<T, E> Result<Option<T>, E> {
913947
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
914948
///
@@ -999,63 +1033,6 @@ impl<'a, T, E> IntoIterator for &'a mut Result<T, E> {
9991033
}
10001034
}
10011035

1002-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1003-
/// Extension trait to get a reference to a Result via the Deref trait.
1004-
pub trait ResultDeref<T, E> {
1005-
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E>`.
1006-
///
1007-
/// Leaves the original Result in-place, creating a new one with a reference
1008-
/// to the original one, additionally coercing the `Ok` arm of the Result via
1009-
/// `Deref`.
1010-
fn deref_ok(&self) -> Result<&T::Target, &E>
1011-
where
1012-
T: Deref;
1013-
1014-
/// Converts from `&Result<T, E>` to `Result<&T, &E::Target>`.
1015-
///
1016-
/// Leaves the original Result in-place, creating a new one with a reference
1017-
/// to the original one, additionally coercing the `Err` arm of the Result via
1018-
/// `Deref`.
1019-
fn deref_err(&self) -> Result<&T, &E::Target>
1020-
where
1021-
E: Deref;
1022-
1023-
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E::Target>`.
1024-
///
1025-
/// Leaves the original Result in-place, creating a new one with a reference
1026-
/// to the original one, additionally coercing both the `Ok` and `Err` arms
1027-
/// of the Result via `Deref`.
1028-
fn deref(&self) -> Result<&T::Target, &E::Target>
1029-
where
1030-
T: Deref,
1031-
E: Deref;
1032-
}
1033-
1034-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1035-
impl<T, E> ResultDeref<T, E> for Result<T, E> {
1036-
fn deref_ok(&self) -> Result<&T::Target, &E>
1037-
where
1038-
T: Deref,
1039-
{
1040-
self.as_ref().map(|t| t.deref())
1041-
}
1042-
1043-
fn deref_err(&self) -> Result<&T, &E::Target>
1044-
where
1045-
E: Deref,
1046-
{
1047-
self.as_ref().map_err(|e| e.deref())
1048-
}
1049-
1050-
fn deref(&self) -> Result<&T::Target, &E::Target>
1051-
where
1052-
T: Deref,
1053-
E: Deref,
1054-
{
1055-
self.as_ref().map(|t| t.deref()).map_err(|e| e.deref())
1056-
}
1057-
}
1058-
10591036
/////////////////////////////////////////////////////////////////////////////
10601037
// The Result Iterators
10611038
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)