Skip to content

Commit

Permalink
Revert "Remove mopa dependency in turbo-tasks" (#7573)
Browse files Browse the repository at this point in the history
Reverts #7206

Closes PACK-2640
  • Loading branch information
kwonoj committed Mar 2, 2024
1 parent fcf6b03 commit 7c1ae37
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 37 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
39 changes: 13 additions & 26 deletions crates/turbo-tasks/src/magic_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Any>` instead
/// of `&dyn Any`.
pub trait MagicAny: mopa::Any + Send + Sync {
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send>;

fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result;
Expand All @@ -43,11 +25,16 @@ pub trait MagicAny: Any + Send + Sync {
fn magic_type_name(&self) -> &'static str;
}

impl<T: Debug + Eq + Ord + Hash + Send + Sync + 'static> 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<T: Debug + Eq + Ord + Hash + Send + Sync + 'static> MagicAny for T {
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send> {
self
}
Expand All @@ -64,7 +51,7 @@ impl<T: Debug + Eq + Ord + Hash + Send + Sync + 'static> MagicAny for T {
}

fn magic_eq(&self, other: &dyn MagicAny) -> bool {
match other.magic_any_ref().downcast_ref::<Self>() {
match other.downcast_ref::<Self>() {
None => false,
Some(other) => self == other,
}
Expand All @@ -75,7 +62,7 @@ impl<T: Debug + Eq + Ord + Hash + Send + Sync + 'static> MagicAny for T {
}

fn magic_cmp(&self, other: &dyn MagicAny) -> Ordering {
match other.magic_any_ref().downcast_ref::<Self>() {
match other.downcast_ref::<Self>() {
None => Ord::cmp(&TypeId::of::<Self>(), &other.type_id()),
Some(other) => self.cmp(other),
}
Expand Down Expand Up @@ -138,7 +125,7 @@ impl dyn MagicAny {
pub fn as_serialize<T: Debug + Eq + Ord + Hash + Serialize + Send + Sync + 'static>(
&self,
) -> &dyn erased_serde::Serialize {
if let Some(r) = self.magic_any_ref().downcast_ref::<T>() {
if let Some(r) = self.downcast_ref::<T>() {
r
} else {
#[cfg(debug_assertions)]
Expand Down
19 changes: 8 additions & 11 deletions crates/turbo-tasks/src/task/task_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,13 @@ where
fn try_from_concrete(input: &ConcreteTaskInput) -> Result<Self> {
match input {
ConcreteTaskInput::SharedValue(value) => {
let v = (*value.1)
.magic_any_ref()
.downcast_ref::<T>()
.ok_or_else(|| {
anyhow!(
"invalid task input type, expected {} got {:?}",
type_name::<T>(),
value.1,
)
})?;
let v = value.1.downcast_ref::<T>().ok_or_else(|| {
anyhow!(
"invalid task input type, expected {} got {:?}",
type_name::<T>(),
value.1,
)
})?;
Ok(Value::new(v.clone()))
}
_ => bail!("invalid task input type, expected {}", type_name::<T>()),
Expand All @@ -278,7 +275,7 @@ where
fn try_from_concrete(input: &ConcreteTaskInput) -> Result<Self> {
match input {
ConcreteTaskInput::TransientSharedValue(value) => {
let v = value.0.magic_any_ref().downcast_ref::<T>().ok_or_else(|| {
let v = value.0.downcast_ref::<T>().ok_or_else(|| {
anyhow!(
"invalid task input type, expected {} got {:?}",
type_name::<T>(),
Expand Down

0 comments on commit 7c1ae37

Please sign in to comment.