Skip to content

Commit 175402a

Browse files
authored
[ty] Remove unnecessary lifetimes for Task (#18261)
1 parent d8216fa commit 175402a

File tree

4 files changed

+40
-41
lines changed

4 files changed

+40
-41
lines changed

crates/ty_server/src/server/api.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod traits;
1616
use self::traits::{NotificationHandler, RequestHandler};
1717
use super::{Result, client::Responder, schedule::BackgroundSchedule};
1818

19-
pub(super) fn request<'a>(req: server::Request) -> Task<'a> {
19+
pub(super) fn request(req: server::Request) -> Task {
2020
let id = req.id.clone();
2121

2222
match req.method.as_str() {
@@ -61,7 +61,7 @@ pub(super) fn request<'a>(req: server::Request) -> Task<'a> {
6161
})
6262
}
6363

64-
pub(super) fn notification<'a>(notif: server::Notification) -> Task<'a> {
64+
pub(super) fn notification(notif: server::Notification) -> Task {
6565
match notif.method.as_str() {
6666
notifications::DidCloseTextDocumentHandler::METHOD => {
6767
local_notification_task::<notifications::DidCloseTextDocumentHandler>(notif)
@@ -100,9 +100,7 @@ pub(super) fn notification<'a>(notif: server::Notification) -> Task<'a> {
100100
})
101101
}
102102

103-
fn _local_request_task<'a, R: traits::SyncRequestHandler>(
104-
req: server::Request,
105-
) -> super::Result<Task<'a>>
103+
fn _local_request_task<R: traits::SyncRequestHandler>(req: server::Request) -> super::Result<Task>
106104
where
107105
<<R as RequestHandler>::RequestType as lsp_types::request::Request>::Params: UnwindSafe,
108106
{
@@ -114,10 +112,10 @@ where
114112
}))
115113
}
116114

117-
fn background_request_task<'a, R: traits::BackgroundDocumentRequestHandler>(
115+
fn background_request_task<R: traits::BackgroundDocumentRequestHandler>(
118116
req: server::Request,
119117
schedule: BackgroundSchedule,
120-
) -> super::Result<Task<'a>>
118+
) -> super::Result<Task>
121119
where
122120
<<R as RequestHandler>::RequestType as lsp_types::request::Request>::Params: UnwindSafe,
123121
{
@@ -187,9 +185,9 @@ fn request_result_to_response<R>(
187185
}
188186
}
189187

