[ty] Remove unnecessary lifetimes for Task (#18261)

This commit is contained in:
Micha Reiser 2025-05-26 14:44:43 +02:00 committed by GitHub
parent d8216fa328
commit 175402aa75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 40 additions and 41 deletions

View file

@ -16,7 +16,7 @@ mod traits;
use self::traits::{NotificationHandler, RequestHandler}; use self::traits::{NotificationHandler, RequestHandler};
use super::{Result, client::Responder, schedule::BackgroundSchedule}; use super::{Result, client::Responder, schedule::BackgroundSchedule};
pub(super) fn request<'a>(req: server::Request) -> Task<'a> { pub(super) fn request(req: server::Request) -> Task {
let id = req.id.clone(); let id = req.id.clone();
match req.method.as_str() { match req.method.as_str() {
@ -61,7 +61,7 @@ pub(super) fn request<'a>(req: server::Request) -> Task<'a> {
}) })
} }
pub(super) fn notification<'a>(notif: server::Notification) -> Task<'a> { pub(super) fn notification(notif: server::Notification) -> Task {
match notif.method.as_str() { match notif.method.as_str() {
notifications::DidCloseTextDocumentHandler::METHOD => { notifications::DidCloseTextDocumentHandler::METHOD => {
local_notification_task::<notifications::DidCloseTextDocumentHandler>(notif) local_notification_task::<notifications::DidCloseTextDocumentHandler>(notif)
@ -100,9 +100,7 @@ pub(super) fn notification<'a>(notif: server::Notification) -> Task<'a> {
}) })
} }
fn _local_request_task<'a, R: traits::SyncRequestHandler>( fn _local_request_task<R: traits::SyncRequestHandler>(req: server::Request) -> super::Result<Task>
req: server::Request,
) -> super::Result<Task<'a>>
where where
<<R as RequestHandler>::RequestType as lsp_types::request::Request>::Params: UnwindSafe, <<R as RequestHandler>::RequestType as lsp_types::request::Request>::Params: UnwindSafe,
{ {
@ -114,10 +112,10 @@ where
})) }))
} }
fn background_request_task<'a, R: traits::BackgroundDocumentRequestHandler>( fn background_request_task<R: traits::BackgroundDocumentRequestHandler>(
req: server::Request, req: server::Request,
schedule: BackgroundSchedule, schedule: BackgroundSchedule,
) -> super::Result<Task<'a>> ) -> super::Result<Task>
where where
<<R as RequestHandler>::RequestType as lsp_types::request::Request>::Params: UnwindSafe, <<R as RequestHandler>::RequestType as lsp_types::request::Request>::Params: UnwindSafe,
{ {
@ -187,9 +185,9 @@ fn request_result_to_response<R>(
} }
} }
fn local_notification_task<'a, N: traits::SyncNotificationHandler>( fn local_notification_task<N: traits::SyncNotificationHandler>(
notif: server::Notification, notif: server::Notification,
) -> super::Result<Task<'a>> { ) -> super::Result<Task> {
let (id, params) = cast_notification::<N>(notif)?; let (id, params) = cast_notification::<N>(notif)?;
Ok(Task::local(move |session, notifier, requester, _| { Ok(Task::local(move |session, notifier, requester, _| {
let _span = tracing::debug_span!("notification", method = N::METHOD).entered(); let _span = tracing::debug_span!("notification", method = N::METHOD).entered();
@ -201,10 +199,10 @@ fn local_notification_task<'a, N: traits::SyncNotificationHandler>(
} }
#[expect(dead_code)] #[expect(dead_code)]
fn background_notification_thread<'a, N>( fn background_notification_thread<N>(
req: server::Notification, req: server::Notification,
schedule: BackgroundSchedule, schedule: BackgroundSchedule,
) -> super::Result<Task<'a>> ) -> super::Result<Task>
where where
N: traits::BackgroundDocumentNotificationHandler, N: traits::BackgroundDocumentNotificationHandler,
<<N as NotificationHandler>::NotificationType as lsp_types::notification::Notification>::Params: <<N as NotificationHandler>::NotificationType as lsp_types::notification::Notification>::Params:

View file

@ -6,12 +6,12 @@ use serde_json::Value;
use super::{ClientSender, schedule::Task}; use super::{ClientSender, schedule::Task};
type ResponseBuilder<'s> = Box<dyn FnOnce(lsp_server::Response) -> Task<'s>>; type ResponseBuilder = Box<dyn FnOnce(lsp_server::Response) -> Task>;
pub(crate) struct Client<'s> { pub(crate) struct Client {
notifier: Notifier, notifier: Notifier,
responder: Responder, responder: Responder,
pub(super) requester: Requester<'s>, pub(super) requester: Requester,
} }
#[derive(Clone)] #[derive(Clone)]
@ -20,13 +20,13 @@ pub(crate) struct Notifier(ClientSender);
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct Responder(ClientSender); pub(crate) struct Responder(ClientSender);
pub(crate) struct Requester<'s> { pub(crate) struct Requester {
sender: ClientSender, sender: ClientSender,
next_request_id: i32, next_request_id: i32,
response_handlers: FxHashMap<lsp_server::RequestId, ResponseBuilder<'s>>, response_handlers: FxHashMap<lsp_server::RequestId, ResponseBuilder>,
} }
impl Client<'_> { impl Client {
pub(super) fn new(sender: ClientSender) -> Self { pub(super) fn new(sender: ClientSender) -> Self {
Self { Self {
notifier: Notifier(sender.clone()), notifier: Notifier(sender.clone()),
@ -91,14 +91,14 @@ impl Responder {
} }
} }
impl<'s> Requester<'s> { impl Requester {
/// Sends a request of kind `R` to the client, with associated parameters. /// Sends a request of kind `R` to the client, with associated parameters.
/// The task provided by `response_handler` will be dispatched as soon as the response /// The task provided by `response_handler` will be dispatched as soon as the response
/// comes back from the client. /// comes back from the client.
pub(crate) fn request<R>( pub(crate) fn request<R>(
&mut self, &mut self,
params: R::Params, params: R::Params,
response_handler: impl Fn(R::Result) -> Task<'s> + 'static, response_handler: impl Fn(R::Result) -> Task + 'static,
) -> crate::Result<()> ) -> crate::Result<()>
where where
R: lsp_types::request::Request, R: lsp_types::request::Request,
@ -155,7 +155,7 @@ impl<'s> Requester<'s> {
Ok(()) Ok(())
} }
pub(crate) fn pop_response_task(&mut self, response: lsp_server::Response) -> Task<'s> { pub(crate) fn pop_response_task(&mut self, response: lsp_server::Response) -> Task {
if let Some(handler) = self.response_handlers.remove(&response.id) { if let Some(handler) = self.response_handlers.remove(&response.id) {
handler(response) handler(response)
} else { } else {

View file

@ -34,7 +34,7 @@ pub(crate) fn event_loop_thread(
pub(crate) struct Scheduler<'s> { pub(crate) struct Scheduler<'s> {
session: &'s mut Session, session: &'s mut Session,
client: Client<'s>, client: Client,
fmt_pool: thread::Pool, fmt_pool: thread::Pool,
background_pool: thread::Pool, background_pool: thread::Pool,
} }
@ -60,7 +60,7 @@ impl<'s> Scheduler<'s> {
pub(super) fn request<R>( pub(super) fn request<R>(
&mut self, &mut self,
params: R::Params, params: R::Params,
response_handler: impl Fn(R::Result) -> Task<'s> + 'static, response_handler: impl Fn(R::Result) -> Task + 'static,
) -> crate::Result<()> ) -> crate::Result<()>
where where
R: lsp_types::request::Request, R: lsp_types::request::Request,
@ -69,13 +69,13 @@ impl<'s> Scheduler<'s> {
} }
/// Creates a task to handle a response from the client. /// Creates a task to handle a response from the client.
pub(super) fn response(&mut self, response: lsp_server::Response) -> Task<'s> { pub(super) fn response(&mut self, response: lsp_server::Response) -> Task {
self.client.requester.pop_response_task(response) self.client.requester.pop_response_task(response)
} }
/// Dispatches a `task` by either running it as a blocking function or /// Dispatches a `task` by either running it as a blocking function or
/// executing it on a background thread pool. /// executing it on a background thread pool.
pub(super) fn dispatch(&mut self, task: task::Task<'s>) { pub(super) fn dispatch(&mut self, task: task::Task) {
match task { match task {
Task::Sync(SyncTask { func }) => { Task::Sync(SyncTask { func }) => {
let notifier = self.client.notifier(); let notifier = self.client.notifier();

View file

@ -6,11 +6,11 @@ use crate::{
session::Session, session::Session,
}; };
type LocalFn<'s> = Box<dyn FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 's>; type LocalFn = Box<dyn FnOnce(&mut Session, Notifier, &mut Requester, Responder)>;
type BackgroundFn = Box<dyn FnOnce(Notifier, Responder) + Send + 'static>; type BackgroundFn = Box<dyn FnOnce(Notifier, Responder) + Send + 'static>;
type BackgroundFnBuilder<'s> = Box<dyn FnOnce(&Session) -> BackgroundFn + 's>; type BackgroundFnBuilder = Box<dyn FnOnce(&Session) -> BackgroundFn>;
/// Describes how the task should be run. /// Describes how the task should be run.
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default)]
@ -36,9 +36,9 @@ pub(in crate::server) enum BackgroundSchedule {
/// while local tasks have exclusive access and can modify it as they please. Keep in mind that /// while local tasks have exclusive access and can modify it as they please. Keep in mind that
/// local tasks will **block** the main event loop, so only use local tasks if you **need** /// local tasks will **block** the main event loop, so only use local tasks if you **need**
/// mutable state access or you need the absolute lowest latency possible. /// mutable state access or you need the absolute lowest latency possible.
pub(in crate::server) enum Task<'s> { pub(in crate::server) enum Task {
Background(BackgroundTaskBuilder<'s>), Background(BackgroundTaskBuilder),
Sync(SyncTask<'s>), Sync(SyncTask),
} }
// The reason why this isn't just a 'static background closure // The reason why this isn't just a 'static background closure
@ -49,30 +49,31 @@ pub(in crate::server) enum Task<'s> {
// that the inner closure can capture. This builder closure has a lifetime linked to the scheduler. // that the inner closure can capture. This builder closure has a lifetime linked to the scheduler.
// When the task is dispatched, the scheduler runs the synchronous builder, which takes the session // When the task is dispatched, the scheduler runs the synchronous builder, which takes the session
// as a reference, to create the inner 'static closure. That closure is then moved to a background task pool. // as a reference, to create the inner 'static closure. That closure is then moved to a background task pool.
pub(in crate::server) struct BackgroundTaskBuilder<'s> { pub(in crate::server) struct BackgroundTaskBuilder {
pub(super) schedule: BackgroundSchedule, pub(super) schedule: BackgroundSchedule,
pub(super) builder: BackgroundFnBuilder<'s>, pub(super) builder: BackgroundFnBuilder,
} }
pub(in crate::server) struct SyncTask<'s> { pub(in crate::server) struct SyncTask {
pub(super) func: LocalFn<'s>, pub(super) func: LocalFn,
} }
impl<'s> Task<'s> { impl Task {
/// Creates a new background task. /// Creates a new background task.
pub(crate) fn background( pub(crate) fn background<F>(schedule: BackgroundSchedule, func: F) -> Self
schedule: BackgroundSchedule, where
func: impl FnOnce(&Session) -> Box<dyn FnOnce(Notifier, Responder) + Send + 'static> + 's, F: FnOnce(&Session) -> Box<dyn FnOnce(Notifier, Responder) + Send + 'static> + 'static,
) -> Self { {
Self::Background(BackgroundTaskBuilder { Self::Background(BackgroundTaskBuilder {
schedule, schedule,
builder: Box::new(func), builder: Box::new(func),
}) })
} }
/// Creates a new local task. /// Creates a new local task.
pub(crate) fn local( pub(crate) fn local<F>(func: F) -> Self
func: impl FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 's, where
) -> Self { F: FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 'static,
{
Self::Sync(SyncTask { Self::Sync(SyncTask {
func: Box::new(func), func: Box::new(func),
}) })