FromRef<()> is not implemented for `Pool<AsyncDieselConnectionManager<AsyncPgConnection>> #2240
-
Hi This is the error:
This is the function: #[debug_handler]
pub async fn create_tv_genre(
DatabaseConnection(mut conn): DatabaseConnection,
Json(new_tv_genre): Json<NewTvGenre>,
) -> Result<Response<Json<TvGenre>>, CreateError<ApiError>> {
let tv_genre = diesel::insert_into(tv_genres::table)
.values(NewDbTvGenre::from(&new_tv_genre))
.get_result::<TvGenre>(&mut conn)
.await?;
Ok(Response::builder()
.status(StatusCode::CREATED)
.header("Location", format!("/tv-genres/{}", tv_genre.id))
.body(Json(tv_genre))
.unwrap())
} And the DatabaseConnection implementation: pub type DbConn = bb8::PooledConnection<'static, AsyncDieselConnectionManager<AsyncPgConnection>>;
pub type DbPool = bb8::Pool<AsyncDieselConnectionManager<AsyncPgConnection>>;
/// Utility function for mapping any error into a `500 Internal Server Error`
/// response.
fn internal_error<E>(err: E) -> (StatusCode, String)
where
E: std::error::Error,
{
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
}
pub struct DatabaseConnection(
pub bb8::PooledConnection<'static, AsyncDieselConnectionManager<AsyncPgConnection>>,
);
#[async_trait]
impl<S> FromRequestParts<S> for DatabaseConnection
where
S: Send + Sync,
DbPool: FromRef<S>,
{
type Rejection = (StatusCode, String);
async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let pool = DbPool::from_ref(state);
let conn = pool.get_owned().await.map_err(internal_error)?;
Ok(Self(conn))
}
} I have other function with the same parameters and they don't give error. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
When using |
Beta Was this translation helpful? Give feedback.
-
And how can I extract 2 State one using State and the other using FromRequestsParts? #[debug_handler(state = DbPool)]
pub async fn delete_program(
DatabaseConnection(mut conn): DatabaseConnection,
State(meilisearch_client): State<meilisearch_sdk::client::Client>,
Path(id): Path<i64>,
){
...
} The documentation shows that I have to make one AppState and the convert to the others but how can I do that and the same time use DatabaseConnection(mut conn): DatabaseConnection ? #[derive(Clone)]
struct AppState {
// that holds some api specific state
api_state: ApiState,
}
// the api specific state
#[derive(Clone)]
struct ApiState {}
// support converting an `AppState` in an `ApiState`
impl FromRef<AppState> for ApiState {
fn from_ref(app_state: &AppState) -> ApiState {
app_state.api_state.clone()
}
} |
Beta Was this translation helpful? Give feedback.
When using
#[debug_handler]
on handlers that use state you have to set it, if it can't otherwise be inferred. See https://docs.rs/axum/latest/axum/attr.debug_handler.html#changing-state-type