Skip to content

Commit

Permalink
refactor(turbo-tasks): Improve Display impl for CachedTaskType, rem…
Browse files Browse the repository at this point in the history
…ove logic from memory backend
  • Loading branch information
bgw committed Sep 4, 2024
1 parent 9056606 commit 5e5f98a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 68 deletions.
34 changes: 1 addition & 33 deletions turbopack/crates/turbo-tasks-memory/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,39 +598,7 @@ impl Task {
match ty {
TaskTypeForDescription::Root => format!("[{}] root", id),
TaskTypeForDescription::Once => format!("[{}] once", id),
TaskTypeForDescription::Persistent(ty) => match &***ty {
CachedTaskType::Native {
fn_type: native_fn,
this: _,
arg: _,
} => {
format!("[{}] {}", id, registry::get_function(*native_fn).name)
}
CachedTaskType::ResolveNative {
fn_type: native_fn,
this: _,
arg: _,
} => {
format!(
"[{}] [resolve] {}",
id,
registry::get_function(*native_fn).name
)
}
CachedTaskType::ResolveTrait {
trait_type,
method_name: fn_name,
this: _,
arg: _,
} => {
format!(
"[{}] [resolve trait] {} in trait {}",
id,
fn_name,
registry::get_trait(*trait_type).name
)
}
},
TaskTypeForDescription::Persistent(ty) => format!("[{id}] {ty}"),
}
}

Expand Down
77 changes: 42 additions & 35 deletions turbopack/crates/turbo-tasks/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,50 @@ pub enum CachedTaskType {
},
}

impl CachedTaskType {
/// Returns the name of the function in the code. Trait methods are
/// formatted as `TraitName::method_name`.
pub fn get_name(&self) -> Cow<'static, str> {
match self {
Self::Native {
fn_type: native_fn,
this: _,
arg: _,
}
| Self::ResolveNative {
fn_type: native_fn,
this: _,
arg: _,
} => Cow::Borrowed(&registry::get_function(*native_fn).name),
Self::ResolveTrait {
trait_type: trait_id,
method_name: fn_name,
this: _,
arg: _,
} => format!("{}::{}", registry::get_trait(*trait_id).name, fn_name).into(),
}
}

pub fn try_get_function_id(&self) -> Option<FunctionId> {
match self {
Self::Native { fn_type, .. } | Self::ResolveNative { fn_type, .. } => Some(*fn_type),
Self::ResolveTrait { .. } => None,
}
}
}

/// Provides a description of the task, including the function name.
impl Display for CachedTaskType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CachedTaskType::Native { .. } => {}
CachedTaskType::ResolveNative { .. } => {
f.write_str("[resolve] ")?;
}
CachedTaskType::ResolveTrait { .. } => {
f.write_str("[resolve trait] ")?;
}
}
f.write_str(&self.get_name())
}
}
Expand Down Expand Up @@ -284,41 +326,6 @@ mod ser {
}
}

impl CachedTaskType {
/// Returns the name of the function in the code. Trait methods are
/// formatted as `TraitName::method_name`.
///
/// Equivalent to [`ToString::to_string`], but potentially more efficient as
/// it can return a `&'static str` in many cases.
pub fn get_name(&self) -> Cow<'static, str> {
match self {
Self::Native {
fn_type: native_fn,
this: _,
arg: _,
}
| Self::ResolveNative {
fn_type: native_fn,
this: _,
arg: _,
} => Cow::Borrowed(&registry::get_function(*native_fn).name),
Self::ResolveTrait {
trait_type: trait_id,
method_name: fn_name,
this: _,
arg: _,
} => format!("{}::{}", registry::get_trait(*trait_id).name, fn_name).into(),
}
}

pub fn try_get_function_id(&self) -> Option<FunctionId> {
match self {
Self::Native { fn_type, .. } | Self::ResolveNative { fn_type, .. } => Some(*fn_type),
Self::ResolveTrait { .. } => None,
}
}
}

pub struct TaskExecutionSpec<'a> {
pub future: Pin<Box<dyn Future<Output = Result<RawVc>> + Send + 'a>>,
pub span: Span,
Expand Down

0 comments on commit 5e5f98a

Please sign in to comment.