Revert "Make Widget::diff mutable"

This reverts commit 497ebcd0c3.
This commit is contained in:
Héctor Ramón Jiménez 2025-08-29 04:21:43 +02:00
parent caeb9ce49c
commit 6be707267e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
31 changed files with 83 additions and 121 deletions

View file

@ -9,7 +9,7 @@ use crate::{
Vector, Widget,
};
use std::borrow::{Borrow, BorrowMut};
use std::borrow::Borrow;
/// A generic [`Widget`].
///
@ -239,37 +239,6 @@ impl<'a, Message, Theme, Renderer>
}
}
impl<'a, Message, Theme, Renderer>
Borrow<dyn Widget<Message, Theme, Renderer> + 'a>
for &mut Element<'a, Message, Theme, Renderer>
{
fn borrow(&self) -> &(dyn Widget<Message, Theme, Renderer> + 'a) {
self.widget.borrow()
}
}
impl<'a, Message, Theme, Renderer>
BorrowMut<dyn Widget<Message, Theme, Renderer> + 'a>
for Element<'a, Message, Theme, Renderer>
{
fn borrow_mut(
&mut self,
) -> &mut (dyn Widget<Message, Theme, Renderer> + 'a) {
self.widget.borrow_mut()
}
}
impl<'a, Message, Theme, Renderer>
BorrowMut<dyn Widget<Message, Theme, Renderer> + 'a>
for &mut Element<'a, Message, Theme, Renderer>
{
fn borrow_mut(
&mut self,
) -> &mut (dyn Widget<Message, Theme, Renderer> + 'a) {
self.widget.borrow_mut()
}
}
struct Map<'a, A, B, Theme, Renderer> {
widget: Box<dyn Widget<A, Theme, Renderer> + 'a>,
mapper: Box<dyn Fn(A) -> B + 'a>,
@ -309,7 +278,7 @@ where
self.widget.children()
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
self.widget.diff(tree);
}
@ -452,7 +421,7 @@ where
self.element.widget.children()
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
self.element.widget.diff(tree);
}

View file

@ -96,7 +96,7 @@ where
}
/// Reconciles the [`Widget`] with the provided [`Tree`].
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
tree.children.clear();
}

View file

