How to specify "missing generics" for type as a function parameter?

Viewed 40

I am trying to use a shared semaphore using futures_intrusive::sync::GenericSemaphore:

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

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

It seems like I need to "add missing generic argument", but I am not sure how to proceed. I have tried specifying a Mutex type and other things without success, but I think this probably has a straightforward answer.

1 Answers

You just need to add the generic type parameter that GenericSemaphore expects to the function signature.

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):

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