Skip to main content

Context

Struct Context 

Source
pub struct Context(/* private fields */);
Expand description

The main application context, providing thread-safe access to request and response data.

This is a wrapper around ContextData that uses an Arc<RwLock<>> to allow for shared, mutable access across asynchronous tasks.

Implementations§

Source§

impl Context

Implementation of methods for Context.

Source

pub fn new( socket: &ArcRwLockUdpSocket, request: &Request, client_addr: SocketAddr, ) -> Self

Creates a new Context with the given socket, request, and client address.

§Arguments
  • socket - The network socket.
  • request - The request data.
  • client_addr - The client’s socket address.
§Returns
  • Self - A new Context instance.
Source

pub async fn get_aborted(&self) -> bool

Checks if the context has been marked as aborted.

§Returns
  • bool - True if the context is aborted, otherwise false.
Source

pub async fn set_aborted(&self, aborted: bool)

Sets the aborted flag for the context.

§Arguments
  • aborted - The aborted state to set.
Source

pub async fn aborted(&self)

Marks the context as aborted.

Source

pub async fn cancel_aborted(&self)

Cancels the aborted state of the context.

Source

pub async fn try_get_socket(&self) -> Option<ArcRwLockUdpSocket>

Retrieves the underlying network socket, if available.

§Returns
  • Option<ArcRwLockUdpSocket> - The thread-safe, shareable network socket if it exists.
Source

pub async fn get_socket(&self) -> ArcRwLockUdpSocket

Retrieves the underlying network socket.

§Returns
  • ArcRwLockUdpSocket - The thread-safe, shareable network socket.
§Panics
  • If the network socket is not found.
Source

pub async fn get_request(&self) -> Request

Retrieves the current request.

§Returns
  • Request - A clone of the current request.
Source

pub async fn get_response(&self) -> Response

Retrieves the current response.

§Returns
  • Response - A clone of the current response.
Source

pub async fn try_get_client_addr(&self) -> Option<SocketAddr>

Retrieves the client address, if available.

§Returns
  • Option<SocketAddr> - The client address if present.
Source

pub async fn get_client_addr(&self) -> SocketAddr

Retrieves the client address.

§Returns
  • SocketAddr - The client address.
§Panics
  • If the client address is not found.
Source

pub async fn try_get_client_addr_string(&self) -> Option<String>

Retrieves the client address as a string.

§Returns
  • Option<String> - The string representation of the client address if available.
Source

pub async fn try_get_client_host(&self) -> Option<IpAddr>

Retrieves the client host IP address.

§Returns
  • Option<IpAddr> - The client IP address if available.
Source

pub async fn try_get_client_port(&self) -> Option<u16>

Retrieves the client port number.

§Returns
  • Option<u16> - The client port number if available.
Source

pub async fn set_attribute<K, V>(&self, key: K, value: V) -> &Self
where K: AsRef<str>, V: AnySendSyncClone,

Sets an attribute in the context.

§Arguments
  • K - The key of the attribute to set.
  • V - The value of the attribute.
§Returns
  • &Self - A reference to the modified context.
Source

pub async fn try_get_attribute<V, K>(&self, key: K) -> Option<V>
where V: AnySendSyncClone, K: AsRef<str>,

Attempts to retrieve a specific attribute by its key.

§Arguments
  • K - The key of the attribute to retrieve.
§Returns
  • Option<V> - The attribute value if it exists and can be cast to the specified type.
Source

pub async fn get_attribute<V, K>(&self, key: K) -> V
where V: AnySendSyncClone, K: AsRef<str>,

Retrieves a specific attribute by its key.

§Arguments
  • K - The key of the attribute to retrieve.
§Returns
  • V - The attribute value.
§Panics
  • If the attribute is not found.
Source

pub async fn remove_attribute<K>(&self, key: K) -> &Self
where K: AsRef<str>,

Removes an attribute from the context.

§Arguments
  • K - The key of the attribute to remove.
§Returns
  • &Self - A reference to the modified context.
Source

pub async fn clear_attributes(&self) -> &Self

Clears all attributes from the context.

§Returns
  • &Self - A reference to the modified context.
Source

pub async fn send<T>(&self, data: T) -> ResponseResult
where T: Into<ResponseData>,

Sends a response to the client.

§Arguments
  • Into<ResponseData> - The response data.
§Returns
  • ResponseResult - Result of the send operation.

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Context

Provides a default implementation for Context.

Source§

fn default() -> Self

Creates a default Context wrapping default ContextData.

§Returns
  • Self - A new instance with default values.

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> AnySendSyncClone for T
where T: Any + Send + Sync + Clone,