Skip to content

Commit 6b6bd30

Browse files
committed
chore: Remove mopa dependency in turbo-tasks (2nd attempt)
1 parent 765b682 commit 6b6bd30

File tree

5 files changed

+31
-81
lines changed

5 files changed

+31
-81
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbo-tasks/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ erased-serde = { workspace = true }
3131
event-listener = "5.4.0"
3232
futures = { workspace = true }
3333
indexmap = { workspace = true, features = ["serde"] }
34-
mopa = "0.2.0"
3534
once_cell = { workspace = true }
3635
parking_lot = { workspace = true, features = ["serde"]}
3736
pin-project-lite = { workspace = true }

turbopack/crates/turbo-tasks/src/magic_any.rs

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,43 @@
1-
use core::fmt;
2-
use std::{
3-
any::{Any, TypeId},
4-
fmt::Debug,
5-
hash::Hash,
6-
sync::Arc,
7-
};
1+
use std::{any::Any, fmt::Debug, hash::Hash};
82

93
use serde::{Deserialize, Serialize, de::DeserializeSeed};
104
use turbo_dyn_eq_hash::{
11-
DynEq, DynHash, DynPartialEq, impl_eq_for_dyn, impl_hash_for_dyn, impl_partial_eq_for_dyn,
5+
DynEq, DynHash, impl_eq_for_dyn, impl_hash_for_dyn, impl_partial_eq_for_dyn,
126
};
137

14-
use crate::trace::{TraceRawVcs, TraceRawVcsContext};
15-
16-
pub trait MagicAny: mopa::Any + DynPartialEq + DynEq + DynHash + Send + Sync {
17-
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send>;
18-
19-
fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result;
20-
21-
fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext);
8+
use crate::trace::TraceRawVcs;
229

10+
pub trait MagicAny: Debug + DynEq + DynHash + TraceRawVcs + Send + Sync + 'static {
2311
#[cfg(debug_assertions)]
2412
fn magic_type_name(&self) -> &'static str;
2513
}
2614

27-
#[allow(clippy::transmute_ptr_to_ref)] // can't fix as it's in the macro
28-
mod clippy {
29-
use mopa::mopafy;
30-
31-
use super::MagicAny;
32-
33-
mopafy!(MagicAny);
34-
}
35-
36-
impl<T: Debug + Eq + Hash + Send + Sync + TraceRawVcs + 'static> MagicAny for T {
37-
fn magic_any_arc(self: Arc<Self>) -> Arc<dyn Any + Sync + Send> {
38-
self
39-
}
40-
41-
fn magic_debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
42-
let mut d = f.debug_tuple("MagicAny");
43-
d.field(&TypeId::of::<Self>());
44-
45-
#[cfg(debug_assertions)]
46-
d.field(&std::any::type_name::<Self>());
47-
48-
d.field(&(self as &Self));
49-
d.finish()
50-
}
51-
52-
fn magic_trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
53-
self.trace_raw_vcs(trace_context);
54-
}
55-
15+
impl<T> MagicAny for T
16+
where
17+
T: Debug + Eq + Hash + Send + Sync + TraceRawVcs + 'static,
18+
{
5619
#[cfg(debug_assertions)]
5720
fn magic_type_name(&self) -> &'static str {
5821
std::any::type_name::<T>()
5922
}
6023
}
6124

62-
impl fmt::Debug for dyn MagicAny {
63-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64-
self.magic_debug(f)
65-
}
66-
}
67-
6825
impl_partial_eq_for_dyn!(dyn MagicAny);
6926
impl_eq_for_dyn!(dyn MagicAny);
7027
impl_hash_for_dyn!(dyn MagicAny);
7128

