-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Revert const hacks and use const closures in std #155957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,11 +106,11 @@ pub fn repeat<T: Clone, const N: usize>(val: T) -> [T; N] { | |
| #[inline] | ||
| #[stable(feature = "array_from_fn", since = "1.63.0")] | ||
| #[rustc_const_unstable(feature = "const_array", issue = "147606")] | ||
| pub const fn from_fn<T: [const] Destruct, const N: usize, F>(f: F) -> [T; N] | ||
| pub const fn from_fn<T: [const] Destruct, const N: usize, F>(mut f: F) -> [T; N] | ||
| where | ||
| F: [const] FnMut(usize) -> T + [const] Destruct, | ||
| { | ||
| try_from_fn(NeverShortCircuit::wrap_mut_1(f)).0 | ||
| try_from_fn(const move |a| NeverShortCircuit(f(a))).0 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, the Wrapped type was only used in these two cases |
||
| } | ||
|
|
||
| /// Creates an array `[T; N]` where each fallible array element `T` is returned by the `cb` call. | ||
|
|
@@ -580,13 +580,13 @@ impl<T, const N: usize> [T; N] { | |
| #[must_use] | ||
| #[stable(feature = "array_map", since = "1.55.0")] | ||
| #[rustc_const_unstable(feature = "const_array", issue = "147606")] | ||
| pub const fn map<F, U>(self, f: F) -> [U; N] | ||
| pub const fn map<F, U>(self, mut f: F) -> [U; N] | ||
| where | ||
| F: [const] FnMut(T) -> U + [const] Destruct, | ||
| U: [const] Destruct, | ||
| T: [const] Destruct, | ||
| { | ||
| self.try_map(NeverShortCircuit::wrap_mut_1(f)).0 | ||
| self.try_map(const |a| NeverShortCircuit(f(a))).0 | ||
| } | ||
|
|
||
| /// A fallible function `f` applied to each element on array `self` in order to | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| use crate::marker::{Destruct, PhantomData}; | ||
| use crate::marker::Destruct; | ||
| use crate::ops::ControlFlow; | ||
|
|
||
| /// The `?` operator and `try {}` blocks. | ||
|
|
@@ -398,39 +398,8 @@ pub(crate) type ChangeOutputType<T: Try<Residual: Residual<V>>, V> = | |
| /// Not currently planned to be exposed publicly, so just `pub(crate)`. | ||
| #[repr(transparent)] | ||
| pub(crate) struct NeverShortCircuit<T>(pub T); | ||
| // FIXME(const-hack): replace with `|a| NeverShortCircuit(f(a))` when const closures added. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these simply not needed any more?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, they were only for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm... I guess we could keep it and return a const closure from it, but is it really worth it?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i mean i think so.. thats what was done pre const hack at least, and i dont think it was a bad design? |
||
| pub(crate) struct Wrapped<T, A, F: FnMut(A) -> T> { | ||
| f: F, | ||
| p: PhantomData<(T, A)>, | ||
| } | ||
| #[rustc_const_unstable(feature = "const_never_short_circuit", issue = "none")] | ||
| impl<T, A, F: [const] FnMut(A) -> T + [const] Destruct> const FnOnce<(A,)> for Wrapped<T, A, F> { | ||
| type Output = NeverShortCircuit<T>; | ||
|
|
||
| extern "rust-call" fn call_once(mut self, args: (A,)) -> Self::Output { | ||
| self.call_mut(args) | ||
| } | ||
| } | ||
| #[rustc_const_unstable(feature = "const_never_short_circuit", issue = "none")] | ||
| impl<T, A, F: [const] FnMut(A) -> T> const FnMut<(A,)> for Wrapped<T, A, F> { | ||
| extern "rust-call" fn call_mut(&mut self, (args,): (A,)) -> Self::Output { | ||
| NeverShortCircuit((self.f)(args)) | ||
| } | ||
| } | ||
|
|
||
| impl<T> NeverShortCircuit<T> { | ||
| /// Wraps a unary function to produce one that wraps the output into a `NeverShortCircuit`. | ||
| /// | ||
| /// This is useful for implementing infallible functions in terms of the `try_` ones, | ||
| /// without accidentally capturing extra generic parameters in a closure. | ||
| #[inline] | ||
| pub(crate) const fn wrap_mut_1<A, F>(f: F) -> Wrapped<T, A, F> | ||
| where | ||
| F: [const] FnMut(A) -> T, | ||
| { | ||
| Wrapped { f, p: PhantomData } | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) fn wrap_mut_2<A, B>(mut f: impl FnMut(A, B) -> T) -> impl FnMut(A, B) -> Self { | ||
| move |a, b| NeverShortCircuit(f(a, b)) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why isn't it
repeat_packed?View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was a very recent change and I messed up a rebase over it, fixed