1 use crate::marker::Destruct;
3 /// Struct representing a closure with owned data.
7 /// use const_closure::ConstFnOnceClosure;
8 /// const fn imp(state: i32, (arg,): (i32,)) -> i32 {
12 /// let cl = ConstFnOnceClosure::new(i, imp);
14 /// assert!(7 == cl(2));
16 pub(crate) struct ConstFnOnceClosure<CapturedData, Function> {
20 impl<CapturedData, Function> ConstFnOnceClosure<CapturedData, Function> {
21 /// Function for creating a new closure.
23 /// `data` is the owned data that is captured from the environment (this data must be `~const Destruct`).
25 /// `func` is the function of the closure, it gets the data and a tuple of the arguments closure
26 /// and return the return value of the closure.
28 pub(crate) const fn new<ClosureArguments, ClosureReturnValue>(
33 CapturedData: ~const Destruct,
34 Function: ~const Fn(CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct,
39 impl<CapturedData, ClosureArguments, Function> const FnOnce<ClosureArguments>
40 for ConstFnOnceClosure<CapturedData, Function>
42 CapturedData: ~const Destruct,
43 Function: ~const Fn<(CapturedData, ClosureArguments)> + ~const Destruct,
45 type Output = Function::Output;
47 extern "rust-call" fn call_once(self, args: ClosureArguments) -> Self::Output {
48 (self.func)(self.data, args)
51 /// Struct representing a closure with mutably borrowed data.
55 /// #![feature(const_mut_refs)]
56 /// use const_closure::ConstFnMutClosure;
57 /// const fn imp(state: &mut i32, (arg,): (i32,)) -> i32 {
62 /// let mut cl = ConstFnMutClosure::new(&mut i, imp);
64 /// assert!(7 == cl(2));
65 /// assert!(8 == cl(1));
67 pub(crate) struct ConstFnMutClosure<'a, CapturedData: ?Sized, Function> {
68 data: &'a mut CapturedData,
71 impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<'a, CapturedData, Function> {
72 /// Function for creating a new closure.
74 /// `data` is the a mutable borrow of data that is captured from the environment.
76 /// `func` is the function of the closure, it gets the data and a tuple of the arguments closure
77 /// and return the return value of the closure.
78 pub(crate) const fn new<ClosureArguments, ClosureReturnValue>(
79 data: &'a mut CapturedData,
83 Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
88 impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const
89 FnOnce<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function>
92 ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct,
94 type Output = ClosureReturnValue;
96 extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
100 impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const
101 FnMut<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function>
103 Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
105 extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
106 (self.func)(self.data, args)
110 /// Struct representing a closure with borrowed data.
114 /// use const_closure::ConstFnClosure;
116 /// const fn imp(state: &i32, (arg,): (i32,)) -> i32 {
120 /// let cl = ConstFnClosure::new(&i, imp);
122 /// assert!(7 == cl(2));
123 /// assert!(6 == cl(1));
125 pub(crate) struct ConstFnClosure<'a, CapturedData: ?Sized, Function> {
126 data: &'a CapturedData,
129 impl<'a, CapturedData: ?Sized, Function> ConstFnClosure<'a, CapturedData, Function> {
130 /// Function for creating a new closure.
132 /// `data` is the a mutable borrow of data that is captured from the environment.
134 /// `func` is the function of the closure, it gets the data and a tuple of the arguments closure
135 /// and return the return value of the closure.
137 pub(crate) const fn new<ClosureArguments, ClosureReturnValue>(
138 data: &'a CapturedData,
142 Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue,
147 impl<'a, CapturedData: ?Sized, Function, ClosureArguments, ClosureReturnValue> const
148 FnOnce<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function>
150 Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct,
152 type Output = ClosureReturnValue;
154 extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
158 impl<'a, CapturedData: ?Sized, Function, ClosureArguments, ClosureReturnValue> const
159 FnMut<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function>
161 Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue,
163 extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
169 CapturedData: ?Sized,
170 Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue,
173 > const Fn<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function>
175 extern "rust-call" fn call(&self, args: ClosureArguments) -> Self::Output {
176 (self.func)(self.data, args)