ctx_ 0.1.8

Extract message form headers only once for one req ( support async and sync ) / 一个请求只提取一次消息, 支持异步和同步
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::any::TypeId;

use crate::Ctx;

pub trait Extract: Sized {
  fn from_ctx(ctx: &super::Ctx) -> Self;
}

pub fn get<T: Extract + 'static>(ctx: &Ctx) -> &T {
  let ptr = *ctx.cache.entry(TypeId::of::<T>()).or_insert_with(|| {
    let bump = ctx.bump.lock();
    let ptr = bump.alloc(T::from_ctx(ctx));
    ptr as *const T as usize
  });

  unsafe { &*(ptr as *const T) }
}