-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add const_closure, Constify Try trait #102186
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b2f717
Added const_closure
onestacked 8e0ea60
Constifed Try trait
onestacked 53049f7
Fixed Doc-Tests
onestacked 6267c60
Added some spacing in const closure
onestacked d78bc41
Remove unused `ConstFn(Once)Closure` structs.
onestacked 84666af
Constify Residual behind const_try
onestacked File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
use crate::marker::Destruct; | ||
|
||
/// Struct representing a closure with owned data. | ||
/// | ||
/// Example: | ||
/// ```no_build | ||
/// use crate::const_closure::ConstFnOnceClosure; | ||
/// const fn imp(state: i32, (arg,): (i32,)) -> i32 { | ||
/// state + arg | ||
/// } | ||
/// let i = 5; | ||
/// let cl = ConstFnOnceClosure::new(i, imp); | ||
/// | ||
/// assert!(7 == cl(2)); | ||
/// ``` | ||
pub(crate) struct ConstFnOnceClosure<CapturedData, Function> { | ||
data: CapturedData, | ||
func: Function, | ||
} | ||
impl<CapturedData, Function> ConstFnOnceClosure<CapturedData, Function> { | ||
/// Function for creating a new closure. | ||
/// | ||
/// `data` is the owned data that is captured from the environment (this data must be `~const Destruct`). | ||
/// | ||
/// `func` is the function of the closure, it gets the data and a tuple of the arguments closure | ||
/// and return the return value of the closure. | ||
#[allow(dead_code)] | ||
pub(crate) const fn new<ClosureArguments, ClosureReturnValue>( | ||
data: CapturedData, | ||
func: Function, | ||
) -> Self | ||
where | ||
CapturedData: ~const Destruct, | ||
Function: ~const Fn(CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct, | ||
{ | ||
Self { data, func } | ||
} | ||
} | ||
impl<CapturedData, ClosureArguments, Function> const FnOnce<ClosureArguments> | ||
for ConstFnOnceClosure<CapturedData, Function> | ||
where | ||
CapturedData: ~const Destruct, | ||
Function: ~const Fn<(CapturedData, ClosureArguments)> + ~const Destruct, | ||
{ | ||
type Output = Function::Output; | ||
|
||
extern "rust-call" fn call_once(self, args: ClosureArguments) -> Self::Output { | ||
(self.func)(self.data, args) | ||
} | ||
} | ||
/// Struct representing a closure with mutably borrowed data. | ||
/// | ||
/// Example: | ||
/// ```no_build | ||
/// #![feature(const_mut_refs)] | ||
/// use crate::const_closure::ConstFnMutClosure; | ||
/// const fn imp(state: &mut i32, (arg,): (i32,)) -> i32 { | ||
/// *state += arg; | ||
/// *state | ||
/// } | ||
/// let mut i = 5; | ||
/// let mut cl = ConstFnMutClosure::new(&mut i, imp); | ||
/// | ||
/// assert!(7 == cl(2)); | ||
/// assert!(8 == cl(1)); | ||
/// ``` | ||
pub(crate) struct ConstFnMutClosure<'a, CapturedData: ?Sized, Function> { | ||
data: &'a mut CapturedData, | ||
func: Function, | ||
} | ||
impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<'a, CapturedData, Function> { | ||
/// Function for creating a new closure. | ||
/// | ||
/// `data` is the a mutable borrow of data that is captured from the environment. | ||
/// | ||
/// `func` is the function of the closure, it gets the data and a tuple of the arguments closure | ||
/// and return the return value of the closure. | ||
pub(crate) const fn new<ClosureArguments, ClosureReturnValue>( | ||
data: &'a mut CapturedData, | ||
func: Function, | ||
) -> Self | ||
where | ||
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue, | ||
{ | ||
Self { data, func } | ||
} | ||
} | ||
impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const | ||
FnOnce<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function> | ||
where | ||
Function: | ||
~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct, | ||
{ | ||
type Output = ClosureReturnValue; | ||
|
||
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output { | ||
self.call_mut(args) | ||
} | ||
} | ||
impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const | ||
FnMut<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function> | ||
where | ||
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue, | ||
{ | ||
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output { | ||
(self.func)(self.data, args) | ||
} | ||
} | ||
|
||
/// Struct representing a closure with borrowed data. | ||
/// | ||
/// Example: | ||
/// ```no_build | ||
/// use crate::const_closure::ConstFnClosure; | ||
/// | ||
/// const fn imp(state: &i32, (arg,): (i32,)) -> i32 { | ||
/// *state + arg | ||
/// } | ||
/// let i = 5; | ||
/// let cl = ConstFnClosure::new(&i, imp); | ||
/// | ||
/// assert!(7 == cl(2)); | ||
/// assert!(6 == cl(1)); | ||
/// ``` | ||
pub(crate) struct ConstFnClosure<'a, CapturedData: ?Sized, Function> { | ||
data: &'a CapturedData, | ||
func: Function, | ||
} | ||
impl<'a, CapturedData: ?Sized, Function> ConstFnClosure<'a, CapturedData, Function> { | ||
/// Function for creating a new closure. | ||
/// | ||
/// `data` is the a mutable borrow of data that is captured from the environment. | ||
/// | ||
/// `func` is the function of the closure, it gets the data and a tuple of the arguments closure | ||
/// and return the return value of the closure. | ||
#[allow(dead_code)] | ||
onestacked marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub(crate) const fn new<ClosureArguments, ClosureReturnValue>( | ||
data: &'a CapturedData, | ||
func: Function, | ||
) -> Self | ||
where | ||
Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue, | ||
{ | ||
Self { data, func } | ||
} | ||
} | ||
impl<'a, CapturedData: ?Sized, Function, ClosureArguments, ClosureReturnValue> const | ||
FnOnce<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function> | ||
where | ||
Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct, | ||
{ | ||
type Output = ClosureReturnValue; | ||
|
||
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output { | ||
self.call_mut(args) | ||
} | ||
} | ||
impl<'a, CapturedData: ?Sized, Function, ClosureArguments, ClosureReturnValue> const | ||
FnMut<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function> | ||
where | ||
Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue, | ||
{ | ||
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output { | ||
self.call(args) | ||
} | ||
} | ||
impl< | ||
'a, | ||
CapturedData: ?Sized, | ||
Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue, | ||
ClosureArguments, | ||
ClosureReturnValue, | ||
> const Fn<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function> | ||
{ | ||
extern "rust-call" fn call(&self, args: ClosureArguments) -> Self::Output { | ||
(self.func)(self.data, args) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.