190-
fn local_notification_task<'a, N: traits::SyncNotificationHandler>(
188+
fn local_notification_task<N: traits::SyncNotificationHandler>(
191189
notif: server::Notification,
192-
) -> super::Result<Task<'a>> {
190+
) -> super::Result<Task> {
193191
let (id, params) = cast_notification::<N>(notif)?;
194192
Ok(Task::local(move |session, notifier, requester, _| {
195193
let _span = tracing::debug_span!("notification", method = N::METHOD).entered();
@@ -201,10 +199,10 @@ fn local_notification_task<'a, N: traits::SyncNotificationHandler>(
201199
}
202200

203201
#[expect(dead_code)]
204-
fn background_notification_thread<'a, N>(
202+
fn background_notification_thread<N>(
205203
req: server::Notification,
206204
schedule: BackgroundSchedule,
207-
) -> super::Result<Task<'a>>
205+
) -> super::Result<Task>
208206
where
209207
N: traits::BackgroundDocumentNotificationHandler,
210208
<<N as NotificationHandler>::NotificationType as lsp_types::notification::Notification>::Params:

crates/ty_server/src/server/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use serde_json::Value;
66

77
use super::{ClientSender, schedule::Task};
88

9-
type ResponseBuilder<'s> = Box<dyn FnOnce(lsp_server::Response) -> Task<'s>>;
9+
type ResponseBuilder = Box<dyn FnOnce(lsp_server::Response) -> Task>;
1010

11-
pub(crate) struct Client<'s> {
11+
pub(crate) struct Client {
1212
notifier: Notifier,
1313
responder: Responder,
14-
pub(super) requester: Requester<'s>,
14+
pub(super) requester: Requester,
1515
}
1616

1717
#[derive(Clone)]
@@ -20,13 +20,13 @@ pub(crate) struct Notifier(ClientSender);
2020
#[derive(Clone)]
2121
pub(crate) struct Responder(ClientSender);
2222

23-
pub(crate) struct Requester<'s> {
23+
pub(crate) struct Requester {
2424
sender: ClientSender,
2525
next_request_id: i32,
26-
response_handlers: FxHashMap<lsp_server::RequestId, ResponseBuilder<'s>>,
26+
response_handlers: FxHashMap<lsp_server::RequestId, ResponseBuilder>,
2727
}
2828

29-
impl Client<'_> {
29+
impl Client {
3030
pub(super) fn new(sender: ClientSender) -> Self {
3131
Self {
3232
notifier: Notifier(sender.clone()),
@@ -91,14 +91,14 @@ impl Responder {
9191
}
9292
}
9393

94-
impl<'s> Requester<'s> {
94+
impl Requester {
9595
/// Sends a request of kind `R` to the client, with associated parameters.
9696
/// The task provided by `response_handler` will be dispatched as soon as the response
9797
/// comes back from the client.
9898
pub(crate) fn request<R>(
9999
&mut self,
100100
params: R::Params,
101-
response_handler: impl Fn(R::Result) -> Task<'s> + 'static,
101+
response_handler: impl Fn(R::Result) -> Task + 'static,
102102
) -> crate::Result<()>
103103
where
104104
R: lsp_types::request::Request,
@@ -155,7 +155,7 @@ impl<'s> Requester<'s> {
155155
Ok(())
156156
}
157157

158-
pub(crate) fn pop_response_task(&mut self, response: lsp_server::Response) -> Task<'s> {
158+
pub(crate) fn pop_response_task(&mut self, response: lsp_server::Response) -> Task {
159159
if let Some(handler) = self.response_handlers.remove(&response.id) {
160160
handler(response)
161161
} else {

crates/ty_server/src/server/schedule.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn event_loop_thread(
3434

3535
pub(crate) struct Scheduler<'s> {
3636
session: &'s mut Session,
37-
client: Client<'s>,
37+
client: Client,
3838
fmt_pool: thread::Pool,
3939
background_pool: thread::Pool,
4040
}
@@ -60,7 +60,7 @@ impl<'s> Scheduler<'s> {
6060
pub(super) fn request<R>(
6161
&mut self,
6262
params: R::Params,
63-
response_handler: impl Fn(R::Result) -> Task<'s> + 'static,
63+
response_handler: impl Fn(R::Result) -> Task + 'static,
6464
) -> crate::Result<()>
6565
where
6666
R: lsp_types::request::Request,
@@ -69,13 +69,13 @@ impl<'s> Scheduler<'s> {
6969
}
7070

7171
/// Creates a task to handle a response from the client.
72-
pub(super) fn response(&mut self, response: lsp_server::Response) -> Task<'s> {
72+
pub(super) fn response(&mut self, response: lsp_server::Response) -> Task {
7373
self.client.requester.pop_response_task(response)
7474
}
7575

7676
/// Dispatches a `task` by either running it as a blocking function or
7777
/// executing it on a background thread pool.
78-
pub(super) fn dispatch(&mut self, task: task::Task<'s>) {
78+
pub(super) fn dispatch(&mut self, task: task::Task) {
7979
match task {
8080
Task::Sync(SyncTask { func }) => {
8181
let notifier = self.client.notifier();

crates/ty_server/src/server/schedule/task.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::{
66
session::Session,
77
};
88

9-
type LocalFn<'s> = Box<dyn FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 's>;
9+
type LocalFn = Box<dyn FnOnce(&mut Session, Notifier, &mut Requester, Responder)>;
1010

1111
type BackgroundFn = Box<dyn FnOnce(Notifier, Responder) + Send + 'static>;
1212

13-
type BackgroundFnBuilder<'s> = Box<dyn FnOnce(&Session) -> BackgroundFn + 's>;
13+
type BackgroundFnBuilder = Box<dyn FnOnce(&Session) -> BackgroundFn>;
1414

1515
/// Describes how the task should be run.
1616
#[derive(Clone, Copy, Debug, Default)]
@@ -36,9 +36,9 @@ pub(in crate::server) enum BackgroundSchedule {
3636
/// while local tasks have exclusive access and can modify it as they please. Keep in mind that
3737
/// local tasks will **block** the main event loop, so only use local tasks if you **need**
3838
/// mutable state access or you need the absolute lowest latency possible.
39-
pub(in crate::server) enum Task<'s> {
40-
Background(BackgroundTaskBuilder<'s>),
41-
Sync(SyncTask<'s>),
39+
pub(in crate::server) enum Task {
40+
Background(BackgroundTaskBuilder),
41+
Sync(SyncTask),
4242
}
4343

4444
// The reason why this isn't just a 'static background closure
@@ -49,30 +49,31 @@ pub(in crate::server) enum Task<'s> {
4949
// that the inner closure can capture. This builder closure has a lifetime linked to the scheduler.
5050
// When the task is dispatched, the scheduler runs the synchronous builder, which takes the session
5151
// as a reference, to create the inner 'static closure. That closure is then moved to a background task pool.
52-
pub(in crate::server) struct BackgroundTaskBuilder<'s> {
52+
pub(in crate::server) struct BackgroundTaskBuilder {
5353
pub(super) schedule: BackgroundSchedule,
54-
pub(super) builder: BackgroundFnBuilder<'s>,
54+
pub(super) builder: BackgroundFnBuilder,
5555
}
5656

57-
pub(in crate::server) struct SyncTask<'s> {
58-
pub(super) func: LocalFn<'s>,
57+
pub(in crate::server) struct SyncTask {
58+
pub(super) func: LocalFn,
5959
}
6060

61-
impl<'s> Task<'s> {
61+
impl Task {
6262
/// Creates a new background task.
63-
pub(crate) fn background(
64-
schedule: BackgroundSchedule,
65-
func: impl FnOnce(&Session) -> Box<dyn FnOnce(Notifier, Responder) + Send + 'static> + 's,
66-
) -> Self {
63+
pub(crate) fn background<F>(schedule: BackgroundSchedule, func: F) -> Self
64+
where
65+
F: FnOnce(&Session) -> Box<dyn FnOnce(Notifier, Responder) + Send + 'static> + 'static,
66+
{
6767
Self::Background(BackgroundTaskBuilder {
6868
schedule,
6969
builder: Box::new(func),
7070
})
7171
}
7272
/// Creates a new local task.
73-
pub(crate) fn local(
74-
func: impl FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 's,
75-
) -> Self {
73+
pub(crate) fn local<F>(func: F) -> Self
74+
where
75+
F: FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 'static,
76+
{
7677
Self::Sync(SyncTask {
7778
func: Box::new(func),
7879
})

0 commit comments

Comments
 (0)