@ -2,7 +2,7 @@
use crate::Widget;
use std::any::{self, Any};
use std::borrow::{Borrow, BorrowMut};
use std::borrow::Borrow;
use std::fmt;
/// A persistent state widget tree.
@ -56,12 +56,12 @@ impl Tree {
/// [`Widget::diff`]: crate::Widget::diff
pub fn diff<'a, Message, Theme, Renderer>(
&mut self,
mut new: impl BorrowMut<dyn Widget<Message, Theme, Renderer> + 'a>,
new: impl Borrow<dyn Widget<Message, Theme, Renderer> + 'a>,
) where
Renderer: crate::Renderer,
{
if self.tag == new.borrow_mut().tag() {
new.borrow_mut().diff(self);
if self.tag == new.borrow().tag() {
new.borrow().diff(self);
} else {
*self = Self::new(new);
}
@ -70,15 +70,13 @@ impl Tree {
/// Reconciles the children of the tree with the provided list of widgets.
pub fn diff_children<'a, Message, Theme, Renderer>(
&mut self,
new_children: &mut [impl BorrowMut<
dyn Widget<Message, Theme, Renderer> + 'a,
>],
new_children: &[impl Borrow<dyn Widget<Message, Theme, Renderer> + 'a>],
) where
Renderer: crate::Renderer,
{
self.diff_children_custom(
new_children,
|tree, widget| tree.diff(widget.borrow_mut()),
|tree, widget| tree.diff(widget.borrow()),
|widget| Self::new(widget.borrow()),
);
}
@ -87,8 +85,8 @@ impl Tree {
/// logic both for diffing and creating new widget state.
pub fn diff_children_custom<T>(
&mut self,
new_children: &mut [T],
diff: impl Fn(&mut Tree, &mut T),
new_children: &[T],
diff: impl Fn(&mut Tree, &T),
new_state: impl Fn(&T) -> Self,
) {
if self.children.len() > new_children.len() {
@ -96,7 +94,7 @@ impl Tree {
}
for (child_state, new) in
self.children.iter_mut().zip(new_children.iter_mut())
self.children.iter_mut().zip(new_children.iter())
{
diff(child_state, new);
}
@ -116,8 +114,8 @@ impl Tree {
/// `maybe_changed` closure.
pub fn diff_children_custom_with_search<T>(
current_children: &mut Vec<Tree>,
new_children: &mut [T],
diff: impl Fn(&mut Tree, &mut T),
new_children: &[T],
diff: impl Fn(&mut Tree, &T),
maybe_changed: impl Fn(usize) -> bool,
new_state: impl Fn(&T) -> Tree,
) {
@ -185,7 +183,7 @@ pub fn diff_children_custom_with_search<T>(
// TODO: Merge loop with extend logic (?)
for (child_state, new) in
current_children.iter_mut().zip(new_children.iter_mut())
current_children.iter_mut().zip(new_children.iter())
{
diff(child_state, new);
}

View file

@ -87,8 +87,8 @@ mod loupe {
self.content.as_widget().children()
}
fn diff(&mut self, tree: &mut widget::Tree) {
self.content.as_widget_mut().diff(tree);
fn diff(&self, tree: &mut widget::Tree) {
self.content.as_widget().diff(tree);
}
fn size(&self) -> Size<Length> {

View file

@ -314,7 +314,7 @@ mod toast {
.collect()
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
let instants = tree.state.downcast_mut::<Vec<Option<Instant>>>();
// Invalidating removed instants to None allows us to remove
@ -336,8 +336,8 @@ mod toast {
}
tree.diff_children(
&mut std::iter::once(&mut self.content)
.chain(self.toasts.iter_mut())
&std::iter::once(&self.content)
.chain(self.toasts.iter())
.collect::<Vec<_>>(),
);
}

View file

@ -100,7 +100,7 @@ where
let mut root = root.into();
let Cache { mut state } = cache;
state.diff(root.as_widget_mut());
state.diff(root.as_widget());
let base = root.as_widget_mut().layout(
&mut state,

View file

@ -223,8 +223,8 @@ where
vec![Tree::new(&self.content)]
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(std::slice::from_mut(&mut self.content));
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content));
}
fn size(&self) -> Size<Length> {

View file

@ -194,8 +194,8 @@ where
self.children.iter().map(Tree::new).collect()
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(&mut self.children);
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children);
}
fn size(&self) -> Size<Length> {

View file

@ -509,7 +509,7 @@ where
vec![widget::Tree::new(&self.text_input as &dyn Widget<_, _, _>)]
}
fn diff(&mut self, _tree: &mut widget::Tree) {
fn diff(&self, _tree: &mut widget::Tree) {
// do nothing so the children don't get cleared
}

View file

@ -247,8 +247,8 @@ where
self.content.as_widget().children()
}
fn diff(&mut self, tree: &mut Tree) {
self.content.as_widget_mut().diff(tree);
fn diff(&self, tree: &mut Tree) {
self.content.as_widget().diff(tree);
}
fn size(&self) -> Size<Length> {

View file

@ -103,8 +103,8 @@ where
self.content.as_widget().children()
}
fn diff(&mut self, tree: &mut widget::Tree) {
self.content.as_widget_mut().diff(tree);
fn diff(&self, tree: &mut widget::Tree) {
self.content.as_widget().diff(tree);
}
fn size(&self) -> Size<Length> {

View file

@ -158,8 +158,8 @@ where
self.children.iter().map(Tree::new).collect()
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(&mut self.children);
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children);
}
fn size(&self) -> Size<Length> {

View file

@ -598,8 +598,8 @@ where
self.content.as_widget().children()
}
fn diff(&mut self, tree: &mut Tree) {
self.content.as_widget_mut().diff(tree);
fn diff(&self, tree: &mut Tree) {
self.content.as_widget().diff(tree);
}
fn size(&self) -> Size<Length> {
@ -761,8 +761,8 @@ where
vec![Tree::new(&self.base), Tree::new(&self.top)]
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(&mut [&mut self.base, &mut self.top]);
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&[&self.base, &self.top]);
}
fn size(&self) -> Size<Length> {

View file

@ -222,7 +222,7 @@ where
self.children.iter().map(Tree::new).collect()
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
let Tree {
state, children, ..
} = tree;
@ -231,8 +231,8 @@ where
tree::diff_children_custom_with_search(
children,
&mut self.children,
|tree, child| child.as_widget_mut().diff(tree),
&self.children,
|tree, child| child.as_widget().diff(tree),
|index| {
self.keys.get(index).or_else(|| self.keys.last()).copied()
!= Some(state.keys[index])

View file

@ -125,7 +125,7 @@ where
self.with_element(|element| vec![Tree::new(element.as_widget())])
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
let current = tree
.state
.downcast_mut::<Internal<Message, Theme, Renderer>>();
@ -144,8 +144,8 @@ where
current.element = Rc::new(RefCell::new(Some(element)));
(*self.element.borrow_mut()) = Some(current.element.clone());
self.with_element_mut(|element| {
tree.diff_children(std::slice::from_mut(element));
self.with_element(|element| {
tree.diff_children(std::slice::from_ref(&element.as_widget()));
});
} else {
(*self.element.borrow_mut()) = Some(current.element.clone());

View file

@ -147,13 +147,13 @@ where
Renderer: renderer::Renderer,
{
fn diff_self(&self) {
self.with_element_mut(|element| {
self.with_element(|element| {
self.tree
.borrow_mut()
.borrow_mut()
.as_mut()
.unwrap()
.diff_children(std::slice::from_mut(element));
.diff_children(std::slice::from_ref(&element));
});
}
@ -279,7 +279,7 @@ where
vec![]
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
let tree = tree.state.downcast_ref::<Rc<RefCell<Option<Tree>>>>();
*self.tree.borrow_mut() = tree.clone();
self.rebuild_element_if_necessary();

View file

@ -181,8 +181,8 @@ where
vec![Tree::new(&self.content)]
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(std::slice::from_mut(&mut self.content));
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content));
}
fn size(&self) -> Size<Length> {

View file

@ -205,7 +205,7 @@ where
class,
} = menu;
let mut list = Scrollable::new(List {
let list = Scrollable::new(List {
options,
hovered_option,
on_selected,
@ -218,7 +218,7 @@ where
class,
});
state.tree.diff(&mut list as &mut dyn Widget<_, _, _>);
state.tree.diff(&list as &dyn Widget<_, _, _>);
Self {
position,

View file

@ -378,7 +378,7 @@ where
self.contents.iter().map(Content::state).collect()
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
let Memory { order, .. } = tree.state.downcast_ref();
// `Pane` always increments and is iterated by Ord so new
@ -401,7 +401,7 @@ where
});
tree.diff_children_custom(
&mut self.contents,
&self.contents,
|state, content| content.diff(state),
Content::state,
);

View file

@ -91,13 +91,13 @@ where
}
}
pub(super) fn diff(&mut self, tree: &mut Tree) {
pub(super) fn diff(&self, tree: &mut Tree) {
if tree.children.len() == 2 {
if let Some(title_bar) = self.title_bar.as_mut() {
if let Some(title_bar) = self.title_bar.as_ref() {
title_bar.diff(&mut tree.children[1]);
}
tree.children[0].diff(&mut self.body);
tree.children[0].diff(&self.body);
} else {
*tree = self.state();
}

View file

@ -128,17 +128,17 @@ where
}
}
pub(super) fn diff(&mut self, tree: &mut Tree) {
pub(super) fn diff(&self, tree: &mut Tree) {
if tree.children.len() == 3 {
if let Some(controls) = self.controls.as_mut() {
if let Some(compact) = controls.compact.as_mut() {
if let Some(controls) = self.controls.as_ref() {
if let Some(compact) = controls.compact.as_ref() {
tree.children[2].diff(compact);
}
tree.children[1].diff(&mut controls.full);
tree.children[1].diff(&controls.full);
}
tree.children[0].diff(&mut self.content);
tree.children[0].diff(&self.content);
} else {
*tree = self.state();
}

View file

@ -127,8 +127,8 @@ where
self.content.as_widget().children()
}
fn diff(&mut self, tree: &mut widget::Tree) {
self.content.as_widget_mut().diff(tree);
fn diff(&self, tree: &mut widget::Tree) {
self.content.as_widget().diff(tree);
}
fn size(&self) -> Size<Length> {

View file

@ -3,7 +3,7 @@ use crate::core::mouse;
use crate::core::overlay;
use crate::core::renderer;
use crate::core::widget;
use crate::core::widget::tree::{self, Tree};
use crate::core::widget::Tree;
use crate::core::{
self, Clipboard, Element, Event, Length, Rectangle, Shell, Size, Vector,
Widget,
@ -66,12 +66,7 @@ impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
where
Renderer: core::Renderer,
{
fn tag(&self) -> tree::Tag {
struct Marker;
tree::Tag::of::<Marker>()
}
fn diff(&mut self, _tree: &mut Tree) {
fn diff(&self, _tree: &mut Tree) {
// Diff is deferred to layout
}
@ -92,7 +87,7 @@ where
let size = limits.max();
self.content = (self.view)(size);
tree.diff_children(std::slice::from_mut(&mut self.content));
tree.diff_children(std::slice::from_ref(&self.content));
let node = self.content.as_widget_mut().layout(
&mut tree.children[0],

View file

@ -196,8 +196,8 @@ where
self.children.iter().map(Tree::new).collect()
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(&mut self.children);
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children);
}
fn size(&self) -> Size<Length> {
@ -398,7 +398,7 @@ where
self.row.children()
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
self.row.diff(tree);
}

View file

@ -399,8 +399,8 @@ where
vec![Tree::new(&self.content)]
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(std::slice::from_mut(&mut self.content));
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content));
}
fn size(&self) -> Size<Length> {

View file

@ -184,8 +184,8 @@ where
vec![Tree::new(&self.content)]
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(std::slice::from_mut(&mut self.content));
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&[&self.content]);
}
fn update(

View file

@ -146,8 +146,8 @@ where
self.children.iter().map(Tree::new).collect()
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(&mut self.children);
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children);
}
fn size(&self) -> Size<Length> {

View file

@ -227,8 +227,8 @@ where
.collect()
}
fn diff(&mut self, state: &mut widget::Tree) {
state.diff_children(&mut self.cells);
fn diff(&self, state: &mut widget::Tree) {
state.diff_children(&self.cells);
}
fn layout(

View file

@ -654,7 +654,7 @@ where
tree::State::new(State::<Renderer::Paragraph>::new())
}
fn diff(&mut self, tree: &mut Tree) {
fn diff(&self, tree: &mut Tree) {
let state = tree.state.downcast_mut::<State<Renderer::Paragraph>>();
// Stop pasting if input becomes disabled

View file

@ -81,8 +81,8 @@ where
self.content.as_widget().children()
}
fn diff(&mut self, tree: &mut Tree) {
self.content.as_widget_mut().diff(tree);
fn diff(&self, tree: &mut Tree) {
self.content.as_widget().diff(tree);
}
fn size(&self) -> Size<Length> {

View file

@ -155,10 +155,10 @@ where
]
}
fn diff(&mut self, tree: &mut widget::Tree) {
tree.diff_children(&mut [
self.content.as_widget_mut(),
self.tooltip.as_widget_mut(),
fn diff(&self, tree: &mut widget::Tree) {
tree.diff_children(&[
self.content.as_widget(),
self.tooltip.as_widget(),
]);
}