You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
axum has an extractor called State. This is similar to poem's Data, but the difference is that axum's State is zero-cost and verified on compile-time. So it's impossible to compile handlers that requests a certain state, without providing that state during registration.
poem currently relies on http's Extensions for storing state, which is a hashmap. This means that your app can error on run-time when you forget to inject the data. This also incurs a performance cost because you need to perform a hashmap lookup on every request (probably negligible, but still).
Code example (if possible)
We can have a new State extractor as an alternative to Data:
use poem::{get, handler, listener::TcpListener,Route,Server,State};structAppState{}#[handler]fnuse_state(state:State<AppState>) -> String{todo!()}#[tokio::main]asyncfnmain() -> Result<(), std::io::Error>{if std::env::var_os("RUST_LOG").is_none(){
std::env::set_var("RUST_LOG","poem=debug");}
tracing_subscriber::fmt::init();let state = AppState{};let app = Route::new().at("/use-state",get(use_state)).with_state(state);// comment out this line to trigger a compile-time errorServer::new(TcpListener::bind("0.0.0.0:3000")).name("app-state").run(app).await}
The text was updated successfully, but these errors were encountered:
Description of the feature
axum
has an extractor calledState
. This is similar topoem
'sData
, but the difference is thataxum
'sState
is zero-cost and verified on compile-time. So it's impossible to compile handlers that requests a certain state, without providing that state during registration.poem
currently relies onhttp
'sExtensions
for storing state, which is a hashmap. This means that your app can error on run-time when you forget to inject the data. This also incurs a performance cost because you need to perform a hashmap lookup on every request (probably negligible, but still).Code example (if possible)
We can have a new
State
extractor as an alternative toData
:The text was updated successfully, but these errors were encountered: