Skip to content

Commit

Permalink
Automatically derive ValueDefault for primitive value types (#5793)
Browse files Browse the repository at this point in the history
### Description

This PR automatically derives `ValueDefault` for primitive value types.
This means that you can replace `Vc::<String>::empty()` with
`Default::default()`. The same goes for all primitive value types
(`bool`, `u8`, `()`, etc.). The underlying type's `Default`
implementation will be used.

### Testing Instructions

N/A

Closes WEB-1430
  • Loading branch information
alexkirsz authored Aug 25, 2023
1 parent faf918f commit f506772
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
56 changes: 47 additions & 9 deletions crates/turbo-tasks-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ impl<'a> RegisterContext<'a> {

self.add_value(&ident);
self.add_value_debug_impl(&ident);
self.add_value_default_impl(&ident);
} else if macro_item
.mac
.path
Expand Down Expand Up @@ -409,6 +410,18 @@ impl<'a> RegisterContext<'a> {
);
}

fn add_value_default_impl(&mut self, ident: &Ident) {
// register default ValueDefault impl generated by proc macro
self.register_default_impl(ident).unwrap();
self.add_value_trait(
ident,
&get_type_ident(&parse_quote! {
turbo_tasks::ValueDefault
})
.unwrap(),
);
}

fn add_value_trait(&mut self, ident: &Ident, trait_ident: &Ident) {
let key: ValueKey = (self.mod_path.to_owned(), ident.clone());

Expand Down Expand Up @@ -437,25 +450,50 @@ impl<'a> RegisterContext<'a> {
)
}

/// Declares the default derive of the `ValueDebug` trait.
fn register_debug_impl(&mut self, ident: &Ident) -> std::fmt::Result {
for fn_name in ["dbg", "dbg_depth"] {
/// Declares a derive of the given trait and its methods.
fn register_impl(
&mut self,
ident: &Ident,
trait_ident: &Ident,
fn_names: &[&'static str],
) -> std::fmt::Result {
for fn_name in fn_names {
let fn_ident = Ident::new(fn_name, ident.span());
let trait_ident = get_type_ident(&parse_quote! {
turbo_tasks::debug::ValueDebug
})
.unwrap();

let (impl_fn_ident, global_name) = (
get_trait_impl_function_ident(ident, &trait_ident, &fn_ident),
self.get_global_name(&[ident, &trait_ident, &fn_ident]),
get_trait_impl_function_ident(ident, trait_ident, &fn_ident),
self.get_global_name(&[ident, trait_ident, &fn_ident]),
);

self.register(impl_fn_ident, global_name)?;
}

Ok(())
}

/// Declares the default derive of the `ValueDebug` trait.
fn register_debug_impl(&mut self, ident: &Ident) -> std::fmt::Result {
self.register_impl(
ident,
&get_type_ident(&parse_quote! {
turbo_tasks::debug::ValueDebug
})
.unwrap(),
&["dbg", "dbg_depth"],
)
}

/// Declares the default derive of the `ValueDefault` trait.
fn register_default_impl(&mut self, ident: &Ident) -> std::fmt::Result {
self.register_impl(
ident,
&get_type_ident(&parse_quote! {
turbo_tasks::ValueDefault
})
.unwrap(),
&["value_default"],
)
}
}

fn has_attribute(attrs: &[Attribute], name: &str) -> bool {
Expand Down
12 changes: 12 additions & 0 deletions crates/turbo-tasks-macros/src/primitive_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ pub fn primitive(input: TokenStream) -> TokenStream {
},
);

let value_default_impl = quote! {
#[turbo_tasks::value_impl]
impl turbo_tasks::ValueDefault for #ty {
#[turbo_tasks::function]
fn value_default() -> Vc<Self> {
Vc::cell(Default::default())
}
}
};

quote! {
#value_type_and_register

#value_debug_impl

#value_default_impl
}
.into()
}
9 changes: 3 additions & 6 deletions crates/turbo-tasks/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn empty_string() -> Vc<String> {
}

impl Vc<String> {
#[deprecated(note = "use Default::default() instead")]
#[inline(always)]
pub fn empty() -> Vc<String> {
empty_string()
Expand All @@ -33,6 +34,7 @@ fn empty_string_vec() -> Vc<Vec<String>> {
}

impl Vc<Vec<String>> {
#[deprecated(note = "use Default::default() instead")]
#[inline(always)]
pub fn empty() -> Vc<Vec<String>> {
empty_string_vec()
Expand All @@ -47,17 +49,12 @@ fn option_string_none() -> Vc<Option<String>> {
}

impl Vc<Option<String>> {
#[deprecated(note = "use Default::default() instead")]
pub fn none() -> Self {
option_string_none()
}
}

impl Default for Vc<Option<String>> {
fn default() -> Self {
Self::none()
}
}

__turbo_tasks_internal_primitive!(bool);
__turbo_tasks_internal_primitive!(u8);
__turbo_tasks_internal_primitive!(u16);
Expand Down
11 changes: 2 additions & 9 deletions crates/turbo-tasks/src/unit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use crate::{ValueDefault, Vc};
use crate::Vc;

// TODO(alexkirsz) Should this be `#[turbo_tasks::function]` or is it okay to
// always return a new `Vc`?
#[deprecated(note = "use Default::default() instead")]
pub fn unit() -> Vc<()> {
Vc::cell(())
}

impl ValueDefault for () {
// TODO(alexkirsz) Should this be `#[turbo_tasks::function]` or is it
// preferrable to always return a new `Vc`?
fn value_default() -> Vc<Self> {
Vc::cell(())
}
}

0 comments on commit f506772

Please sign in to comment.