提问人:kaweinh 提问时间:11/11/2023 最后编辑:Aleksander Krauzekaweinh 更新时间:11/13/2023 访问量:51
Axum get 请求特征不会与泛型保持一致
Axum get request traits won't line up for generics
问:
我最近一直在测试 rust,并决定使用 axum 构建一个服务器。我想以通用形式实现端点,以便将来可以轻松创建它们,并且只使用泛型。我遇到了一个问题,即我在函数定义中为我的类型提供的特征与 get 和 put 处理程序所需的特征不一致。我看了一段时间的 axum 文档来添加和删除特征,但没有任何运气。这是发生错误的路由器定义。
pub fn create_new_router<T, UP, QP>( config: EndpointsConfig ) -> Router where
T: for<'r> FromRow<'r, SqliteRow> + Send + Sync + 'static + Unpin + TableName,
UP: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static + traits::Verification,
QP: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static {
let mut router = Router::new();
for endpoint_config in config.endpoint_configs {
match endpoint_config.verb {
EndpointVerb::GET => {
router = router.route( &format!("/{}", config.path_name), axum::routing::get( endpoints::http_get::<T, QP> ) );
},
EndpointVerb::POST => {
router = router.route( &format!("/{}", config.path_name), axum::routing::post( endpoints::http_post::<T, UP> ) );
},
EndpointVerb::PUT => {
router = router.route( &format!("/{}/:id", config.path_name), axum::routing::put( endpoints::http_put::<T, UP> ) );
},
EndpointVerb::DELETE => {
router = router.route( &format!("/{}/:id", config.path_name), axum::routing::delete( endpoints::http_delete::<T> ) );
}
}
}
return router;
}
然后是 Get 和 Post 处理程序定义。
pub async fn http_get<T, QP>(
Extension( connection_pool ): Extension<SqlitePool>,
Query( parameters ): Query<QP> ) -> Result<Json<Vec<T>>, StatusCode>
where T: for<'r> FromRow<'r, SqliteRow> + Send + Unpin + TableName, QP: Serialize {
let map = convert_to_hashmap( ¶meters );
if let Ok( objects ) = read::<T>( &connection_pool, T::table_name(), &map, None ).await {
Ok( Json( objects ) )
} else {
Err( StatusCode::INTERNAL_SERVER_ERROR )
}
}
pub async fn http_post<T, UP>(
Extension( connection_pool ): Extension<SqlitePool>,
Json( params ): Json<UP> ) -> Result<Json<i32>, StatusCode>
where T: TableName, UP: Verification + Serialize {
let map = convert_to_hashmap( ¶ms );
if params.verify() {
if let Ok( id ) = create( &connection_pool, T::table_name(), &map).await {
Ok( Json( id ) )
} else {
Err( StatusCode::INTERNAL_SERVER_ERROR )
}
} else {
Err( StatusCode::BAD_REQUEST )
}
}
我应该提到奇怪的是,Post 和 Delete 处理程序没有给我任何错误,但 Get 和 Put 都是。帮助信息对我来说也有点陌生,但也许这里有人比我更能理解它。
error[E0277]: the trait bound `fn(Extension<Pool<Sqlite>>, axum::extract::Query<QP>) -> impl Future<Output = Result<axum::Json<Vec<T>>, StatusCode>> {http_get::<T, QP>}: Handler<_, _, _>` is not satisfied
--> src/lib.rs:43:95
|
43 | router = router.route( &format!("/{}", config.path_name), axum::routing::get( endpoints::http_get::<T, QP> ) );
| ------------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(Extension<Pool<Sqlite>>, axum::extract::Query<QP>) -> impl Future<Output = Result<axum::Json<Vec<T>>, StatusCode>> {http_get::<T, QP>}`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `Handler<T, S, B>`:
<Layered<L, H, T, S, B, B2> as Handler<T, S, B2>>
<MethodRouter<S, B> as Handler<(), S, B>>
note: required by a bound in `axum::routing::get`
如果有人看到我在这里错过的东西,我将不胜感激。我只是盯着这个方向看了太久了
答: 暂无答案
评论
Json<Vec<T>>
T: Serialize