Skip to content

Commit 648c9ca

Browse files
remove ambiguity for foo.try_reduce(...)?
1 parent 743de4b commit 648c9ca

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

examples/signatures.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#![feature(try_trait_v2)]
2+
#![feature(try_trait_v2_residual)]
3+
#![feature(iterator_try_reduce)]
24
#![allow(dead_code)]
35
#![allow(clippy::disallowed_names)]
46

5-
use std::ops::{ControlFlow, FromResidual, Try};
7+
use std::ops::{ControlFlow, FromResidual, Residual, Try};
68

79
trait Foo: Iterator {
810
fn try_reduce2<R, U>(&mut self, f: impl FnMut(Self::Item, Self::Item) -> R) -> U
911
where
1012
Self: Sized,
1113
R: Try<Output = Self::Item>,
14+
// U is the *canonical* TryType from R::Residual, without this foo.try_reduce(...)? is ambiguous
15+
R::Residual: Residual<Option<Self::Item>, TryType = U>,
1216
U: Try<Output = Option<Self::Item>> + FromResidual<R::Residual>,
1317
{
1418
let first = match self.next() {
@@ -23,4 +27,17 @@ trait Foo: Iterator {
2327
}
2428
}
2529

30+
impl<I: Iterator> Foo for I {}
31+
32+
fn immediate_qmark() -> Result<Option<i32>, i32> {
33+
let nums: [i32; 3] = [1, 2, 3];
34+
let sum_positive = |acc, n| match n {
35+
..0 => Err(n),
36+
_ => Ok(acc + n),
37+
};
38+
let _sum = nums.into_iter().try_reduce(sum_positive)?;
39+
let sum2 = nums.into_iter().try_reduce2(sum_positive)?;
40+
Ok(sum2)
41+
}
42+
2643
fn main() {}

0 commit comments

Comments
 (0)