Skip to content

Commit 5573a16

Browse files
committed
Use try{} in try_fold to decouple library from Try details
1 parent 8dae8cd commit 5573a16

File tree

8 files changed

+31
-30
lines changed

8 files changed

+31
-30
lines changed

library/core/src/iter/adapters/chain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ where
109109
acc = b.try_fold(acc, f)?;
110110
// we don't fuse the second iterator
111111
}
112-
Try::from_ok(acc)
112+
try { acc }
113113
}
114114

115115
fn fold<Acc, F>(self, mut acc: Acc, mut f: F) -> Acc
@@ -292,7 +292,7 @@ where
292292
acc = a.try_rfold(acc, f)?;
293293
// we don't fuse the second iterator
294294
}
295-
Try::from_ok(acc)
295+
try { acc }
296296
}
297297

298298
fn rfold<Acc, F>(self, mut acc: Acc, mut f: F) -> Acc

library/core/src/iter/adapters/flatten.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ where
317317
}
318318
self.backiter = None;
319319

320-
Try::from_ok(init)
320+
try { init }
321321
}
322322

323323
#[inline]
@@ -397,7 +397,7 @@ where
397397
}
398398
self.frontiter = None;
399399

400-
Try::from_ok(init)
400+
try { init }
401401
}
402402

403403
#[inline]

library/core/src/iter/adapters/fuse.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ where
303303
acc = iter.try_fold(acc, fold)?;
304304
self.iter = None;
305305
}
306-
Try::from_ok(acc)
306+
try { acc }
307307
}
308308

309309
#[inline]
@@ -353,7 +353,7 @@ where
353353
acc = iter.try_rfold(acc, fold)?;
354354
self.iter = None;
355355
}
356-
Try::from_ok(acc)
356+
try { acc }
357357
}
358358

359359
#[inline]

library/core/src/iter/adapters/mod.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ where
579579
})?;
580580

581581
if is_empty {
582-
return Try::from_ok(acc);
582+
return try { acc };
583583
}
584584

585585
loop {
@@ -715,7 +715,7 @@ where
715715
if self.first_take {
716716
self.first_take = false;
717717
match self.iter.next() {
718-
None => return Try::from_ok(acc),
718+
None => return try { acc },
719719
Some(x) => acc = f(acc, x)?,
720720
}
721721
}
@@ -792,7 +792,7 @@ where
792792
}
793793

794794
match self.next_back() {
795-
None => Try::from_ok(init),
795+
None => try { init },
796796
Some(x) => {
797797
let acc = f(init, x)?;
798798
from_fn(nth_back(&mut self.iter, self.step)).try_fold(acc, f)
@@ -1075,7 +1075,7 @@ fn filter_try_fold<'a, T, Acc, R: Try<Ok = Acc>>(
10751075
predicate: &'a mut impl FnMut(&T) -> bool,
10761076
mut fold: impl FnMut(Acc, T) -> R + 'a,
10771077
) -> impl FnMut(Acc, T) -> R + 'a {
1078-
move |acc, item| if predicate(&item) { fold(acc, item) } else { R::from_ok(acc) }
1078+
move |acc, item| if predicate(&item) { fold(acc, item) } else { try { acc } }
10791079
}
10801080

10811081
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1229,7 +1229,7 @@ fn filter_map_try_fold<'a, T, B, Acc, R: Try<Ok = Acc>>(
12291229
) -> impl FnMut(Acc, T) -> R + 'a {
12301230
move |acc, item| match f(item) {
12311231
Some(x) => fold(acc, x),
1232-
None => R::from_ok(acc),
1232+
None => try { acc },
12331233
}
12341234
}
12351235

