#spawn_pinned_with_tracing

7 messages · Page 1 of 1 (latest)

faint plank
#

I'm trying to write a function similar to these

pub fn spawn_blocking_with_tracing<F, R>(f: F) -> JoinHandle<R>
where
    F: FnOnce() -> R + Send + 'static,
    R: Send + 'static,
{
    let current_span = tracing::Span::current();
    spawn_blocking(move || current_span.in_scope(f))
}

pub fn spawn_with_tracing<F, R>(f: F) -> JoinHandle<R>
where
    F: FnOnce() -> R + Send + 'static,
    R: Send + 'static + Future<Output = R>,
{
    let current_span = tracing::Span::current();
    spawn(current_span.in_scope(f))
}
    }
}

So that I can spawn_pinned() while maintaining the current span for tracing purposes. I wrote this


use tokio::task::JoinHandle;
pub fn spawn_pinned_with_tracing<F, R>(p: LocalPoolHandle,f: F) -> JoinHandle<R>
where
    F: FnOnce() -> R + Send + 'static,
    R: Send + 'static + futures::Future<Output=R>,
{
    let current_span = tracing::Span::current();
    p.spawn_pinned(move || current_span.in_scope(f))
}

But I'm having a hell of a time using it, I keep getting:

error[E0271]: expected `[async block@/celPool/celData/Works/projects/leptos/integrations/axum/src/lib.rs:795:58: 814:18]` to be a future that resolves to `[async block@/celPool/celData/Works/projects/leptos/integrations/axum/src/lib.rs:795:58: 814:18]`, but it resolves to `()`
   --> /celPool/celData/Works/projects/leptos/integrations/axum/src/lib.rs:795:17
    |
795 |                   spawn_pinned_with_tracing(local_pool, || async move {
    |  _________________^^^^^^^^^^^^^^^^^^^^^^^^^________________-
    | |                 |
    | |                 expected `async` block, found `()`
796 | |                     let app = {
797 | |                         let full_path = full_path.clone();
798 | |                         let (req, req_parts) = generate_request_and_parts(req).await;
...   |
813 | |                     forward_stream(&options, res_options2, bundle, runtime, scope, tx).await;
814 | |                 });
    | |_________________- the expected `async` block
    |
    = note: expected `async` block `[async block@/celPool/celData/Works/projects/leptos/integrations/axum/src/lib.rs:795:58: 814:18]`
                   found unit type `()`
note: required by a bound in `spawn_pinned_with_tracing`

I'm really not sure what I'm doing wrong here. Is the function return type wrong? Am I misunderstanding how to call it?

#

Here's where I call it currently

pub fn thing(){
async move {
                let local_pool = get_leptos_pool();
                spawn_pinned_with_tracing(local_pool, || async move {
                    let app = {
                        let full_path = full_path.clone();
                        let (req, req_parts) = generate_request_and_parts(req).await;
                        let leptos_req = generate_leptos_request(req).await;
                        move |cx| {
                            provide_contexts(cx, full_path, req_parts,leptos_req, default_res_options);
                            app_fn(cx).into_view(cx)
                        }
                    };

                    let (bundle, runtime, scope) =
                        leptos::ssr::render_to_stream_in_order_with_prefix_undisposed_with_context(
                            app,
                            |cx| generate_head_metadata_separated(cx).1.into(),
                            add_context,
                        );

                    forward_stream(&options, res_options2, bundle, runtime, scope, tx).await;
                });
}
}
split oxide
#

that means a future that outputs itself

#

never what you want really

#

You want to write Future<Output = ()>

#

I’m assuming you just got mixed up somewhere?