From 2f94004f5baede604505da353e347bf807696fbf Mon Sep 17 00:00:00 2001 From: Alex Kirszenberg Date: Thu, 24 Aug 2023 11:11:06 +0200 Subject: [PATCH] Automatically derive ValueDefault for primitive value types --- crates/turbo-tasks-macros/src/primitive_macro.rs | 12 ++++++++++++ crates/turbo-tasks/src/primitives.rs | 9 +++------ crates/turbo-tasks/src/unit.rs | 11 ++--------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/crates/turbo-tasks-macros/src/primitive_macro.rs b/crates/turbo-tasks-macros/src/primitive_macro.rs index e6a2590805162e..2d0b99e857ae64 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 ccb89fc209fe86..b14773ecb8eddf 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 d3af9af7b84a92..15e6a2f4c0ebef 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(()) - } -}