Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,19 @@ impl<T> const ops::FromResidual for Option<T> {
}
}

#[unstable(feature = "try_trait_v2", issue = "84277")]
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
impl<T, E, F: ~const From<E>> const ops::FromResidual<Result<convert::Infallible, E>>
for Option<Result<T, F>>
{
#[inline]
fn from_residual(residual: Result<convert::Infallible, E>) -> Self {
match residual {
Err(e) => Some(Err(From::from(e))),
}
}
}

#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
impl<T> ops::FromResidual<ops::Yeet<()>> for Option<T> {
#[inline]
Expand Down
11 changes: 11 additions & 0 deletions library/core/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,14 @@ fn zip_unzip_roundtrip() {
let a = z.unzip();
assert_eq!(a, (x, y));
}

#[test]
fn from_residual_option_result() {
fn test<F: FnOnce() -> Result<u8, u16>>(f: F) -> Option<Result<u8, u32>> {
let x: u8 = f()?;
Some(Ok(x * 2))
}

assert_eq!(test(|| Ok(2)), Some(Ok(4)));
assert_eq!(test(|| Err(2)), Some(Err(2)));
}
2 changes: 2 additions & 0 deletions src/test/ui/try-trait/bad-interconversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ LL | | }
|
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `Option<u16>`
= help: the following other types implement trait `FromResidual<R>`:
<Option<Result<T, F>> as FromResidual<Result<Infallible, E>>>
<Option<T> as FromResidual<Yeet<()>>>
<Option<T> as FromResidual>

Expand All @@ -77,6 +78,7 @@ LL | | }
|
= help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Option<u64>`
= help: the following other types implement trait `FromResidual<R>`:
<Option<Result<T, F>> as FromResidual<Result<Infallible, E>>>
<Option<T> as FromResidual<Yeet<()>>>
<Option<T> as FromResidual>

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/try-trait/option-to-result.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ LL | | }
|
= help: the trait `FromResidual<Result<Infallible, i32>>` is not implemented for `Option<i32>`
= help: the following other types implement trait `FromResidual<R>`:
<Option<Result<T, F>> as FromResidual<Result<Infallible, E>>>
<Option<T> as FromResidual<Yeet<()>>>
<Option<T> as FromResidual>

Expand Down