Giter Site home page Giter Site logo

cannot inspect async code about cargo-inspect HOT 11 CLOSED

mre avatar mre commented on May 26, 2024
cannot inspect async code

from cargo-inspect.

Comments (11)

alexzanderr avatar alexzanderr commented on May 26, 2024 1

my goal is to desguar the async/await syntax to understand whats behind

from cargo-inspect.

alexzanderr avatar alexzanderr commented on May 26, 2024 1

Warning: it's very low-level, but the async fns get desugared.

yeah, i saw, "a little hard" to understand

thanks a lot for your time!

from cargo-inspect.

mre avatar mre commented on May 26, 2024

This crate was superseded by cargo-expand, which added support for all the features that were missing when we started to work on cargo-inspect. Can you try with cargo-expand?

from cargo-inspect.

alexzanderr avatar alexzanderr commented on May 26, 2024

i've tried. it doesnt desugar the async fn and await.
do i have to run it with special arguments or ... ?

from cargo-inspect.

alexzanderr avatar alexzanderr commented on May 26, 2024

command:

cargo expand --example simple > examples/simple.expanded.rs

out

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
extern crate tokio;
use tokio::runtime::{Runtime, Builder as RuntimeBuilder};
use tokio::time::sleep as asleep;
use tokio::join as ajoin;
use std::time::Duration;
async fn task_function(task_name: &str) -> usize {
    let mut sum = 0usize;
    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(&["start!\n"], &[]));
    };
    for index in 0..10 {
        asleep(Duration::from_secs_f32(0.5)).await;
        {
            ::std::io::_print(::core::fmt::Arguments::new_v1(
                &["", ": ", "\n"],
                &[
                    ::core::fmt::ArgumentV1::new_display(&task_name),
                    ::core::fmt::ArgumentV1::new_display(&index),
                ],
            ));
        };
        sum += index;
    }
    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(&["stop!\n"], &[]));
    };
    sum
}
async fn async_main() {
    let task1 = task_function("task-1");
    let task2 = task_function("task-2");
    let (r1, r2) = {
        use ::tokio::macros::support::{maybe_done, poll_fn, Future, Pin};
        use ::tokio::macros::support::Poll::{Ready, Pending};
        let mut futures = (maybe_done(task1), maybe_done(task2));
        let mut skip_next_time: u32 = 0;
        poll_fn(move |cx| {
            const COUNT: u32 = 0 + 1 + 1;
            let mut is_pending = false;
            let mut to_run = COUNT;
            let mut skip = skip_next_time;
            skip_next_time = if skip + 1 == COUNT { 0 } else { skip + 1 };
            loop {
                if skip == 0 {
                    if to_run == 0 {
                        break;
                    }
                    to_run -= 1;
                    let (fut, ..) = &mut futures;
                    let mut fut = unsafe { Pin::new_unchecked(fut) };
                    if fut.poll(cx).is_pending() {
                        is_pending = true;
                    }
                } else {
                    skip -= 1;
                }
                if skip == 0 {
                    if to_run == 0 {
                        break;
                    }
                    to_run -= 1;
                    let (_, fut, ..) = &mut futures;
                    let mut fut = unsafe { Pin::new_unchecked(fut) };
                    if fut.poll(cx).is_pending() {
                        is_pending = true;
                    }
                } else {
                    skip -= 1;
                }
            }
            if is_pending {
                Pending
            } else {
                Ready((
                    {
                        let (fut, ..) = &mut futures;
                        let mut fut = unsafe { Pin::new_unchecked(fut) };
                        fut.take_output().expect("expected completed future")
                    },
                    {
                        let (_, fut, ..) = &mut futures;
                        let mut fut = unsafe { Pin::new_unchecked(fut) };
                        fut.take_output().expect("expected completed future")
                    },
                ))
            }
        })
        .await
    };
}
fn main() {
    let rt = RuntimeBuilder::new_multi_thread()
        .enable_time()
        .worker_threads(1)
        .thread_name("main-workter")
        .thread_stack_size(3 * 1024 * 1024)
        .build()
        .unwrap();
    rt.block_on(async_main());
}

from cargo-inspect.

mre avatar mre commented on May 26, 2024

Oof I don't know if you can do that to be honest. At least I don't know of a way but keep me posted if you figure out how to do it. 😕

from cargo-inspect.

mre avatar mre commented on May 26, 2024

cargo-inspect and cargo-expand are just thin wrappers around what the compiler spits out, so if anything it must be a compiler flag. Probably something around rustc -Z as we do here:

cargo-inspect/src/hir.rs

Lines 42 to 49 in 80cbb58

let mut p = process::Command::new("cargo")
.arg("+nightly")
.arg("rustc")
.arg("--")
.arg(format!("-Zunpretty={}", unpretty))
.stderr(process::Stdio::piped())
.stdout(process::Stdio::piped())
.spawn()?;

from cargo-inspect.

alexzanderr avatar alexzanderr commented on May 26, 2024

cargo-inspect and cargo-expand are just thin wrappers around what the compiler spits out

yeah, but how comes that macros are spitted out (in the most high detail) and async/await syntax is not?

i guess there is a stage at compilation to desugar stuff like async/await, if let .. etc (after the macro expansion)

expansion != desugaring

correct me if im wrong

from cargo-inspect.

mre avatar mre commented on May 26, 2024

expansion != desugaring

That's correct.
Expansion is used for macro expansion, but desugaring goes beyond that and is what you want.
You can try

 cargo +nightly rustc -- -Zunpretty=mir

Warning: it's very low-level, but the async fns get desugared.

All options here:
rust-lang/rust#36473 (comment)

from cargo-inspect.

mre avatar mre commented on May 26, 2024

Yeah it's tough.

You can limit the output to a few functions like main. Instructions here: https://github.com/rust-lang/rustc-dev-guide/blob/master/src/mir/debugging.md

Project idea: a MIR prettifier. Although it would probably a task that takes a lifetime to complete. 😆

from cargo-inspect.

alexzanderr avatar alexzanderr commented on May 26, 2024

Project idea: a MIR prettifier. Although it would probably a task that takes a lifetime to complete. laughing

yep, no thanks 😆

from cargo-inspect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.