diff --git a/crates/turbo-tasks-build/src/lib.rs b/crates/turbo-tasks-build/src/lib.rs index 97bb31f0d610d..4f2548c9aee9f 100644 --- a/crates/turbo-tasks-build/src/lib.rs +++ b/crates/turbo-tasks-build/src/lib.rs @@ -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 @@ -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()); @@ -437,18 +450,19 @@ 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)?; @@ -456,6 +470,30 @@ impl<'a> RegisterContext<'a> { 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 { diff --git a/crates/turbo-tasks-macros/src/primitive_macro.rs b/crates/turbo-tasks-macros/src/primitive_macro.rs index e6a2590805162..2d0b99e857ae6 100644 --- a/crates/turbo-tasks-macros/src/primitive_macro.rs +++ b/crates/turbo-tasks-macros/src/primitive_macro.rs @@ -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 { + Vc::cell(Default::default()) + } + } + }; + quote! { #value_type_and_register #value_debug_impl + + #value_default_impl } .into() } diff --git a/crates/turbo-tasks/src/primitives.rs b/crates/turbo-tasks/src/primitives.rs index ccb89fc209fe8..b14773ecb8edd 100644 --- a/crates/turbo-tasks/src/primitives.rs +++ b/crates/turbo-tasks/src/primitives.rs @@ -18,6 +18,7 @@ fn empty_string() -> Vc { } impl Vc { + #[deprecated(note = "use Default::default() instead")] #[inline(always)] pub fn empty() -> Vc { empty_string() @@ -33,6 +34,7 @@ fn empty_string_vec() -> Vc> { } impl Vc> { + #[deprecated(note = "use Default::default() instead")] #[inline(always)] pub fn empty() -> Vc> { empty_string_vec() @@ -47,17 +49,12 @@ fn option_string_none() -> Vc> { } impl Vc> { + #[deprecated(note = "use Default::default() instead")] pub fn none() -> Self { option_string_none() } } -impl Default for Vc> { - fn default() -> Self { - Self::none() - } -} - __turbo_tasks_internal_primitive!(bool); __turbo_tasks_internal_primitive!(u8); __turbo_tasks_internal_primitive!(u16); diff --git a/crates/turbo-tasks/src/unit.rs b/crates/turbo-tasks/src/unit.rs index d3af9af7b84a9..15e6a2f4c0ebe 100644 --- a/crates/turbo-tasks/src/unit.rs +++ b/crates/turbo-tasks/src/unit.rs @@ -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 { - Vc::cell(()) - } -}