Skip to content

Commit

Permalink
Start working on makeing the generated code no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Dec 1, 2021
1 parent a201c31 commit 0e174b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions api/sixtyfps-rs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ pub fn register_font_from_path<P: AsRef<std::path::Path>>(
/// internal re_exports used by the macro generated
#[doc(hidden)]
pub mod re_exports {
pub use alloc::boxed::Box;
pub use alloc::format;
pub use alloc::rc::{Rc, Weak};
pub use alloc::string::String;
pub use alloc::{vec, vec::Vec};
pub use const_field_offset::{self, FieldOffsets, PinnedDrop};
pub use core::iter::FromIterator;
pub use once_cell::race::OnceBox;
Expand Down
23 changes: 12 additions & 11 deletions sixtyfps_compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,7 @@ fn generate_component(
});
let mut model = compile_expression(&repeated.model, &parent_compo);
if repeated.is_conditional_element {
model =
quote!(sixtyfps::re_exports::ModelHandle::new(std::rc::Rc::<bool>::new(#model)))
model = quote!(sixtyfps::re_exports::ModelHandle::new(sixtyfps::re_exports::Rc::<bool>::new(#model)))
}

let self_weak_downgrade = if self.generating_component.is_sub_component() {
Expand Down Expand Up @@ -921,7 +920,7 @@ fn generate_component(

has_window_impl = Some(quote!(
impl sixtyfps::re_exports::WindowHandleAccess for #inner_component_id {
fn window_handle(&self) -> &std::rc::Rc<sixtyfps::re_exports::Window> {
fn window_handle(&self) -> &sixtyfps::re_exports::Rc<sixtyfps::re_exports::Window> {
self.window.window_handle()
}
}
Expand Down Expand Up @@ -1067,7 +1066,7 @@ fn generate_component(
}
} else {
quote! {
let self_rc = ::std::rc::Rc::pin(self_);
let self_rc = sixtyfps::re_exports::Rc::pin(self_);
let _self = self_rc.as_ref();
}
};
Expand All @@ -1086,7 +1085,7 @@ fn generate_component(
let component_handle = if !component.is_global() {
quote!(vtable::VRc<sixtyfps::re_exports::ComponentVTable, Self>)
} else {
quote!(::core::pin::Pin<::std::rc::Rc<Self>>)
quote!(::core::pin::Pin<sixtyfps::re_exports::Rc<Self>>)
};

let public_component_id = public_component_id(component);
Expand Down Expand Up @@ -1185,7 +1184,7 @@ fn generate_component(
component.global_aliases().into_iter().map(|name| ident(&name)).collect::<Vec<_>>();

Some(quote!(
#visibility struct #public_component_id<'a>(&'a ::core::pin::Pin<::std::rc::Rc<#inner_component_id>>);
#visibility struct #public_component_id<'a>(&'a ::core::pin::Pin<sixtyfps::re_exports::Rc<#inner_component_id>>);

impl<'a> #public_component_id<'a> {
#(#property_and_callback_accessors)*
Expand Down Expand Up @@ -1318,7 +1317,7 @@ fn generate_component(
#(#repeated_element_names : sixtyfps::re_exports::Repeater<#repeated_element_components>,)*
#(#self_weak : sixtyfps::re_exports::OnceCell<#self_weak_type>,)*
#(parent : #parent_component_type,)*
#(#global_name : ::core::pin::Pin<::std::rc::Rc<#global_type>>,)*
#(#global_name : ::core::pin::Pin<sixtyfps::re_exports::Rc<#global_type>>,)*
#root_field
#item_tree_index_field
#tree_index_of_first_child_field
Expand Down Expand Up @@ -1590,10 +1589,10 @@ fn compile_expression(expr: &Expression, component: &Rc<Component>) -> TokenStre
let f = compile_expression(&*from, component);
match (from.ty(), to) {
(Type::Float32, Type::String) | (Type::Int32, Type::String) => {
quote!(sixtyfps::re_exports::SharedString::from(format!("{}", #f).as_str()))
quote!(sixtyfps::re_exports::SharedString::from(sixtyfps::re_exports::format!("{}", #f).as_str()))
}
(Type::Float32, Type::Model) | (Type::Int32, Type::Model) => {
quote!(sixtyfps::re_exports::ModelHandle::new(std::rc::Rc::<usize>::new(#f as usize)))
quote!(sixtyfps::re_exports::ModelHandle::new(sixtyfps::re_exports::Rc::<usize>::new(#f as usize)))
}
(Type::Float32, Type::Color) => {
quote!(sixtyfps::re_exports::Color::from_argb_encoded(#f as u32))
Expand Down Expand Up @@ -1949,7 +1948,9 @@ fn compile_expression(expr: &Expression, component: &Rc<Component>) -> TokenStre
let rust_element_ty = rust_type(element_ty).unwrap();
let val = values.iter().map(|e| compile_expression(e, component));
quote!(sixtyfps::re_exports::ModelHandle::new(
std::rc::Rc::new(sixtyfps::re_exports::VecModel::<#rust_element_ty>::from(vec![#(#val as _),*]))
sixtyfps::re_exports::Rc::new(sixtyfps::re_exports::VecModel::<#rust_element_ty>::from(
sixtyfps::re_exports::vec![#(#val as _),*]
))
))
}
Expression::Struct { ty, values } => {
Expand Down Expand Up @@ -2330,7 +2331,7 @@ fn box_layout_data(
}
(
quote! { {
let mut items_vec = Vec::with_capacity(#fixed_count #repeated_count);
let mut items_vec = sixtyfps::re_exports::Vec::with_capacity(#fixed_count #repeated_count);
#push_code
items_vec
} },
Expand Down

0 comments on commit 0e174b6

Please sign in to comment.