ngyn_shared::server::context

Struct NgynContext

Source
pub struct NgynContext<'a> { /* private fields */ }
Expand description

Represents the context of a request in Ngyn

Implementations§

Source§

impl<'a> NgynContext<'a>

Source

pub fn request(&self) -> &Request<Vec<u8>>

Retrieves the request associated with the context.

§Returns

A reference to the request associated with the context.

§Examples
use ngyn_shared::core::context::NgynContext;
use hyper::Request;

let request = Request::new(Vec::new());
let context = NgynContext::from_request(request);

let request_ref = context.request();
Source

pub fn response(&mut self) -> &mut NgynResponse

👎Deprecated since 0.5.2: use response_mut() instead
Source

pub fn response_mut(&mut self) -> &mut NgynResponse

Retrieves the response associated with the context.

§Returns

A mutable reference to the response associated with the context.

§Examples
use ngyn_shared::core::context::NgynContext;
use http::Request;

let request = Request::new(Vec::new());
let context = NgynContext::from_request(request);

let response_ref = context.response_mut();
Source

pub fn params(&self) -> Option<&Params<'a, 'a>>

Retrieves the params associated with the context.

§Returns

An optional reference to the params associated with the context.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());

let params_ref = context.params();
Source§

impl NgynContext<'_>

Source

pub fn state<T: 'static>(&self) -> Option<&T>

Retrieves the state of the context as a reference to the specified type.

§Type Parameters
  • T - The type of the state to retrieve.
§Returns

An optional reference to the state of the specified type. Returns None if the state is not found or if it cannot be downcasted to the specified type.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);

let state_ref = context.state::<TestAppState>();
Source

pub fn state_mut<T: 'static>(&mut self) -> Option<&mut T>

Retrieves the state of the context as a mutable reference to the specified type.

§Type Parameters
  • T - The type of the state to retrieve.
§Returns

An optional reference to the state of the specified type. Returns None if the state is not found or if it cannot be downcasted to the specified type.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);

let state_ref = context.state::<TestAppState>();
Source§

impl<'b> NgynContext<'b>

Source

pub fn get<V: for<'a> Deserialize<'a>>(&self, key: &str) -> Option<V>

Retrieves the value associated with the given key from the context.

§Arguments
  • key - The key (case-insensitive) to retrieve the value for.
§Returns

A reference to the value associated with the key. If the key is not found, returns a reference to an empty context value.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());

let value: String = context.get("name").unwrap();
assert_eq!(value, "John".to_string());
Source

pub fn set<V: Serialize>(&mut self, key: &'b str, value: V)

Sets the value associated with the given key in the context.

§Arguments
  • key - The key (case-insensitive) to set the value for.
  • value - The value to associate with the key.
§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());

let value: String = context.get("name").unwrap();
assert_eq!(value, "John".to_string());
Source

pub fn remove(&mut self, key: &str)

Removes the value associated with the given key from the context.

§Arguments
  • key - The key (case-insensitive) to remove the value for.
§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());

context.remove("name");
let value = context.get::<String>("name");
assert_eq!(value, None);
Source

pub fn clear(&mut self)

Clears all values from the context.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());
context.set("age", 30.into());

context.clear();
assert_eq!(context.len(), 0);
Source

pub fn len(&self) -> usize

Returns the number of values in the context.

§Returns

The number of values in the context.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());
context.set("age", 30.into());

assert_eq!(context.len(), 2);
Source

pub fn is_empty(&self) -> bool

Checks if the context is empty.

§Returns

true if the context is empty, false otherwise.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);

assert!(context.is_empty());

context.set("name", "John".to_string());
assert!(!context.is_empty());
Source

pub fn has(&self, key: &str) -> bool

Checks if the context contains a value for the given key.

§Arguments
  • key - The key (case-insensitive) to check for.
§Returns

true if the context contains a value for the key, false otherwise.

§Examples
use ngyn_shared::core::context::NgynContext;

let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());

assert!(context.has("name"));
assert!(!context.has("age"));

Trait Implementations§

Source§

impl<'a> Transformer<'a> for &'a NgynContext<'a>

Source§

fn transform(cx: &'a mut NgynContext<'_>) -> Self

Transforms the given NgynContext and returns an instance of Self. Read more

Auto Trait Implementations§

§

impl<'a> !Freeze for NgynContext<'a>

§

impl<'a> !RefUnwindSafe for NgynContext<'a>

§

impl<'a> Send for NgynContext<'a>

§

impl<'a> Sync for NgynContext<'a>

§

impl<'a> Unpin for NgynContext<'a>

§

impl<'a> !UnwindSafe for NgynContext<'a>

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T