Skip to content

Commit

Permalink
fix: fix type inference on extract() functions (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj authored Jan 26, 2024
1 parent 26d1aee commit 1b5961e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
6 changes: 2 additions & 4 deletions integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,15 +1390,13 @@ impl LeptosRoutes for &mut ServiceConfig {
/// Ok(data)
/// }
/// ```
pub async fn extract<T, CustErr>() -> Result<T, ServerFnError<CustErr>>
pub async fn extract<T>() -> Result<T, ServerFnError>
where
T: actix_web::FromRequest,
<T as FromRequest>::Error: Display,
{
let req = use_context::<HttpRequest>().ok_or_else(|| {
ServerFnError::ServerError(
"HttpRequest should have been provided via context".to_string(),
)
ServerFnError::new("HttpRequest should have been provided via context")
})?;

T::extract(&req)
Expand Down
20 changes: 6 additions & 14 deletions integrations/axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ use leptos_router::*;
use once_cell::sync::OnceCell;
use parking_lot::RwLock;
use server_fn::redirect::REDIRECT_HEADER;
use std::{
error::Error, fmt::Debug, io, pin::Pin, sync::Arc,
thread::available_parallelism,
};
use std::{fmt::Debug, io, pin::Pin, sync::Arc, thread::available_parallelism};
use tokio_util::task::LocalPoolHandle;
use tracing::Instrument;

Expand Down Expand Up @@ -1772,13 +1769,12 @@ fn get_leptos_pool() -> LocalPoolHandle {
/// Ok(query)
/// }
/// ```
pub async fn extract<T, CustErr>() -> Result<T, ServerFnError>
pub async fn extract<T>() -> Result<T, ServerFnError>
where
T: Sized + FromRequestParts<()>,
T::Rejection: Debug,
CustErr: Error + 'static,
{
extract_with_state::<T, (), CustErr>(&()).await
extract_with_state::<T, ()>(&()).await
}

/// A helper to make it easier to use Axum extractors in server functions. This
Expand All @@ -1800,18 +1796,14 @@ where
/// Ok(query)
/// }
/// ```
pub async fn extract_with_state<T, S, CustErr>(
state: &S,
) -> Result<T, ServerFnError>
pub async fn extract_with_state<T, S>(state: &S) -> Result<T, ServerFnError>
where
T: Sized + FromRequestParts<S>,
T::Rejection: Debug,
CustErr: Error + 'static,
{
let mut parts = use_context::<Parts>().ok_or_else(|| {
ServerFnError::ServerError::<CustErr>(
"should have had Parts provided by the leptos_axum integration"
.to_string(),
ServerFnError::new(
"should have had Parts provided by the leptos_axum integration",
)
})?;
T::from_request_parts(&mut parts, state)
Expand Down

0 comments on commit 1b5961e

Please sign in to comment.