gpt4 book ai didi

rust - 指向带有生命周期参数的方法的函数指针的类型

转载 作者:行者123 更新时间:2023-12-03 11:26:29 26 4
gpt4 key购买 nike

我有一个带有生命周期和一些方法的结构:

struct Ctrl<'a> {
x: &'a i32,
}

impl<'a> Ctrl<'a> {
fn foo(&self) -> i32 {
*self.x + 1
}
fn bar(&self) -> i32 {
*self.x + 2
}
}

现在我想将指向方法的指针存储在一个切片中,有点像查找表:

const LIST: &[_] = &[Ctrl::foo, Ctrl::bar];

Rust 要求知道切片元素类型并建议 for<'r> fn(&'r Ctrl) -> i32 ,但这会导致错误:

error[E0308]: mismatched types
--> main.rs:16:48
|
16 | const LIST: &[for<'r> fn(&'r Ctrl) -> i32] = &[Ctrl::foo, Ctrl::bar];
| ^^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'s, 'r> fn(&'r Ctrl<'s>) -> _`
found fn pointer `for<'r> fn(&'r Ctrl<'_>) -> _`

有没有办法指定正确的类型?

最佳答案

问题是 fooCtrl<'x> 的一种方法对于一些特定的生命周期'x ,而不是一个通用的方法Ctrl<'_> .如果你试图让所有的生命周期都显式化,你会发现有些生命周期是无法表达的:

const LIST: &[for<'a, 'b> fn(&'b Ctrl<'a>) -> i32] = &[Ctrl::<'?>::foo, Ctrl::<'?>::bar)];
// What lifetimes? ^^^^ ^^^^

foo不是通用的,但绑定(bind)到特定的 Ctrl<'_> ,没有办法表达你需要的概念。你可以尝试像 <for<'a> Ctrl<'a>>::foo 这样的东西,但这在当前的 Rust 中是不合法的。

解决这个问题的一种方法是将每个方法包装在一个可以强制转换为函数指针的闭包中:

const LIST: &[fn(&Ctrl<'_>) -> i32] = &[|this| this.foo(), |this| this.bar()];

如果您有很多方法或签名更复杂,这可能会有点冗长。另一种选择是将每个方法包装在一个具有正确签名的自由函数中。您可以编写声明性宏以使其更容易:

/// Wrap `Ctrl::$method` in a free function and return it.
macro_rules! ctrl {
($method:ident) => {{
fn inner(this: &Ctrl<'_>) -> i32 {
this.$method()
}
inner
}};
}

const LIST: &[fn(&Ctrl<'_>) -> i32] = &[ctrl!(foo), ctrl!(bar)];

关于rust - 指向带有生命周期参数的方法的函数指针的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64102352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com