Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
Some of these suggestions are kind of bad
but that's by-the-by
  • Loading branch information
DJMcNab committed Apr 25, 2024
1 parent 5fd9474 commit 7355a71
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
4 changes: 4 additions & 0 deletions crates/masonry/src/widget/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ impl Flex {
pub fn len(&self) -> usize {
self.children.len()
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

// --- Mutate live Flex - WidgetMut ---
Expand Down
16 changes: 11 additions & 5 deletions crates/xilem_masonry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::comparison_chain)]
use std::{any::Any, collections::HashMap};

use masonry::{
Expand Down Expand Up @@ -49,18 +50,18 @@ masonry::declare_widget!(RootWidgetMut, RootWidget<E: (Widget)>);

impl<E: 'static + Widget> Widget for RootWidget<E> {
fn on_pointer_event(&mut self, ctx: &mut EventCtx, event: &PointerEvent) {
self.pod.on_pointer_event(ctx, event)
self.pod.on_pointer_event(ctx, event);
}
fn on_text_event(&mut self, ctx: &mut EventCtx, event: &TextEvent) {
self.pod.on_text_event(ctx, event)
self.pod.on_text_event(ctx, event);
}

fn on_status_change(&mut self, _: &mut LifeCycleCtx, _: &StatusChange) {
// Intentionally do nothing?
}

fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle) {
self.pod.lifecycle(ctx, event)
self.pod.lifecycle(ctx, event);
}

fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
Expand All @@ -70,7 +71,7 @@ impl<E: 'static + Widget> Widget for RootWidget<E> {
}

fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut Scene) {
self.pod.paint(ctx, scene)
self.pod.paint(ctx, scene);
}

fn children(&self) -> SmallVec<[WidgetRef<'_, dyn Widget>; 16]> {
Expand Down Expand Up @@ -114,7 +115,8 @@ where

let changed = next_view.rebuild(&mut self.view_cx, &self.current_view, element);
if !changed.changed {
tracing::debug!("TODO: Skip some work?")
// Masonry manages all of this itself - ChangeFlags is probably not needed?
tracing::debug!("TODO: Skip some work?");
}
self.current_view = next_view;
}
Expand Down Expand Up @@ -216,6 +218,10 @@ impl ChangeFlags {
}

pub struct ViewCx {
/// The map from a widgets id to its position in the View tree.
///
/// This includes only the widgets which might send actions
/// This is currently never cleaned up
widget_map: HashMap<WidgetId, Vec<ViewId>>,
id_path: Vec<ViewId>,
}
Expand Down
11 changes: 7 additions & 4 deletions crates/xilem_masonry/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use masonry::{widget::WidgetMut, Widget, WidgetPod};

use crate::{ChangeFlags, MasonryView, MessageResult, ViewCx, ViewId};

#[allow(clippy::len_without_is_empty)]
pub trait ElementSplice {
/// Insert a new element at the current index in the resulting collection (and increment the index by 1)
fn push(&mut self, element: WidgetPod<Box<dyn Widget>>);
/// Mutate the next existing element, and add it to the resulting collection (and increment the index by 1)
// TODO: This should actually return `WidgetMut<dyn Widget>`, but that isn't supported in Masonry itself yet
fn mutate<'a>(&'a mut self) -> WidgetMut<Box<dyn Widget>>;
fn mutate(&mut self) -> WidgetMut<Box<dyn Widget>>;
/// Delete the next n existing elements (this doesn't change the index)
fn delete(&mut self, n: usize);
/// Current length of the elements collection
Expand Down Expand Up @@ -91,7 +92,7 @@ impl<State, Action, View: MasonryView<State, Action>> ViewSequence<State, Action
message: Box<dyn std::any::Any>,
app_state: &mut State,
) -> MessageResult<Action> {
self.message(&id_path, message, app_state)
self.message(id_path, message, app_state)
}

fn count(&self) -> usize {
Expand Down Expand Up @@ -140,7 +141,7 @@ impl<State, Action, Marker, VT: ViewSequence<State, Action, Marker>>
app_state: &mut State,
) -> MessageResult<Action> {
if let Some(this) = self {
this.message(&id_path, message, app_state)
this.message(id_path, message, app_state)
} else {
MessageResult::Stale(message)
}
Expand All @@ -163,7 +164,7 @@ impl<T, A, Marker, VT: ViewSequence<T, A, Marker>> ViewSequence<T, A, (WasASeque
let i: u64 = i.try_into().unwrap();
let id = NonZeroU64::new(i + 1).unwrap();
cx.with_id(ViewId::for_type::<VT>(id), |cx| child.build(cx, elements));
})
});
}

fn rebuild(
Expand All @@ -187,6 +188,8 @@ impl<T, A, Marker, VT: ViewSequence<T, A, Marker>> ViewSequence<T, A, (WasASeque
elements.delete(n_delete);
changed.changed |= ChangeFlags::CHANGED.changed;
} else if n > prev.len() {
// This suggestion from clippy is kind of bad, because we use the absolute index in the id
#[allow(clippy::needless_range_loop)]
for ix in prev.len()..n {
let id_u64: u64 = ix.try_into().unwrap();
let id = NonZeroU64::new(id_u64 + 1).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions crates/xilem_masonry/src/vec_splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ impl<'a, 'b, T> VecSplice<'a, 'b, T> {

impl ElementSplice for VecSplice<'_, '_, masonry::WidgetPod<Box<dyn masonry::Widget>>> {
fn push(&mut self, element: masonry::WidgetPod<Box<dyn masonry::Widget>>) {
self.push(element)
self.push(element);
}

fn mutate<'a>(&'a mut self) -> WidgetMut<Box<dyn masonry::Widget>> {
fn mutate(&mut self) -> WidgetMut<Box<dyn masonry::Widget>> {
unreachable!("VecSplice can only be used for `build`, not rebuild")
}

fn delete(&mut self, n: usize) {
self.delete(n)
self.delete(n);
}

fn len(&self) -> usize {
Expand Down
6 changes: 3 additions & 3 deletions crates/xilem_masonry/src/view/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
"ViewSequence shouldn't leave splice in strange state"
);
for item in elements.drain(..) {
view = view.with_child_pod(item);
view = view.with_child_pod(item).with_default_spacer();
}
WidgetPod::new(view)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl ElementSplice for FlexSplice<'_> {
self.ix += 1;
}

fn mutate<'a>(&'a mut self) -> WidgetMut<'a, Box<dyn Widget>> {
fn mutate(&mut self) -> WidgetMut<Box<dyn Widget>> {
#[cfg(debug_assertions)]
let mut iterations = 0;
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -109,7 +109,7 @@ impl ElementSplice for FlexSplice<'_> {
deleted_count += 1;
}
}
self.element.remove_child(self.ix)
self.element.remove_child(self.ix);
}
}

Expand Down

0 comments on commit 7355a71

Please sign in to comment.