@@ -1660,7 +1660,7 @@ impl<I: Iterator> Iterator for Peekable<I> {
16601660
R: Try<Ok = B>,
16611661
{
16621662
let acc = match self.peeked.take() {
1663-
Some(None) => return Try::from_ok(init),
1663+
Some(None) => return try { init },
16641664
Some(Some(v)) => f(init, v)?,
16651665
None => init,
16661666
};
@@ -1703,7 +1703,7 @@ where
17031703
R: Try<Ok = B>,
17041704
{
17051705
match self.peeked.take() {
1706-
Some(None) => Try::from_ok(init),
1706+
Some(None) => try { init },
17071707
Some(Some(v)) => match self.iter.try_rfold(init, &mut f).into_result() {
17081708
Ok(acc) => f(acc, v),
17091709
Err(e) => {
@@ -1938,7 +1938,7 @@ where
19381938
if !self.flag {
19391939
match self.next() {
19401940
Some(v) => init = fold(init, v)?,
1941-
None => return Try::from_ok(init),
1941+
None => return try { init },
19421942
}
19431943
}
19441944
self.iter.try_fold(init, fold)
@@ -2065,13 +2065,13 @@ where
20652065
ControlFlow::from_try(fold(acc, x))
20662066
} else {
20672067
*flag = true;
2068-
ControlFlow::Break(Try::from_ok(acc))
2068+
ControlFlow::Break(try { acc })
20692069
}
20702070
}
20712071
}
20722072

20732073
if self.flag {
2074-
Try::from_ok(init)
2074+
try { init }
20752075
} else {
20762076
let flag = &mut self.flag;
20772077
let p = &mut self.predicate;
@@ -2180,7 +2180,7 @@ where
21802180
let Self { iter, predicate } = self;
21812181
iter.try_fold(init, |acc, x| match predicate(x) {
21822182
Some(item) => ControlFlow::from_try(fold(acc, item)),
2183-
None => ControlFlow::Break(Try::from_ok(acc)),
2183+
None => ControlFlow::Break(try { acc }),
21842184
})
21852185
.into_try()
21862186
}
@@ -2316,7 +2316,7 @@ where
23162316
if n > 0 {
23172317
// nth(n) skips n+1
23182318
if self.iter.nth(n - 1).is_none() {
2319-
return Try::from_ok(init);
2319+
return try { init };
23202320
}
23212321
}
23222322
self.iter.try_fold(init, fold)
@@ -2382,7 +2382,7 @@ where
23822382

23832383
let n = self.len();
23842384
if n == 0 {
2385-
Try::from_ok(init)
2385+
try { init }
23862386
} else {
23872387
self.iter.try_rfold(init, check(n, fold)).into_try()
23882388
}
@@ -2509,7 +2509,7 @@ where
25092509
}
25102510

25112511
if self.n == 0 {
2512-
Try::from_ok(init)
2512+
try { init }
25132513
} else {
25142514
let n = &mut self.n;
25152515
self.iter.try_fold(init, check(n, fold)).into_try()
@@ -2587,11 +2587,11 @@ where
25872587
R: Try<Ok = Acc>,
25882588
{
25892589
if self.n == 0 {
2590-
Try::from_ok(init)
2590+
try { init }
25912591
} else {
25922592
let len = self.iter.len();
25932593
if len > self.n && self.iter.nth_back(len - self.n - 1).is_none() {
2594-
Try::from_ok(init)
2594+
try { init }
25952595
} else {
25962596
self.iter.try_rfold(init, fold)
25972597
}
@@ -2687,7 +2687,7 @@ where
26872687
mut fold: impl FnMut(Acc, B) -> R + 'a,
26882688
) -> impl FnMut(Acc, T) -> ControlFlow<Acc, R> + 'a {
26892689
move |acc, x| match f(state, x) {
2690-
None => ControlFlow::Break(Try::from_ok(acc)),
2690+
None => ControlFlow::Break(try { acc }),
26912691
Some(x) => ControlFlow::from_try(fold(acc, x)),
26922692
}
26932693
}
@@ -2951,7 +2951,7 @@ where
29512951
Ok(x) => ControlFlow::from_try(f(acc, x)),
29522952
Err(e) => {
29532953
*error = Err(e);
2954-
ControlFlow::Break(Try::from_ok(acc))
2954+
ControlFlow::Break(try { acc })
29552955
}
29562956
})
29572957
.into_try()

library/core/src/iter/range.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
713713
R: Try<Ok = B>,
714714
{
715715
if self.is_empty() {
716-
return Try::from_ok(init);
716+
return try { init };
717717
}
718718

719719
let mut accum = init;
@@ -731,7 +731,7 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
731731
accum = f(accum, self.start.clone())?;
732732
}
733733

734-
Try::from_ok(accum)
734+
try { accum }
735735
}
736736

737737
#[inline]
@@ -818,7 +818,7 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
818818
R: Try<Ok = B>,
819819
{
820820
if self.is_empty() {
821-
return Try::from_ok(init);
821+
return try { init };
822822
}
823823

824824
let mut accum = init;
@@ -836,7 +836,7 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
836836
accum = f(accum, self.start.clone())?;
837837
}
838838

839-
Try::from_ok(accum)
839+
try { accum }
840840
}
841841

842842
#[inline]

library/core/src/iter/traits/double_ended.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub trait DoubleEndedIterator: Iterator {
221221
while let Some(x) = self.next_back() {
222222
accum = f(accum, x)?;
223223
}
224-
Try::from_ok(accum)
224+
try { accum }
225225
}
226226

227227
/// An iterator method that reduces the iterator's elements to a single,

library/core/src/iter/traits/iterator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ pub trait Iterator {
18871887
while let Some(x) = self.next() {
18881888
accum = f(accum, x)?;
18891889
}
1890-
Try::from_ok(accum)
1890+
try { accum }
18911891
}
18921892

18931893
/// An iterator method that applies a fallible function to each item in the

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
#![feature(std_internals)]
128128
#![feature(stmt_expr_attributes)]
129129
#![feature(transparent_unions)]
130+
#![feature(try_blocks)]
130131
#![feature(unboxed_closures)]
131132
#![feature(unsized_locals)]
132133
#![feature(untagged_unions)]

0 commit comments

Comments
 (0)