Skip to content

Commit 3b06943

Browse files
Pascal-SoMadoshakalaka
authored andcommitted
add warning comment about assumptions
1 parent 0707a12 commit 3b06943

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

packages/yew/src/functional/hooks/use_reducer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,14 @@ where
275275
let mut val = val.borrow_mut();
276276
let next_val = (*val).clone().reduce(action);
277277

278-
// Check if the reduce action just returned the same `Rc` again
279-
// instead of producing a new one.
278+
// Check if the reduce action just returned the same `Rc` again instead of producing
279+
// a new one.
280+
// NOTE: here we make the assumption that an unchanged address implies that the
281+
// "identity" of the `Rc` is unchanged. This assumption is valid here because we
282+
// still keep the old Rc around. But if we were to instead move the old Rc into
283+
// the `reduce` function, then the address could be reused and the object inside
284+
// the Rc might be different. The `rc_was_reused` variable is thus only meaningful
285+
// as long as we use a `clone` before `reduce`.
280286
let rc_was_reused = Rc::ptr_eq(&val, &next_val);
281287

282288
let should_render = !rc_was_reused && should_render_fn(&next_val, &val);

0 commit comments

Comments
 (0)