Issue with iterator with non numerical key #1268
-
Hello ! I have been trying Leptos to discover both the world of Rust and web assembly because I dream of the day I'll be able to write a front end in a type safe language. I was enticed by Leptos view! macro and heard good things about it in general, so I decided to try it out. In short I'm fairly new to both Rust and web assembly. I have been struggling with something that should be simple IMO. I have a stream of values that come in a random order, and want to display them in alphabetical order as they arrive, but my code keeps panicking with the error After simplifying my code as much as I could, here is the smallest reproducer I have: use leptos::*;
static VALUES: [&str; 26] = ["AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", "NN", "OO", "PP", "QQ", "RR", "SS", "TT", "UU", "VV", "WW", "XX", "YY", "ZZ"];
fn add_values(
values_: RwSignal<Vec<String>>,
new_values: Vec<String>
) {
let mut all_values = std::collections::BTreeSet::<String>::new();
values_.with(|current_values| { current_values.iter().for_each(|value| { all_values.insert(value.to_owned()); } ) });
new_values.iter().for_each(|value| { all_values.insert(value.to_owned()); } );
values_.set(all_values.iter().map(|value| value.to_owned()).collect());
log!("{:?}", values_.get());
}
#[component]
pub fn ValuesList(cx: Scope) -> impl IntoView {
let values_ = create_rw_signal(cx, Vec::new());
let mut i = 17;
let feed_values = move |_| {
let mut values = Vec::new();
for _ in 0..3 {
let index = i % VALUES.len();
values.push(VALUES.get(index).unwrap().to_string());
i = i.wrapping_mul(i); // very basic random generator
}
log!("{:?}", values);
add_values(values_, values);
};
view! { cx,
<button on:click=feed_values>"Add values"</button>
<For
each=move || values_.get()
key=|value| value.to_owned()
view=move |cx, value| {
view! { cx,
<div>{value}</div>
}
}
/>
}
}
fn main() {
mount_to_body(|cx| view! { cx, <ValuesList/> })
} Clicking on "Add values" adds three random values that it adds to the list in alphabetical order, not keeping duplicates. After clicking a few times, I get the out of bounds panick mesage. I initially used the random crate instead of wrapping multiplications, but I was afraid the issue could come from there, plus it made the issue harder to debug as I never knew when I'd encounter the error. So my question regarding this is: what am I doing wrong and how should I tackle this problem ? I guess I could use static loops, but my end goal is to have a somewhat sparsely populated (and big) bidimensional table so redrawing the whole table everytime seems a bit heavy handed and bad for performances. Subsidiary question that might be more related to web assembly, I noticed that when panicking, sometimes the drop() method does not seem to be called. In particular, I had a mutex that was not poisoned, and instead complained that you cannot lock recursively the next time I tried to lock it. I'm using nightly Rust and leptos 0.4.1. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I have tried starting from scratch using trunks instead of cargo-leptos and switched to rust stable after noticing the behaviour of my code in codesandbox.io was not the same. I updated the code snippet in my original message with the whole main.rs. I no longer see the |
Beta Was this translation helpful? Give feedback.
Sorry I didn't see this over the weekend. It is very possible this is a bug in
<For/>
. Could you open an issue with the reproduction so we can keep track of it, please?