首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将类型的“缺失泛型”指定为函数参数?

如何将类型的“缺失泛型”指定为函数参数?
EN

Stack Overflow用户
提问于 2022-06-11 18:00:21
回答 1查看 143关注 0票数 1

我试图使用一个共享的信号量使用futures_intrusive::sync::GenericSemaphore

代码语言:javascript
复制
use std::path::Path;
use futures_intrusive::sync::GenericSemaphore;

async fn refresh_file(raster_file: &Path, slr: f64, output_path: &Path, chunk_size: u32, semaphore: &GenericSemaphore) {
    if output_path.exists().not() {
        build_required_windows(&raster_file, slr, chunk_size, output_path, &semaphore).await?;
    }
}

然而,我得到了一个错误:

代码语言:javascript
复制
error[E0107]: missing generics for struct `GenericSemaphore`
   --> src/linear_bathtubbing.rs:129:106
    |
129 | pub async fn refresh_file(raster_file: &Path, slr: f64, output_path: &Path, chunk_size: u32, semaphore: &GenericSemaphore) {
    |                                                                                                          ^^^^^^^^^^^^^^^^ expected 1 generic argument
    |
note: struct defined here, with 1 generic parameter: `MutexType`
   --> /.../.cargo/registry/src/github.com-1ecc6299db9ec823/futures-intrusive-0.4.0/src/sync/semaphore.rs:433:12
    |
433 | pub struct GenericSemaphore<MutexType: RawMutex> {
    |            ^^^^^^^^^^^^^^^^ ---------
help: add missing generic argument
    |
129 | pub async fn refresh_file(raster_file: &Path, slr: f64, output_path: &Path, chunk_size: u32, semaphore: &GenericSemaphore<MutexType>) {
    |                                                                                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~

似乎我需要“添加缺少的泛型参数”,但我不知道如何继续。我尝试过指定Mutex类型和其他东西,但没有成功,但我认为这可能有一个简单的答案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-11 20:25:08

只需将GenericSemaphore期望的泛型类型参数添加到函数签名中即可。

代码语言:javascript
复制
async fn refresh_file<T: RawMutex>(
    raster_file: &Path,
    slr: f64,
    output_path: &Path,
    chunk_size: u32,
    semaphore: &GenericSemaphore<T>
) {

或者,使用impl语法(对于上面的代码来说,这是糖):

代码语言:javascript
复制
async fn refresh_file(
    raster_file: &Path,
    slr: f64,
    output_path: &Path,
    chunk_size: u32,
    semaphore: &GenericSemaphore<impl RawMutex>
) {
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72586756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档