Skip to main content

Context

Struct Context 

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

The main connection context, providing thread-safe access to connection data.

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

Implementations§

Source§

impl Context

Implementation of methods for the Context structure.

Source

pub fn new() -> Self

Creates a new Context with default values.

§Returns
  • Self - A new Context instance.
Source

pub async fn is_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) -> &Self

Sets the aborted flag for the context.

§Arguments
  • bool - The aborted state to set.
§Returns
  • &Self - Reference to self for method chaining.
Source

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

Marks the context as aborted.

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

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

Cancels the aborted state of the context.

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

pub async fn is_closed(&self) -> bool

Checks if the connection has been closed.

§Returns
  • bool - True if the connection is closed, otherwise false.
Source

pub async fn set_closed(&self, closed: bool) -> &Self

Sets the closed flag for the connection.

§Arguments
  • bool - The closed state to set.
§Returns
  • &Self - Reference to self for method chaining.
Source

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

Marks the connection as closed.

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

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

Opens the connection (clears the closed flag).

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

pub async fn is_terminated(&self) -> bool

Checks if the connection has been terminated (aborted or closed).

§Returns
  • bool - True if the connection is either aborted or closed, otherwise false.
Source

pub async fn try_get_stream(&self) -> Option<ArcRwLockStream>

Gets the stream from the context.

§Returns
  • Option<ArcRwLockStream> - The stream if available.
Source

pub async fn get_stream(&self) -> ArcRwLockStream

Gets the stream from the context.

§Returns
  • ArcRwLockStream - The stream.
§Panics

Panics if the stream is not set.

Source

pub async fn set_stream(&self, stream: ArcRwLockStream) -> &Self

Sets the stream in the context.

§Arguments
  • ArcRwLockStream - The stream to set.
§Returns
  • &Self - Reference to self for method chaining.
Source

pub async fn get_request(&self) -> Request

Gets the request from the context.

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

pub async fn set_request(&self, request: Request) -> &Self

Sets the request in the context.

§Arguments
  • Request - The request to set.
§Returns
  • &Self - Reference to self for method chaining.
Source

pub async fn get_response(&self) -> Response

Gets the response from the context.

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

pub async fn set_response(&self, response: Response) -> &Self

Sets the response in the context.

§Arguments
  • Response - The response to set.
§Returns
  • &Self - Reference to self for method chaining.
Source

pub async fn try_get_socket_addr(&self) -> OptionSocketAddr

Attempts to get the socket address from the stream.

§Returns
  • OptionSocketAddr - The socket address if available.
Source

pub async fn get_socket_addr(&self) -> SocketAddr

Gets the socket address.

§Returns
  • SocketAddr - The socket address.
§Panics

Panics if the socket address is not available.

Source

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

Gets the socket address as a string.

§Returns
  • Option<String> - The socket address as a string if available.
Source

pub async fn get_socket_addr_string(&self) -> String

Gets the socket address as a string.

§Returns
  • String - The socket address as a string.
§Panics

Panics if the socket address is not available.

Source

pub async fn try_get_socket_host(&self) -> OptionSocketHost

Attempts to get the socket host (IP address).

§Returns
  • OptionSocketHost - The socket host if available.
Source

pub async fn get_socket_host(&self) -> IpAddr

Gets the socket host.

§Returns
  • std::net::IpAddr - The socket host.
§Panics

Panics if the socket host is not available.

Source

pub async fn try_get_socket_port(&self) -> OptionSocketPort

Attempts to get the socket port.

§Returns
  • OptionSocketPort - The socket port if available.
Source

pub async fn get_socket_port(&self) -> u16

Gets the socket port.

§Returns
  • u16 - The socket port.
§Panics

Panics if the socket port is not available.

Source

pub async fn set_data<K, V>(&self, key: K, value: V) -> &Self
where K: Into<String>, V: Any + Send + Sync + Clone,

Sets a data value in the context’s data map.

§Arguments
  • Into<String> - The key for the data.
  • Any + Send + Sync + Clone - The value to set, which must be cloneable and thread-safe.
§Returns
  • &Self - Reference to self for method chaining.
Source

pub async fn try_get_data<V, K>(&self, key: K) -> Option<V>
where V: Any + Send + Sync + Clone, K: AsRef<str>,

Gets a data value from the context’s data map.

§Arguments
  • AsRef<str> - The key for the data.
§Returns
  • Option<V> - The data value if found and successfully downcasted, otherwise None.
Source

pub async fn get_data_value<V, K>(&self, key: K) -> V
where V: Any + Send + Sync + Clone, K: AsRef<str>,

Gets a data value from the context’s data map.

§Arguments
  • AsRef<str> - The key for the data.
§Returns
  • V - The data value.
§Panics

Panics if the data is not found or cannot be downcasted.

Source

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

Removes a data value from the context’s data map.

§Arguments
  • AsRef<str> - The key of the data to remove.
§Returns
  • &Self - Reference to self for method chaining.
Source

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

Clears all data from the context’s data map.

§Returns
  • &Self - Reference to self for method chaining.
Source

pub async fn try_send<D>(&self, data: D) -> ResponseResult
where D: AsRef<[u8]>,

Attempts to send data through the stream.

§Arguments
  • D - Data that can be converted to a byte slice.
§Returns
  • ResponseResult - Ok(()) on success, or an error on failure.
Source

pub async fn send<D>(&self, data: D)
where D: AsRef<[u8]>,

Sends data through the stream.

§Arguments
  • D - Data that can be converted to a byte slice.
§Panics

Panics if the send operation fails.

Source

pub async fn try_flush(&self) -> ResponseResult

Attempts to flush the stream.

§Returns
  • ResponseResult - Ok(()) on success, or an error on failure.
Source

pub async fn flush(&self)

Flushes the stream.

§Panics

Panics if the flush operation fails.

Source

pub async fn try_shutdown(&self) -> ResponseResult

Attempts to shut down the stream.

§Returns
  • ResponseResult - Ok(()) on success, or an error on failure.
Source

pub async fn shutdown(&self)

Shuts down the stream.

§Panics

Panics if the shutdown operation fails.

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 new Context instance with default values.

§Returns
  • Self - A new Context wrapping default ContextData.
Source§

impl From<ArcRwLockStream> for Context

Implementation of From<ArcRwLockStream> for Context.

Source§

fn from(stream: ArcRwLockStream) -> Self

Converts an ArcRwLockStream into a Context.

§Arguments
  • ArcRwLockStream - The stream to set in the context.
§Returns
  • Context - A new Context instance with the stream set.

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.