[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 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();
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() {
notifications::DidCloseTextDocumentHandler::METHOD => {
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>(
req: server::Request,
) -> super::Result<Task<'a>>
fn _local_request_task<R: traits::SyncRequestHandler>(req: server::Request) -> super::Result<Task>
where
<<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,
schedule: BackgroundSchedule,
) -> super::Result<Task<'a>>
) -> super::Result<Task>
where
<<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,
) -> super::Result<Task<'a>> {
) -> super::Result<Task> {
let (id, params) = cast_notification::<N>(notif)?;
Ok(Task::local(move |session, notifier, requester, _| {
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)]
fn background_notification_thread<'a, N>(
fn background_notification_thread<N>(
req: server::Notification,
schedule: BackgroundSchedule,
) -> super::Result<Task<'a>>
) -> super::Result<Task>
where
N: traits::BackgroundDocumentNotificationHandler,
<<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};
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,
responder: Responder,
pub(super) requester: Requester<'s>,
pub(super) requester: Requester,
}
#[derive(Clone)]
@ -20,13 +20,13 @@ pub(crate) struct Notifier(ClientSender);
#[derive(Clone)]
pub(crate) struct Responder(ClientSender);
pub(crate) struct Requester<'s> {
pub(crate) struct Requester {
sender: ClientSender,
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 {
Self {
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.
/// The task provided by `response_handler` will be dispatched as soon as the response
/// comes back from the client.
pub(crate) fn request<R>(
&mut self,
params: R::Params,
response_handler: impl Fn(R::Result) -> Task<'s> + 'static,
response_handler: impl Fn(R::Result) -> Task + 'static,
) -> crate::Result<()>
where
R: lsp_types::request::Request,
@ -155,7 +155,7 @@ impl<'s> Requester<'s> {
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) {
handler(response)
} else {

View file

@ -34,7 +34,7 @@ pub(crate) fn event_loop_thread(
pub(crate) struct Scheduler<'s> {
session: &'s mut Session,
client: Client<'s>,
client: Client,
fmt_pool: thread::Pool,
background_pool: thread::Pool,
}
@ -60,7 +60,7 @@ impl<'s> Scheduler<'s> {
pub(super) fn request<R>(
&mut self,
params: R::Params,
response_handler: impl Fn(R::Result) -> Task<'s> + 'static,
response_handler: impl Fn(R::Result) -> Task + 'static,
) -> crate::Result<()>
where
R: lsp_types::request::Request,
@ -69,13 +69,13 @@ impl<'s> Scheduler<'s> {
}
/// 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)
}
/// Dispatches a `task` by either running it as a blocking function or
/// 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 {
Task::Sync(SyncTask { func }) => {
let notifier = self.client.notifier();

View file

@ -6,11 +6,11 @@ use crate::{
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 BackgroundFnBuilder<'s> = Box<dyn FnOnce(&Session) -> BackgroundFn + 's>;
type BackgroundFnBuilder = Box<dyn FnOnce(&Session) -> BackgroundFn>;
/// Describes how the task should be run.
#[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
/// 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.
pub(in crate::server) enum Task<'s> {
Background(BackgroundTaskBuilder<'s>),
Sync(SyncTask<'s>),
pub(in crate::server) enum Task {
Background(BackgroundTaskBuilder),
Sync(SyncTask),
}
// 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.
// 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.
pub(in crate::server) struct BackgroundTaskBuilder<'s> {
pub(in crate::server) struct BackgroundTaskBuilder {
pub(super) schedule: BackgroundSchedule,
pub(super) builder: BackgroundFnBuilder<'s>,
pub(super) builder: BackgroundFnBuilder,
}
pub(in crate::server) struct SyncTask<'s> {
pub(super) func: LocalFn<'s>,
pub(in crate::server) struct SyncTask {
pub(super) func: LocalFn,
}
impl<'s> Task<'s> {
impl Task {
/// Creates a new background task.
pub(crate) fn background(
schedule: BackgroundSchedule,
func: impl FnOnce(&Session) -> Box<dyn FnOnce(Notifier, Responder) + Send + 'static> + 's,
) -> Self {
pub(crate) fn background<F>(schedule: BackgroundSchedule, func: F) -> Self
where
F: FnOnce(&Session) -> Box<dyn FnOnce(Notifier, Responder) + Send + 'static> + 'static,
{
Self::Background(BackgroundTaskBuilder {
schedule,
builder: Box::new(func),
})
}
/// Creates a new local task.
pub(crate) fn local(
func: impl FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 's,
) -> Self {
pub(crate) fn local<F>(func: F) -> Self
where
F: FnOnce(&mut Session, Notifier, &mut Requester, Responder) + 'static,
{
Self::Sync(SyncTask {
func: Box::new(func),
})