You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#[deriving(Clone)]enumThing{Func(fn(&str) -> String)}#[allow(unused_variables)]fna(b:&str) -> String{"a".to_string()}#[allow(unused_variables)]fnmain(){let f = Thing::Func(a);println!("Good to go!");}
Yielding the error...
<anon>:3:10: 3:28 error: the trait `core::clone::Clone` is not implemented for the type`fn(&str) -> collections::string::String`<anon>:3 Func(fn(&str) -> String)
^~~~~~~~~~~~~~~~~~
note: in expansion of #[deriving]<anon>:1:1: 1:19 note: expansion site
However, it works when the argument reference is given a lifetime specifier (as below).
#[deriving(Clone)]enumThing{Func(fn(&'staticstr) -> String)}#[allow(unused_variables)]fna(b:&'staticstr) -> String{"a".to_string()}#[allow(unused_variables)]fnmain(){let f = Thing::Func(a);println!("Good to go!");}
Good to go!
Structs/enums cloneability should not rely on the lifetime of a bare function's arguments, since the argument's lifetime is relative to the function, not the struct.
I have got the same error, is this a bug or what? I just can not clone any function that takes a reference.
kmcallister
changed the title
Unable to copy enum/struct with bare function that accepts a reference as an argument
bare fn types fail to be Clone depending on parameter lifetimes
Jan 14, 2015
Note that if you contain types in the fn signature that are from your own crate, you can provide an explicit implementation yourself as a workaround. That doesn't solve this in the general case though, such as in the example in the first post, since neither &str nor String are from your own crate.
#[derive(Clone)]
enum Thing {
Func(fn(&str) -> String)
}
#[allow(unused_variables)]
fn a(b: &str) -> String {
"a".to_string()
}
#[allow(unused_variables)]
fn main() {
let f = Thing::Func(a);
println!("Good to go!");
}
Mark-Simulacrum
changed the title
bare fn types fail to be Clone depending on parameter lifetimes
fn pointer type is not Clone when containing a parameter with a unspecified lifetime
May 2, 2017
Mark-Simulacrum
changed the title
fn pointer type is not Clone when containing a parameter with a unspecified lifetime
fn pointer type is not Clone when containing a parameter with an unspecified lifetime
May 2, 2017
The following snippet fails to compile.
Yielding the error...
However, it works when the argument reference is given a lifetime specifier (as below).
Structs/enums cloneability should not rely on the lifetime of a bare function's arguments, since the argument's lifetime is relative to the function, not the struct.
Compiler version details:
The text was updated successfully, but these errors were encountered: