Skip to content

Commit 3da67b6

Browse files
authored
Merge pull request #107 from taiki-e/impl-lifetime
Replace elided lifetimes to explicit lifetimes in trait impl
2 parents 7c746b6 + a9858d2 commit 3da67b6

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/expand.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ pub fn expand(input: &mut Item, is_local: bool) {
8181
}
8282
}
8383
Item::Impl(input) => {
84+
let mut lifetimes = CollectLifetimes::with("'impl_life");
85+
lifetimes.visit_type_mut(&mut *input.self_ty);
86+
lifetimes.visit_path_mut(&mut input.trait_.as_mut().unwrap().1);
87+
let params = &input.generics.params;
88+
let elided = lifetimes.elided;
89+
input.generics.params = parse_quote!( #(#elided,)* #params );
90+
8491
let context = Context::Impl {
8592
impl_generics: &input.generics,
8693
receiver: &input.self_ty,

src/lifetime.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,23 @@ impl VisitMut for HasAsyncLifetime {
2424
pub struct CollectLifetimes {
2525
pub elided: Vec<Lifetime>,
2626
pub explicit: Vec<Lifetime>,
27+
pub name: &'static str,
2728
}
2829

2930
impl CollectLifetimes {
3031
pub fn new() -> Self {
3132
CollectLifetimes {
3233
elided: Vec::new(),
3334
explicit: Vec::new(),
35+
name: "'life",
36+
}
37+
}
38+
39+
pub fn with(name: &'static str) -> Self {
40+
CollectLifetimes {
41+
elided: Vec::new(),
42+
explicit: Vec::new(),
43+
name,
3444
}
3545
}
3646

@@ -50,7 +60,7 @@ impl CollectLifetimes {
5060
}
5161

5262
fn next_lifetime(&mut self) -> Lifetime {
53-
let name = format!("'life{}", self.elided.len());
63+
let name = format!("{}{}", self.name, self.elided.len());
5464
let life = Lifetime::new(&name, Span::call_site());
5565
self.elided.push(life.clone());
5666
life

tests/test.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,3 +905,35 @@ mod issue104 {
905905

906906
impl_t1!(Foo, 1);
907907
}
908+
909+
// https://github.com/dtolnay/async-trait/issues/106
910+
mod issue106 {
911+
use async_trait::async_trait;
912+
use std::future::Future;
913+
914+
#[async_trait]
915+
pub trait ProcessPool: Send + Sync {
916+
type ThreadPool;
917+
918+
async fn spawn<F, Fut, T>(&self, work: F) -> T
919+
where
920+
F: FnOnce(&Self::ThreadPool) -> Fut + Send,
921+
Fut: Future<Output = T> + 'static;
922+
}
923+
924+
#[async_trait]
925+
impl<P: ?Sized> ProcessPool for &P
926+
where
927+
P: ProcessPool,
928+
{
929+
type ThreadPool = P::ThreadPool;
930+
931+
async fn spawn<F, Fut, T>(&self, work: F) -> T
932+
where
933+
F: FnOnce(&Self::ThreadPool) -> Fut + Send,
934+
Fut: Future<Output = T> + 'static,
935+
{
936+
(*self).spawn(work).await
937+
}
938+
}
939+
}

0 commit comments

Comments
 (0)