Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

recursive use of an object detected which would lead to unsafe aliasing in rust #3658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RedstoneWizard08 opened this issue Oct 16, 2023 · 0 comments
Labels

Comments

@RedstoneWizard08
Copy link

RedstoneWizard08 commented Oct 16, 2023

Summary

When I call a method from JavaScript, it throws the error "recursive use of an object detected which would lead to unsafe aliasing in rust." How can I fix this?

Additional Details

Similar to #1578 (closed)

Here's the code causing this (note that I am using a custom version of anyhow to add wasm support):

//! crates/core/src/services/tasks/save.rs

use anyhow::Result;
use serde_json::Value;

use crate::{impl_save_fn, models::Task, services::POSTGREST};

impl_save_fn!(Task, "tasks");

//! crates/core/src/macros/funcs/save.rs

#[macro_export]
macro_rules! impl_save_fn {
    ($struct_: ident, $table: expr) => {
        impl $struct_ {
            async fn insert(&mut self, token: String) -> Result<()> {
                let client = POSTGREST.from($table).auth(token);
                let encoded = serde_json::to_string(&self)?;
                let res = client.insert(encoded).execute().await?;
                let data = res.text().await?;
                let data = serde_json::from_str::<Value>(&data)?;
                let id = data.get("id");

                if let Some(id) = id {
                    let id = id
                        .as_i64()
                        .ok_or(anyhow!("Cannot convert Value to i64!"))?;

                    self.id = Some(id as i32);

                    return Ok(());
                } else {
                    let data = data
                        .as_array()
                        .ok_or(anyhow!("Cannot convert Value to Vec<Value>!"))?;

                    let data = data
                        .get(0)
                        .ok_or(anyhow!(
                            "Index 0 out of bounds for Vec<Value> of length {}!",
                            data.len()
                        ))?
                        .clone();
                        
                    let id = data
                        .get("id")
                        .ok_or(anyhow!("Cannot get ID from response data!"))?
                        .as_i64()
                        .ok_or(anyhow!("Cannot convert Value to i64!"))?;

                    self.id = Some(id as i32);

                    return Ok(());
                }
            }
        }

        #[wasm_bindgen]
        impl $struct_ {
            pub async fn save(&mut self, token: String) -> Result<()> {
                let client = POSTGREST.from($table).auth(&token);

                let existing = client
                    .clone()
                    .eq("user_id", self.user_id.clone())
                    .select("*")
                    .execute()
                    .await?;

                let data = existing.text().await?;
                let data = serde_json::from_str::<Value>(&data)?;

                let data = data
                    .as_array()
                    .ok_or(anyhow!("Cannot convert Value to Vec<Value>!"));

                let encoded = serde_json::to_string(&self)?;

                if let Ok(data) = data {
                    if data.is_empty() {
                        return self.insert(token).await;
                    }

                    let res = client
                        .eq("user_id", self.user_id.clone())
                        .update(encoded)
                        .execute()
                        .await?;

                    let data = res.text().await?;

                    let data = serde_json::from_str::<Value>(&data)?
                        .as_array()
                        .ok_or(anyhow!("Cannot convert Value to Vec<Value>!"))?
                        .clone();

                    let data = data
                        .get(0)
                        .ok_or(anyhow!(
                            "Index 0 out of bounds for Vec<Value> of length {}!",
                            data.len()
                        ))?
                        .clone();

                    let id = data
                        .get("id")
                        .ok_or(anyhow!("Cannot get ID from response data!"))?
                        .as_i64()
                        .ok_or(anyhow!("Cannot convert Value to i64!"))?;

                    self.id = Some(id as i32);

                    return Ok(());
                }

                self.insert(token).await
            }
        }
    };
}
<!-- apps/planner/src/App.svelte -->

<script lang="ts">
    import init from "...";
    import { initializeAuth, initializeTheme } from "./lib";

    const initialize = async () => {
        await init();
        await initializeAuth();

        initializeTheme();
    };

    initialize();
</script>

<!-- ... -->
// apps/planner/src/lib/auth.ts (this is a school project lmao)

export const initializeAuth = async () => {
    // ...

    if (token) {
        // ...

        await postLogin();
    }
};

export const postLogin = async () => {
    // ...

    tasks.subscribe(async (ts) => {
        for (const val of ts.values()) {
            for (const task of val) {
                await task.save(get(accessToken)!);
            }
        }
    });
};
# crates/core/Cargo.toml
# ...
[dependencies]
# ...
serde-wasm-bindgen = "0.6.0"
wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4.37"
# ...
@rustwasm rustwasm locked and limited conversation to collaborators Oct 17, 2023
@daxpedda daxpedda converted this issue into discussion #3660 Oct 17, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

1 participant