I'm trying to shorten a function signature by introducing a type alias. The type specification of the parameter looks like this
writer: Arc<Mutex<&mut (dyn AsyncWrite + Unpin + Send + Sync)>> // working
which I thought could be shortened by introducing this type alias
pub type RefAsyncWriter<'a> = &'a mut (dyn AsyncWrite + Unpin + Send + Sync);
so the type parameter becomes just
writer: Arc<Mutex<RefAsyncWriter>> // error[E0726]: implicit elided lifetime not allowed here
Unfortunately this change gets me into trouble with lifetimes.
Here I made a brief example
use tokio::io::AsyncWrite;
use tokio::fs::File;
use std::sync::{Arc,Mutex};
use std::path::PathBuf;
pub type RefAsyncWriter<'a> = &'a mut (dyn AsyncWrite + Unpin + Send + Sync);
#[tokio::main]
async fn main() {
let f = File::create(PathBuf::from("/tmp/test.txt")).await.unwrap();
w(Arc::new(Mutex::new(&mut f)));
}
async fn w(writer: Arc<Mutex<RefAsyncWriter>>) {
// TODO
}
The result is the following
Compiling playground v0.0.1 (/playground)
error[E0726]: implicit elided lifetime not allowed here
--> src/main.rs:13:31
|
13 | async fn w (writer: Arc<Mutex<RefAsyncWriter>>) {}
| ^^^^^^^^^^^^^^- help: indicate the anonymous lifetime: `<'_>`
error: aborting due to previous error
If somebody could explain to me (1) if there is a way to get this working and maybe (2) why the type alias actually makes such a difference to the compiler that would be of great help.
Thanks a lot in advance.