将方法引用传递给结构
Passing in method reference to a struct
我有一个结构other_struct
,它有一堆我需要根据特定情况调用的方法(在这个例子中只有foo()
。我想要一个字段在 other_struct
中称为 fmap
,它存储 other_struct
方法的 HashMap。
use std::collections::HashMap;
pub struct fn_struct {
pub func: Option<fn(&other_struct) -> ()>,
}
pub struct other_struct<'a> {
fmap: HashMap<String, fn_struct>,
some_str: &'a str,
}
impl<'a> other_struct<'a> {
fn new(some_str: &str) -> other_struct {
let mut new_struct = other_struct {
fmap: HashMap::new(),
some_str: some_str,
};
new_struct.fmap.insert(
String::from("foo"),
fn_struct {
func: Some(other_struct::foo),
},
);
new_struct
}
pub fn foo(&self) {
println!("Do some stuff foo");
}
}
fn main() {
let test_str = "test";
let mut new_o = other_struct::new(test_str);
new_o.fmap.get("foo").unwrap().func.unwrap()(&new_o);
}
我正在努力处理生命周期,因为我收到以下错误:
error[E0308]: mismatched types
--> src/main.rs:22:28
|
22 | func: Some(other_struct::foo),
| ^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r, 's> fn(&'r other_struct<'s>)`
found fn pointer `for<'r> fn(&'r other_struct<'_>)`
我一直在阅读排名靠前的 trait bound 文档,但我不清楚这里发生了什么。这是否意味着编译器希望我以某种方式指定 other_struct
实例的生命周期与方法指针的生命周期相关?
fn_struct
的 fn(&other_struct) -> ()
部分是一个函数指针,必须能够接受任何生命周期 for<'r, 's> fn(&'r other_struct<'s>) -> ()
的 other_struct
。但是,other_struct::foo
只接受 other_struct<'_>
.
的特定生命周期
您可以通过以某种方式指定该生命周期来修复它,这里基本上是说它总是会自行传递:
pub struct fn_struct<'a> {
pub func: Option<fn(&other_struct<'a>) -> ()>,
}
pub struct other_struct<'a> {
fmap: HashMap<String, fn_struct<'a>>,
some_str: &'a str,
}
或者通过解除 'a
的绑定,使 foo
更通用。
pub fn foo(_self: &other_struct) {
println!("Do some stuff foo");
}
差异很小,但原始版本是 other_struct<'a>::foo<'b>(&'b other_struct<'a>) -> ()
,固定版本是 other_struct<'a>::foo<'b, 'c>(&'b other_struct<'c>) -> ()
。
我有一个结构other_struct
,它有一堆我需要根据特定情况调用的方法(在这个例子中只有foo()
。我想要一个字段在 other_struct
中称为 fmap
,它存储 other_struct
方法的 HashMap。
use std::collections::HashMap;
pub struct fn_struct {
pub func: Option<fn(&other_struct) -> ()>,
}
pub struct other_struct<'a> {
fmap: HashMap<String, fn_struct>,
some_str: &'a str,
}
impl<'a> other_struct<'a> {
fn new(some_str: &str) -> other_struct {
let mut new_struct = other_struct {
fmap: HashMap::new(),
some_str: some_str,
};
new_struct.fmap.insert(
String::from("foo"),
fn_struct {
func: Some(other_struct::foo),
},
);
new_struct
}
pub fn foo(&self) {
println!("Do some stuff foo");
}
}
fn main() {
let test_str = "test";
let mut new_o = other_struct::new(test_str);
new_o.fmap.get("foo").unwrap().func.unwrap()(&new_o);
}
我正在努力处理生命周期,因为我收到以下错误:
error[E0308]: mismatched types
--> src/main.rs:22:28
|
22 | func: Some(other_struct::foo),
| ^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r, 's> fn(&'r other_struct<'s>)`
found fn pointer `for<'r> fn(&'r other_struct<'_>)`
我一直在阅读排名靠前的 trait bound 文档,但我不清楚这里发生了什么。这是否意味着编译器希望我以某种方式指定 other_struct
实例的生命周期与方法指针的生命周期相关?
fn_struct
的 fn(&other_struct) -> ()
部分是一个函数指针,必须能够接受任何生命周期 for<'r, 's> fn(&'r other_struct<'s>) -> ()
的 other_struct
。但是,other_struct::foo
只接受 other_struct<'_>
.
您可以通过以某种方式指定该生命周期来修复它,这里基本上是说它总是会自行传递:
pub struct fn_struct<'a> {
pub func: Option<fn(&other_struct<'a>) -> ()>,
}
pub struct other_struct<'a> {
fmap: HashMap<String, fn_struct<'a>>,
some_str: &'a str,
}
或者通过解除 'a
的绑定,使 foo
更通用。
pub fn foo(_self: &other_struct) {
println!("Do some stuff foo");
}
差异很小,但原始版本是 other_struct<'a>::foo<'b>(&'b other_struct<'a>) -> ()
,固定版本是 other_struct<'a>::foo<'b, 'c>(&'b other_struct<'c>) -> ()
。