From 56a75a69e5b737fa29ce2ef41dca05b792d0dd1a Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 19 Jun 2020 10:06:13 +0200 Subject: [PATCH] Move the repeater part that is specific to rust within the rust lib --- api/sixtyfps-rs/lib.rs | 4 ++- api/sixtyfps-rs/repeater.rs | 37 ++++++++++++++++++++++++++ sixtyfps_runtime/corelib/abi/model.rs | 38 --------------------------- 3 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 api/sixtyfps-rs/repeater.rs diff --git a/api/sixtyfps-rs/lib.rs b/api/sixtyfps-rs/lib.rs index 5e4bcd3b9..8bc6aa573 100644 --- a/api/sixtyfps-rs/lib.rs +++ b/api/sixtyfps-rs/lib.rs @@ -66,13 +66,15 @@ fn main() { pub use sixtyfps_rs_macro::sixtyfps; +pub(crate) mod repeater; + /// internal re_exports used by the macro generated #[doc(hidden)] pub mod re_exports { + pub use crate::repeater::*; pub use const_field_offset::{self, FieldOffsets}; pub use once_cell::sync::Lazy; pub use sixtyfps_corelib::abi::datastructures::*; - pub use sixtyfps_corelib::abi::model::*; pub use sixtyfps_corelib::abi::primitives::*; pub use sixtyfps_corelib::abi::properties::Property; pub use sixtyfps_corelib::abi::signals::Signal; diff --git a/api/sixtyfps-rs/repeater.rs b/api/sixtyfps-rs/repeater.rs new file mode 100644 index 000000000..ccd713356 --- /dev/null +++ b/api/sixtyfps-rs/repeater.rs @@ -0,0 +1,37 @@ +/// Component that can be instantiated by a repeater. +pub trait RepeatedComponent: sixtyfps_corelib::abi::datastructures::Component { + /// The data corresponding to the model + type Data; + + /// Update this component at the given index and the given data + fn update(&self, index: usize, data: &Self::Data); +} + +/// This field is put in a component when using the `for` syntax +/// It helps instantiating the components `C` +#[derive(Default)] +pub struct Repeater { + components: Vec>, +} + +impl Repeater +where + C: RepeatedComponent, +{ + /// Called when the model is changed + pub fn update_model(&mut self, data: &[Data]) { + self.components.clear(); + for (i, d) in data.iter().enumerate() { + let c = C::create(); + c.update(i, d); + self.components.push(Box::new(c)); + } + } + + /// Call the visitor for each component + pub fn visit(&self, mut visitor: sixtyfps_corelib::abi::datastructures::ItemVisitorRefMut) { + for c in &self.components { + c.visit_children_item(-1, visitor.borrow_mut()); + } + } +} diff --git a/sixtyfps_runtime/corelib/abi/model.rs b/sixtyfps_runtime/corelib/abi/model.rs index 95dbddd78..7cb303e46 100644 --- a/sixtyfps_runtime/corelib/abi/model.rs +++ b/sixtyfps_runtime/corelib/abi/model.rs @@ -26,41 +26,3 @@ pub struct ComponentVecHolder { _todo: Vec>, } */ - -/// Component that can be instantiated by a repeater. -pub trait RepeatedComponent: crate::abi::datastructures::Component { - /// The data corresponding to the model - type Data; - - /// Update this component at the given index and the given data - fn update(&self, index: usize, data: &Self::Data); -} - -/// This field is put in a component when using the `for` syntax -/// It helps instantiating the components `C` -#[derive(Default)] -pub struct Repeater { - components: Vec>, -} - -impl Repeater -where - C: RepeatedComponent, -{ - /// Called when the model is changed - pub fn update_model(&mut self, data: &[Data]) { - self.components.clear(); - for (i, d) in data.iter().enumerate() { - let c = C::create(); - c.update(i, d); - self.components.push(Box::new(c)); - } - } - - /// Call the visitor for each component - pub fn visit(&self, mut visitor: super::datastructures::ItemVisitorRefMut) { - for c in &self.components { - c.visit_children_item(-1, visitor.borrow_mut()); - } - } -}