简体   繁体   English

在 Rust 中将其作为函数参数传递时如何为 futures_intrusive::sync::GenericSemaphore 指定类型

[英]How to specify type to a futures_intrusive::sync::GenericSemaphore when passing it down as a function parameter in Rust

I am trying to implement a shared semaphore using futures_intrusive::sync::GenericSemaphore .我正在尝试使用futures_intrusive::sync::GenericSemaphore实现共享信号量。 The code looks like this:代码如下所示:

use std::path::Path;
use std::fmt::Write;

use futures_intrusive::sync::GenericSemaphore;


mod linear_bathtubbing

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?;
    }
}

#[tokio::main]
async fn main() {
    let semaphore = GenericSemaphore::new(false, 100);
    for raster_data in RASTER_INPUTS.iter() {
        let [state, raster_file, chunk_size] = raster_data;
        let chunk_size: u32 = chunk_size.parse().unwrap();
        let raster_filename: Vec<&str> = raster_file.split("/").collect();
        for slr_level in SLR_LEVELS.iter() {
            let mut output_filename = String::from("slr_dems/linear/");
            output_filename += state;
            output_filename += "/slr";
            write!(output_filename, "{:1.1}", slr_level).unwrap();
            write!(output_filename, "_{}", raster_filename[raster_filename.len() -1]).unwrap();
            linear_bathtubbing::refresh_file(Path::new(&raster_file), *slr_level, Path::new(&output_filename), chunk_size, &semaphore).await?;
        };
    }
}

However I get an error:但是我收到一个错误:


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>) {
    |                                                                                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0107`.

It seems like I need to add missing generic argument , but I am not sure how to proceed.似乎我需要add missing generic argument ,但我不确定如何继续。 I have tried specifying a Mutex type and other things without success, but I think this probably has a straightforward answer which I simply don't know because of lack of experience with Rust.我曾尝试指定Mutex类型和其他东西但没有成功,但我认为这可能有一个简单的答案,由于缺乏 Rust 经验,我根本不知道。

You just need to add the generic type parameter that GenericSemaphore expects to the function signature.您只需将GenericSemaphore期望的泛型类型参数添加到函数签名中。

async fn refresh_file<T: RawMutex>(
    raster_file: &Path,
    slr: f64,
    output_path: &Path,
    chunk_size: u32,
    semaphore: &GenericSemaphore<T>
) {

Alternatively, using impl syntax (which is sugar for the code above):或者,使用impl语法(这是上面代码的糖):

async fn refresh_file(
    raster_file: &Path,
    slr: f64,
    output_path: &Path,
    chunk_size: u32,
    semaphore: &GenericSemaphore<impl RawMutex>
) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM