Aidoku / aidoku-rs

Aidoku source bindings in Rust
29 stars 8 forks source link

ObjectRef ownership lose issue #4

Open Trzeth opened 2 years ago

Trzeth commented 2 years ago

When I try to deal with json data encounter this, here is some test code.

    let root = Request::new("https://api.dmzj.com/dynamic/comicinfo/40956.json", HttpMethod::Get).json().as_object()?;

    // two level get will make result empty
    let infoNodeEmpty = root.get("data").as_object()?.get("info").as_object()?;
    check!(infoNodeEmpty);
    //check!(infoNode.get("id").as_string().read());

    let infoNodeOK = root
        .get("data")
        .as_object()?
        .get("info")
        .clone()// Add clone make this ok
        .as_object()?;
    check!(infoNodeOK);

    let dataNode = root.get("data");
    vcheck!(dataNode);// OK

    let dataNodeCopy = root.get("data");
    let infoNode1 = dataNodeCopy.as_object()?.get("info");
    vcheck!(infoNode1);// Empty
    // Maybe this cause issue.

    let dataNodeObject = root.get("data").as_object()?;
    check!(dataNodeObject);

used marco

macro_rules! check {
    ($e: expr) => {
        println!(
            "ObjectRef {}:{} is {}",
            stringify!($e),
            $e.rid(),
            match $e.is_empty() {
                true => "Empty",
                false => "OK",
            }
        );
    };
}

macro_rules! vcheck {
    ($e: expr) => {
        println!(
            "ValueRef {}:{} is {}",
            stringify!($e),
            $e.rid(),
            match $e.is_none() {
                true => "Empty",
                false => "OK",
            }
        )
    };
}
Skittyblock commented 2 years ago

This is due to rust dropping a value that Aidoku needs to stay alive. I could fix this on Aidoku's side but it would mean more difficult memory management for any source language apart from rust, so ideally if there's a rust way to do it better that would be best.