72-
impl TraceRawVcs for dyn MagicAny {
73-
fn trace_raw_vcs(&self, trace_context: &mut TraceRawVcsContext) {
74-
self.magic_trace_raw_vcs(trace_context)
75-
}
76-
}
77-
7829
impl dyn MagicAny {
7930
pub fn as_serialize<T: Debug + Eq + Hash + Serialize + Send + Sync + TraceRawVcs + 'static>(
8031
&self,
8132
) -> &dyn erased_serde::Serialize {
82-
if let Some(r) = self.downcast_ref::<T>() {
33+
if let Some(r) = (self as &dyn Any).downcast_ref::<T>() {
8334
r
8435
} else {
8536
#[cfg(debug_assertions)]
8637
panic!(
8738
"MagicAny::as_serializable broken: got {} but expected {}",
8839
self.magic_type_name(),
89-
std::any::type_name::<T>()
40+
std::any::type_name::<T>(),
9041
);
9142
#[cfg(not(debug_assertions))]
9243
panic!("MagicAny::as_serializable bug");

turbopack/crates/turbo-tasks/src/native_function.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt::Debug, hash::Hash, pin::Pin};
1+
use std::{any::Any, fmt::Debug, hash::Hash, pin::Pin};
22

33
use anyhow::Result;
44
use futures::Future;
@@ -105,32 +105,36 @@ fn resolve_functor_impl<T: MagicAny + TaskInput>(value: &dyn MagicAny) -> Resolv
105105

106106
#[cfg(debug_assertions)]
107107
#[inline(never)]
108-
pub fn debug_downcast_args_error_msg(expected: &str, actual: &dyn MagicAny) -> String {
109-
format!(
110-
"Invalid argument type, expected {expected} got {}",
111-
(*actual).magic_type_name()
112-
)
108+
pub fn debug_downcast_args_error_msg(expected: &str, actual: &str) -> String {
109+
format!("Invalid argument type, expected {expected} got {actual}")
113110
}
114111

115112
pub fn downcast_args_owned<T: MagicAny>(args: Box<dyn MagicAny>) -> Box<T> {
116-
#[allow(unused_variables)]
117-
args.downcast::<T>()
118-
.map_err(|args| {
113+
#[cfg(debug_assertions)]
114+
let args_type_name = args.magic_type_name();
115+
116+
(args as Box<dyn Any>)
117+
.downcast::<T>()
118+
.map_err(|_args| {
119119
#[cfg(debug_assertions)]
120-
return debug_downcast_args_error_msg(std::any::type_name::<T>(), &*args);
120+
return anyhow::anyhow!(debug_downcast_args_error_msg(
121+
std::any::type_name::<T>(),
122+
args_type_name,
123+
));
121124
#[cfg(not(debug_assertions))]
122125
return anyhow::anyhow!("Invalid argument type");
123126
})
124127
.unwrap()
125128
}
126129

127130
pub fn downcast_args_ref<T: MagicAny>(args: &dyn MagicAny) -> &T {
128-
args.downcast_ref::<T>()
131+
(args as &dyn Any)
132+
.downcast_ref::<T>()
129133
.ok_or_else(|| {
130134
#[cfg(debug_assertions)]
131135
return anyhow::anyhow!(debug_downcast_args_error_msg(
132136
std::any::type_name::<T>(),
133-
args
137+
args.magic_type_name(),
134138
));
135139
#[cfg(not(debug_assertions))]
136140
return anyhow::anyhow!("Invalid argument type");

turbopack/crates/turbo-tasks/src/task/function.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,13 @@ macro_rules! task_inputs_impl {
172172
/// gives the compiler more chances to dedupe monomorphized code across small functions with less
173173
/// typevars.
174174
fn get_args<T: MagicAny + Clone>(arg: &dyn MagicAny) -> Result<T> {
175-
let value = arg.downcast_ref::<T>().cloned();
175+
let value = (arg as &dyn std::any::Any).downcast_ref::<T>().cloned();
176176
#[cfg(debug_assertions)]
177177
return anyhow::Context::with_context(value, || {
178-
crate::native_function::debug_downcast_args_error_msg(std::any::type_name::<T>(), arg)
178+
crate::native_function::debug_downcast_args_error_msg(
179+
std::any::type_name::<T>(),
180+
arg.magic_type_name(),
181+
)
179182
});
180183
#[cfg(not(debug_assertions))]
181184
return anyhow::Context::context(value, "Invalid argument type");

0 commit comments

Comments
 (0)