Return either std::sync::Mutex or tokio::sync::Mutex

I wrote a function to return either either std::sync::Mutex or tokio::sync::Mutex

// MUTEX can be either std::sync::Mutex or tokio::sync::Mutex
fn create_lock<MUTEX>() -> std::sync::Arc<MUTEX<i64>> {
    std::sync::Arc::new(MUTEX::new(0))
}

The compiler throws the following error:

| fn create_lock<MUTEX>() -> std::sync::Arc<MUTEX<i64>> {
|                                                 ^^^ type argument not allowed

Full code: Rust Playground

Any ideas? Thanks

There is a either create https://crates.io/crates/either, but it makes code quite ugly.

That would require higher kinded types. This is unsupported by rustc. As alternative you can rewrite it a bit to use generic associated types which are currently unstable.

GAT needs to create a trait and a struct, what I need is a pure function, I don't want to introduce a trait and a struct.

You need a trait either way to specify the amount of type arguments. You can't avoid the struct without higher kinded types which we don't support in favor of generic associated types.

Understood now, thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.