在try 有条件地创建future 时,为了以后与select_biased!
一起使用:
let mut heartbeat_timer = if let ServerState::Leader(_, _, _) = server_state {
// if we're the Leader, we want to send out AppendEntries RPC at a
// minimum interval as required to maintain our leadership
task::sleep(HEARTBEAT_DURATION)
} else {
// if we're not the leader, no need to send those
pending::<()>()
};
// ...
select_biased! {
h = heartbeat_timer => self.heartbeat().await,
// ... some others declared in priority order
default => task::yield_now().await,
}
我很难理解为什么会出现编译错误:
error[E0308]: `if` and `else` have incompatible types
--> src/raft.rs:185:17
|
182 | let heartbeat_timer = if let ServerState::Leader(_, _, _) = server_state {
| ___________________________________-
183 | | task::sleep(HEARTBEAT_DURATION)
| | ------------------------------- expected because of this
184 | | } else {
185 | | pending::<()>()
| | ^^^^^^^^^^^^^^^ expected future, found `Pending<()>`
186 | | };
| |_____________- `if` and `else` have incompatible types
|
= note: expected opaque type `impl futures::Future<Output = ()>`
found struct `futures::future::Pending<()>`
单击(在我的IDE中)pending
会显示它的类型是Pending<T>
,我还可以进一步看到Pending<T>
实现了Future<Output=T>
:
impl<T> Future for Pending<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<T> {
Poll::Pending
}
}
出于某种原因,我还不能理解,编译器没有遵循相同的面包屑.编译器知道我遗漏了什么秘密?我的代码有没有一些直接的转换可以满足编译器的要求?我应该怎么想才能提供编译器所需要的东西呢?
rustup --version
包括输出:
info: The currently active `rustc` version is `rustc 1.69.0 (84c898d65 2023-04-16)`