struct Closure<'a> {
s : String,
t : &'a String,
}
impl<'a> FnOnce<()> for Closure<'a> {
type Output = String;
fn call_once(self, args : ()) -> Self::Output {
self.s += &*self.t;
self.s
}
}
This is one of the examples that I saw on internet and all of them didn't work for me.
I also try to fix lat rustc suggest to fix but still no success. I just give up and decide to ask how I can do that on rust stable.
Also is it possible to capture mutable reference within closure that is used as fnonce ?
The std::ops::FnOnce trait is unstable (the design isn't fully finalised) and you can only implement it with a nightly compiler and the unboxed_closures feature flag.
The compiler's error message explains in more detail and also links to a ticket on GitHub:
error[E0658]: the precise format of `Fn`-family traits' type parameters is subject to change
--> src/lib.rs:8:10
|
8 | impl<'a> FnOnce<()> for Closure<'a> {
| ^^^^^^^^^^ help: use parenthetical notation instead: `FnOnce() -> ()`
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
= help: add `#![feature(unboxed_closures)]` to the crate attributes to enable
Yes, normal closures can take a mutable reference to a captured variable.
fn main() {
let mut i = 0;
let mut closure = || {
let mutable_i: &mut u32 = &mut i;
*mutable_i += 1;
};
closure();
closure();
closure();
// drop the closure so `i` is no longer borrowed mutable
drop(closure);
// and now we can print the result
println!("i: {}", i);
}
it look like it is unstable feature and I should enable it manually
it allow me to make function that take only 1 argument except self
I can build it in the playground but I can't build it on my machine probably I should enable something
error[E0658]: rust-call ABI is subject to change
--> src/abstraction/piston_abstraction.rs:92:12
|
92 | extern "rust-call" fn call_once(mut self, _args : ()) -> Self::Output {
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
error[E0658]: rust-call ABI is subject to change
--> src/abstraction/piston_abstraction.rs:99:12
|
99 | extern "rust-call" fn call_mut(&mut self, _args : ()) -> Self::Output {
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
And I can't capture game as reference because window.draw_2d take closure of type fnonce. I didn't expect that it is not possible to implement that in rust
I suspect you're focusing on the wrong part of the error message -- FnOnce is not something you should normally need to implement manually. Can you share the full error?
error[E0507]: cannot move out of `*game` which is behind a shared reference
--> src/abstraction/piston_abstraction.rs:135:21
|
135 | game.render(&ctx);
| ^^^^ move occurs because `*game` has type `states::state_machine::StateMachine`, which does not implement the `Copy` trait