From 7c1ae37705b2631bc126bb93e30da9d9b867040a Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Fri, 1 Mar 2024 22:46:43 -0800 Subject: [PATCH] Revert "Remove mopa dependency in turbo-tasks" (#7573) Reverts vercel/turbo#7206 Closes PACK-2640 --- Cargo.lock | 7 ++++ crates/turbo-tasks/Cargo.toml | 1 + crates/turbo-tasks/src/magic_any.rs | 39 ++++++++--------------- crates/turbo-tasks/src/task/task_input.rs | 19 +++++------ 4 files changed, 29 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea034f02c7206..cc666c851e30e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5013,6 +5013,12 @@ dependencies = [ "swc_ecma_visit", ] +[[package]] +name = "mopa" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a785740271256c230f57462d3b83e52f998433a7062fc18f96d5999474a9f915" + [[package]] name = "more-asserts" version = "0.2.2" @@ -10595,6 +10601,7 @@ dependencies = [ "event-listener", "futures 0.3.28", "indexmap 1.9.3", + "mopa", "nohash-hasher", "once_cell", "parking_lot 0.12.1", diff --git a/crates/turbo-tasks/Cargo.toml b/crates/turbo-tasks/Cargo.toml index 371fb4bc545b2..6ceccb0af21da 100644 --- a/crates/turbo-tasks/Cargo.toml +++ b/crates/turbo-tasks/Cargo.toml @@ -28,6 +28,7 @@ erased-serde = "0.3.20" event-listener = "2.5.3" futures = { workspace = true } indexmap = { workspace = true, features = ["serde"] } +mopa = "0.2.0" nohash-hasher = "0.2.0" once_cell = { workspace = true } parking_lot = { workspace = true } diff --git a/crates/turbo-tasks/src/magic_any.rs b/crates/turbo-tasks/src/magic_any.rs index a7be3cfed775f..cdc697bfe1969 100644 --- a/crates/turbo-tasks/src/magic_any.rs +++ b/crates/turbo-tasks/src/magic_any.rs @@ -10,25 +10,7 @@ use std::{ use serde::{de::DeserializeSeed, Deserialize, Serialize}; -/// An extension of [`Any`], such that `dyn MagicAny` implements [`fmt::Debug`], -/// [`PartialEq`], [`Eq`], [`PartialOrd`], [`Ord`], and [`Hash`], assuming the -/// concrete type also implements those traits. -/// -/// This allows `dyn MagicAny` to be used in contexts where `dyn Any` could not, -/// such as a key in a map. -pub trait MagicAny: Any + Send + Sync { - /// Upcasts to `&dyn Any`, which can later be used with - /// [`Any::downcast_ref`][Any#method.downcast_ref] to convert back to a - /// concrete type. - /// - /// This is a workaround for [Rust's lack of upcasting - /// support][trait_upcasting]. - /// - /// [trait_upcasting]: https://rust-lang.github.io/rfcs/3324-dyn-upcasting.html - fn magic_any_ref(&self) -> &(dyn Any + Sync + Send); - - /// Same as [`MagicAny::magic_any_arc`], except for `Arc` instead - /// of `&dyn Any`. +pub trait MagicAny: mopa::Any + Send + Sync { fn magic_any_arc(self: Arc) -> Arc; fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result; @@ -43,11 +25,16 @@ pub trait MagicAny: Any + Send + Sync { fn magic_type_name(&self) -> &'static str; } -impl MagicAny for T { - fn magic_any_ref(&self) -> &(dyn Any + Sync + Send) { - self - } +#[allow(clippy::transmute_ptr_to_ref)] // can't fix as it's in the macro +mod clippy { + use mopa::mopafy; + + use super::MagicAny; + mopafy!(MagicAny); +} + +impl MagicAny for T { fn magic_any_arc(self: Arc) -> Arc { self } @@ -64,7 +51,7 @@ impl MagicAny for T { } fn magic_eq(&self, other: &dyn MagicAny) -> bool { - match other.magic_any_ref().downcast_ref::() { + match other.downcast_ref::() { None => false, Some(other) => self == other, } @@ -75,7 +62,7 @@ impl MagicAny for T { } fn magic_cmp(&self, other: &dyn MagicAny) -> Ordering { - match other.magic_any_ref().downcast_ref::() { + match other.downcast_ref::() { None => Ord::cmp(&TypeId::of::(), &other.type_id()), Some(other) => self.cmp(other), } @@ -138,7 +125,7 @@ impl dyn MagicAny { pub fn as_serialize( &self, ) -> &dyn erased_serde::Serialize { - if let Some(r) = self.magic_any_ref().downcast_ref::() { + if let Some(r) = self.downcast_ref::() { r } else { #[cfg(debug_assertions)] diff --git a/crates/turbo-tasks/src/task/task_input.rs b/crates/turbo-tasks/src/task/task_input.rs index 9ca58f7673b86..24eacfa4d228c 100644 --- a/crates/turbo-tasks/src/task/task_input.rs +++ b/crates/turbo-tasks/src/task/task_input.rs @@ -246,16 +246,13 @@ where fn try_from_concrete(input: &ConcreteTaskInput) -> Result { match input { ConcreteTaskInput::SharedValue(value) => { - let v = (*value.1) - .magic_any_ref() - .downcast_ref::() - .ok_or_else(|| { - anyhow!( - "invalid task input type, expected {} got {:?}", - type_name::(), - value.1, - ) - })?; + let v = value.1.downcast_ref::().ok_or_else(|| { + anyhow!( + "invalid task input type, expected {} got {:?}", + type_name::(), + value.1, + ) + })?; Ok(Value::new(v.clone())) } _ => bail!("invalid task input type, expected {}", type_name::()), @@ -278,7 +275,7 @@ where fn try_from_concrete(input: &ConcreteTaskInput) -> Result { match input { ConcreteTaskInput::TransientSharedValue(value) => { - let v = value.0.magic_any_ref().downcast_ref::().ok_or_else(|| { + let v = value.0.downcast_ref::().ok_or_else(|| { anyhow!( "invalid task input type, expected {} got {:?}", type_name::(),