context!() { /* proc-macro */ }Expand description
Generates a context reference binding statement.
This function-like procedural macro generates a let statement that converts
a context pointer into a reference to ::hyperlane::Context.
The conversion is performed through the Into trait with an intermediate
conversion to usize.
The mutability of the generated reference is determined by the optional type annotation:
- If the type annotation contains
&mut, generatesleak_mut()call - Otherwise, generates
leak()call (default behavior)
§Arguments
TokenStream- The input token stream containing the variable name identifier and an optional type annotation (e.g.,ctxorctx: &mut Context).
§Returns
TokenStream- A let statement binding the specified variable name to a&mut ::hyperlane::Contextor&::hyperlane::Contextobtained through pointer conversion.
§Examples
With explicit type annotation for mutable reference:
use hyperlane::*;
use hyperlane_macros::*;
#[route("/context_mut")]
struct ContextMut;
impl ServerHook for ContextMut {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status {
let new_ctx: &mut Context = unsafe { context!(ctx: &mut Context) };
let _ = new_ctx.get_mut_response();
Status::Continue
}
}
async fn example(_: &mut Stream, ctx: &mut Context) {
let new_ctx: &mut Context = unsafe { context!(ctx: &mut Context) };
let _ = new_ctx.get_mut_response();
}With explicit type annotation for immutable reference:
use hyperlane::*;
use hyperlane_macros::*;
#[route("/context_ref")]
struct ContextRef;
impl ServerHook for ContextRef {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status {
let new_ctx: &::hyperlane::Context = unsafe { context!(ctx: &::hyperlane::Context) };
let _ = new_ctx.get_request();
Status::Continue
}
}
async fn example(_: &mut Stream, ctx: &mut Context) {
let new_ctx: &::hyperlane::Context = unsafe { context!(ctx: &::hyperlane::Context) };
let _ = new_ctx.get_request();
}Without type annotation (defaults to immutable):
use hyperlane::*;
use hyperlane_macros::*;
#[route("/context_default")]
struct ContextDefault;
impl ServerHook for ContextDefault {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status {
let new_ctx = unsafe { context!(ctx) };
let _ = new_ctx.get_request();
Status::Continue
}
}
async fn example(_: &mut Stream, ctx: &mut Context) {
let new_ctx = unsafe { context!(ctx) };
let _ = new_ctx.get_request();
}§Safety
- The address is guaranteed to be a valid
Selfinstance that was previously converted from a reference and is managed by the runtime.