recursive use of an object detected which would lead to unsafe aliasing in rust #3660
Answered
by
sezna
RedstoneWizard08
asked this question in
Q&A
-
SummaryWhen 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 DetailsSimilar to #1578 (closed) Here's the code causing this (note that I am using a custom version of //! 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
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"
# ... |
Beta Was this translation helpful? Give feedback.
Answered by
sezna
Nov 15, 2023
Replies: 1 comment 8 replies
-
Bump...? Not trying to be rude or anything but I am kind of under a deadline. (This is a school project) |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you tried eliminating the
&mut self
, instead rewriting it such that it returns a new instance ofTask
?