Skip to main content

IncomingRequests

Struct IncomingRequests 

Source
pub struct IncomingRequests<I> { /* private fields */ }
Expand description

Tracks requests received from clients (incoming to the server).

When the server receives a request, it registers the request ID along with any metadata needed to process the response. When the server is ready to send a response, it completes the request to retrieve the metadata.

Each request is also associated with a CancellationToken that can be used to signal cancellation (e.g., when receiving $/cancelRequest).

The generic parameter I represents user-defined metadata associated with each incoming request (e.g., handler context, timing info, request origin).

§Example

use lsp_server_tokio::{IncomingRequests, RequestId};
use tokio_util::sync::CancellationToken;

let mut incoming: IncomingRequests<String> = IncomingRequests::new();

// Register a request with metadata and cancellation token
let token1 = CancellationToken::new();
let token2 = CancellationToken::new();
incoming.register(1.into(), "textDocument/hover".to_string(), token1);
incoming.register(2.into(), "textDocument/completion".to_string(), token2);

assert_eq!(incoming.pending_count(), 2);
assert!(incoming.is_pending(&1.into()));

// Cancel a request
incoming.cancel(&2.into());

// Complete request and get metadata back
let method = incoming.complete(&1.into());
assert_eq!(method, Some("textDocument/hover".to_string()));
assert_eq!(incoming.pending_count(), 1);

Implementations§

Source§

impl<I> IncomingRequests<I>

Source

pub fn new() -> Self

Creates a new empty incoming request tracker.

Source

pub fn register(&mut self, id: RequestId, data: I, token: CancellationToken)

Registers an incoming request with associated metadata and cancellation token.

The metadata can be any user-defined type that you want to associate with this request until it’s completed. The cancellation token can be used to signal request cancellation to async handlers.

Source

pub fn complete(&mut self, id: &RequestId) -> Option<I>

Completes an incoming request, removing it from tracking and returning the metadata.

Returns Some(metadata) if the request was pending, None otherwise. The cancellation token is dropped when the request is completed.

Source

pub fn is_pending(&self, id: &RequestId) -> bool

Returns true if the request is currently pending.

Source

pub fn cancel(&self, id: &RequestId) -> bool

Cancels a pending request by triggering its cancellation token.

Returns true if the request was found and cancelled, false if the request ID was not pending. Note that cancelling an already-cancelled token is a no-op.

Source

pub fn get_token(&self, id: &RequestId) -> Option<CancellationToken>

Returns a clone of the cancellation token for a pending request.

Returns None if the request is not pending. The returned token can be passed to async handlers for cooperative cancellation.

Source

pub fn pending_count(&self) -> usize

Returns the number of currently pending requests.

Trait Implementations§

Source§

impl<I: Debug> Debug for IncomingRequests<I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I> Default for IncomingRequests<I>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<I> Freeze for IncomingRequests<I>

§

impl<I> RefUnwindSafe for IncomingRequests<I>
where I: RefUnwindSafe,

§

impl<I> Send for IncomingRequests<I>
where I: Send,

§

impl<I> Sync for IncomingRequests<I>
where I: Sync,

§

impl<I> Unpin for IncomingRequests<I>
where I: Unpin,

§

impl<I> UnsafeUnpin for IncomingRequests<I>

§

impl<I> UnwindSafe for IncomingRequests<I>
where I: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.