From 1cdc1fcd0669bfea096237c07b32742c1a3f2158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 3 Nov 2022 02:46:31 +0100 Subject: [PATCH] Rename `iced_lazy::Cached` to `Lazy` :tada: --- examples/cached/src/main.rs | 59 ++++++++++++++++----------------- lazy/src/{cached.rs => lazy.rs} | 14 ++++---- lazy/src/lib.rs | 17 ++++++++-- 3 files changed, 50 insertions(+), 40 deletions(-) rename lazy/src/{cached.rs => lazy.rs} (96%) diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index 39364dc9..7c8b06f0 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -3,7 +3,7 @@ use iced::widget::{ button, column, horizontal_space, row, scrollable, text, text_input, }; use iced::{Element, Length, Sandbox, Settings}; -use iced_lazy::Cached; +use iced_lazy::lazy; use std::collections::HashSet; @@ -71,39 +71,36 @@ impl Sandbox for App { } fn view(&self) -> Element { - let options = - Cached::new((&self.sort_order, self.options.len()), || { - let mut options: Vec<_> = self.options.iter().collect(); + let options = lazy((&self.sort_order, self.options.len()), || { + let mut options: Vec<_> = self.options.iter().collect(); - options.sort_by(|a, b| match self.sort_order { - SortOrder::Ascending => { - a.to_lowercase().cmp(&b.to_lowercase()) - } - SortOrder::Descending => { - b.to_lowercase().cmp(&a.to_lowercase()) - } - }); - - column( - options - .into_iter() - .map(|option| { - row![ - text(option), - horizontal_space(Length::Fill), - button("Delete") - .on_press(Message::DeleteOption( - option.to_string(), - ),) - .style(theme::Button::Destructive) - ] - .into() - }) - .collect(), - ) - .spacing(10) + options.sort_by(|a, b| match self.sort_order { + SortOrder::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), + SortOrder::Descending => { + b.to_lowercase().cmp(&a.to_lowercase()) + } }); + column( + options + .into_iter() + .map(|option| { + row![ + text(option), + horizontal_space(Length::Fill), + button("Delete") + .on_press(Message::DeleteOption( + option.to_string(), + ),) + .style(theme::Button::Destructive) + ] + .into() + }) + .collect(), + ) + .spacing(10) + }); + column![ scrollable(options).height(Length::Fill), row![ diff --git a/lazy/src/cached.rs b/lazy/src/lazy.rs similarity index 96% rename from lazy/src/cached.rs rename to lazy/src/lazy.rs index 931184b5..d61cc77e 100644 --- a/lazy/src/cached.rs +++ b/lazy/src/lazy.rs @@ -15,14 +15,14 @@ use std::marker::PhantomData; use std::rc::Rc; #[allow(missing_debug_implementations)] -pub struct Cached<'a, Message, Renderer, Dependency, View> { +pub struct Lazy<'a, Message, Renderer, Dependency, View> { dependency: Dependency, view: Box View + 'a>, element: RefCell>>>>, } impl<'a, Message, Renderer, Dependency, View> - Cached<'a, Message, Renderer, Dependency, View> + Lazy<'a, Message, Renderer, Dependency, View> where Dependency: Hash + 'a, View: Into>, @@ -56,7 +56,7 @@ struct Internal { } impl<'a, Message, Renderer, Dependency, View> Widget - for Cached<'a, Message, Renderer, Dependency, View> + for Lazy<'a, Message, Renderer, Dependency, View> where View: Into> + 'static, Dependency: Hash + 'a, @@ -237,7 +237,7 @@ where #[self_referencing] struct Overlay<'a, 'b, Message, Renderer, Dependency, View> { - cached: &'a Cached<'b, Message, Renderer, Dependency, View>, + cached: &'a Lazy<'b, Message, Renderer, Dependency, View>, tree: &'a mut Tree, types: PhantomData<(Message, Dependency, View)>, @@ -348,7 +348,7 @@ where } impl<'a, Message, Renderer, Dependency, View> - From> + From> for Element<'a, Message, Renderer> where View: Into> + 'static, @@ -356,7 +356,7 @@ where Message: 'static, Dependency: Hash + 'a, { - fn from(cached: Cached<'a, Message, Renderer, Dependency, View>) -> Self { - Self::new(cached) + fn from(lazy: Lazy<'a, Message, Renderer, Dependency, View>) -> Self { + Self::new(lazy) } } diff --git a/lazy/src/lib.rs b/lazy/src/lib.rs index c01b439b..f49fe4b6 100644 --- a/lazy/src/lib.rs +++ b/lazy/src/lib.rs @@ -17,17 +17,30 @@ clippy::type_complexity )] #![cfg_attr(docsrs, feature(doc_cfg))] -pub mod cached; +mod lazy; + pub mod component; pub mod responsive; -pub use cached::Cached; pub use component::Component; +pub use lazy::Lazy; pub use responsive::Responsive; mod cache; use iced_native::{Element, Size}; +use std::hash::Hash; + +pub fn lazy<'a, Message, Renderer, Dependency, View>( + dependency: Dependency, + view: impl Fn() -> View + 'a, +) -> Lazy<'a, Message, Renderer, Dependency, View> +where + Dependency: Hash + 'a, + View: Into>, +{ + Lazy::new(dependency, view) +} /// Turns an implementor of [`Component`] into an [`Element`] that can be /// embedded in any application.