Skip to content

Commit f0dc8ad

Browse files
committed
Do we still need the NeverShortCircuit type?
This was added back before `ControlFlow` existed, when it was definitely better than `Result<T, !>` by avoiding the `From::from` calls. But now that we have `ControlFlow`, maybe we can avoid the extra type and impls? (Still keeps the helper methods for wrapping closures and such, of course.)
1 parent 07bdbae commit f0dc8ad

File tree

8 files changed

+20
-48
lines changed

8 files changed

+20
-48
lines changed

library/core/src/array/iter/iter_inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<T> PolymorphicIter<[MaybeUninit<T>]> {
199199

200200
#[inline]
201201
pub(super) fn fold<B>(&mut self, init: B, f: impl FnMut(B, T) -> B) -> B {
202-
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
202+
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
203203
}
204204

205205
#[inline]
@@ -257,7 +257,7 @@ impl<T> PolymorphicIter<[MaybeUninit<T>]> {
257257

258258
#[inline]
259259
pub(super) fn rfold<B>(&mut self, init: B, f: impl FnMut(B, T) -> B) -> B {
260-
self.try_rfold(init, NeverShortCircuit::wrap_mut_2(f)).0
260+
self.try_rfold(init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
261261
}
262262

263263
#[inline]

library/core/src/array/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn from_fn<T, const N: usize, F>(f: F) -> [T; N]
109109
where
110110
F: FnMut(usize) -> T,
111111
{
112-
try_from_fn(NeverShortCircuit::wrap_mut_1(f)).0
112+
try_from_fn(NeverShortCircuit::wrap_mut_1(f)).into_continue()
113113
}
114114

115115
/// Creates an array `[T; N]` where each fallible array element `T` is returned by the `cb` call.
@@ -553,7 +553,7 @@ impl<T, const N: usize> [T; N] {
553553
where
554554
F: FnMut(T) -> U,
555555
{
556-
self.try_map(NeverShortCircuit::wrap_mut_1(f)).0
556+
self.try_map(NeverShortCircuit::wrap_mut_1(f)).into_continue()
557557
}
558558

559559
/// A fallible function `f` applied to each element on array `self` in order to
@@ -848,7 +848,7 @@ impl<T, const N: usize> [T; N] {
848848
/// it easily optimizes away) so it doesn't impact the loop that fills the array.
849849
#[inline]
850850
fn from_trusted_iterator<T, const N: usize>(iter: impl UncheckedIterator<Item = T>) -> [T; N] {
851-
try_from_trusted_iterator(iter.map(NeverShortCircuit)).0
851+
try_from_trusted_iterator(iter.map(NeverShortCircuit::Continue)).into_continue()
852852
}
853853

854854
#[inline]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ where
212212
Self: Sized,
213213
F: FnMut(B, Self::Item) -> B,
214214
{
215-
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
215+
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
216216
}
217217
}
218218

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
4242
F: FnMut(B, Self::Item) -> B,
4343
{
4444
// `fold` needs ownership, so this can't forward directly.
45-
I::try_fold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
45+
I::try_fold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
4646
}
4747

4848
#[inline]
@@ -78,7 +78,7 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
7878
F: FnMut(B, Self::Item) -> B,
7979
{
8080
// `rfold` needs ownership, so this can't forward directly.
81-
I::try_rfold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
81+
I::try_rfold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
8282
}
8383

8484
#[inline]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<I: Iterator> SpecTake for Take<I> {
266266
F: FnMut(B, Self::Item) -> B,
267267
{
268268
use crate::ops::NeverShortCircuit;
269-
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
269+
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
270270
}
271271

272272
#[inline]

library/core/src/iter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ macro_rules! impl_fold_via_try_fold {
377377
{
378378
use crate::ops::NeverShortCircuit;
379379

380-
self.$try_fold(init, NeverShortCircuit::wrap_mut_2(fold)).0
380+
self.$try_fold(init, NeverShortCircuit::wrap_mut_2(fold)).into_continue()
381381
}
382382
};
383383
}

library/core/src/ops/index_range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl Iterator for IndexRange {
150150

151151
#[inline]
152152
fn fold<B, F: FnMut(B, usize) -> B>(mut self, init: B, f: F) -> B {
153-
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
153+
self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
154154
}
155155

156156
#[inline]
@@ -192,7 +192,7 @@ impl DoubleEndedIterator for IndexRange {
192192

193193
#[inline]
194194
fn rfold<B, F: FnMut(B, usize) -> B>(mut self, init: B, f: F) -> B {
195-
self.try_rfold(init, NeverShortCircuit::wrap_mut_2(f)).0
195+
self.try_rfold(init, NeverShortCircuit::wrap_mut_2(f)).into_continue()
196196
}
197197

198198
#[inline]

library/core/src/ops/try_trait.rs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::convert::Infallible;
12
use crate::ops::ControlFlow;
23

34
/// The `?` operator and `try {}` blocks.
@@ -384,15 +385,8 @@ pub fn residual_into_try_type<R: Residual<O>, O>(r: R) -> <R as Residual<O>>::Tr
384385
pub(crate) type ChangeOutputType<T: Try<Residual: Residual<V>>, V> =
385386
<T::Residual as Residual<V>>::TryType;
386387

387-
/// An adapter for implementing non-try methods via the `Try` implementation.
388-
///
389-
/// Conceptually the same as `Result<T, !>`, but requiring less work in trait
390-
/// solving and inhabited-ness checking and such, by being an obvious newtype
391-
/// and not having `From` bounds lying around.
392-
///
393-
/// Not currently planned to be exposed publicly, so just `pub(crate)`.
394-
#[repr(transparent)]
395-
pub(crate) struct NeverShortCircuit<T>(pub T);
388+
/// A helper alias for implementing non-try methods via the `Try` implementation.
389+
pub(crate) type NeverShortCircuit<T> = ControlFlow<Infallible, T>;
396390

397391
impl<T> NeverShortCircuit<T> {
398392
/// Wraps a unary function to produce one that wraps the output into a `NeverShortCircuit`.
@@ -403,43 +397,21 @@ impl<T> NeverShortCircuit<T> {
403397
pub(crate) fn wrap_mut_1<A>(
404398
mut f: impl FnMut(A) -> T,
405399
) -> impl FnMut(A) -> NeverShortCircuit<T> {
406-
move |a| NeverShortCircuit(f(a))
400+
move |a| NeverShortCircuit::Continue(f(a))
407401
}
408402

409403
#[inline]
410404
pub(crate) fn wrap_mut_2<A, B>(mut f: impl FnMut(A, B) -> T) -> impl FnMut(A, B) -> Self {
411-
move |a, b| NeverShortCircuit(f(a, b))
412-
}
413-
}
414-
415-
pub(crate) enum NeverShortCircuitResidual {}
416-
417-
impl<T> Try for NeverShortCircuit<T> {
418-
type Output = T;
419-
type Residual = NeverShortCircuitResidual;
420-
421-
#[inline]
422-
fn branch(self) -> ControlFlow<NeverShortCircuitResidual, T> {
423-
ControlFlow::Continue(self.0)
405+
move |a, b| NeverShortCircuit::Continue(f(a, b))
424406
}
425407

426408
#[inline]
427-
fn from_output(x: T) -> Self {
428-
NeverShortCircuit(x)
409+
pub(crate) fn into_continue(self) -> T {
410+
let NeverShortCircuit::Continue(v) = self;
411+
v
429412
}
430413
}
431414

432-
impl<T> FromResidual for NeverShortCircuit<T> {
433-
#[inline]
434-
fn from_residual(never: NeverShortCircuitResidual) -> Self {
435-
match never {}
436-
}
437-
}
438-
439-
impl<T> Residual<T> for NeverShortCircuitResidual {
440-
type TryType = NeverShortCircuit<T>;
441-
}
442-
443415
/// Implement `FromResidual<Yeet<T>>` on your type to enable
444416
/// `do yeet expr` syntax in functions returning your type.
445417
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]

0 commit comments

Comments
 (0)