Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor naga span updates #7029

Merged
merged 5 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions naga/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ impl<E> WithSpan<E> {

/// Convenience trait for [`Error`] to be able to apply spans to anything.
pub(crate) trait AddSpan: Sized {
/// The returned output type.
type Output;

/// See [`WithSpan::new`].
fn with_span(self) -> Self::Output;
/// See [`WithSpan::with_span`].
Expand All @@ -325,6 +327,30 @@ pub(crate) trait AddSpan: Sized {
fn with_span_handle<T, A: SpanProvider<T>>(self, handle: Handle<T>, arena: &A) -> Self::Output;
}

impl<E> AddSpan for E {
type Output = WithSpan<Self>;

fn with_span(self) -> WithSpan<Self> {
WithSpan::new(self)
}

fn with_span_static(self, span: Span, description: &'static str) -> WithSpan<Self> {
WithSpan::new(self).with_span(span, description)
}

fn with_span_context(self, span_context: SpanContext) -> WithSpan<Self> {
WithSpan::new(self).with_context(span_context)
}

fn with_span_handle<T, A: SpanProvider<T>>(
self,
handle: Handle<T>,
arena: &A,
) -> WithSpan<Self> {
WithSpan::new(self).with_handle(handle, arena)
}
}

/// Trait abstracting over getting a span from an [`Arena`] or a [`UniqueArena`].
pub(crate) trait SpanProvider<T> {
fn get_span(&self, handle: Handle<T>) -> Span;
Expand All @@ -351,36 +377,12 @@ impl<T> SpanProvider<T> for UniqueArena<T> {
}
}

impl<E> AddSpan for E
where
E: Error,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this was just flat out not needed?

{
type Output = WithSpan<Self>;
fn with_span(self) -> WithSpan<Self> {
WithSpan::new(self)
}

fn with_span_static(self, span: Span, description: &'static str) -> WithSpan<Self> {
WithSpan::new(self).with_span(span, description)
}

fn with_span_context(self, span_context: SpanContext) -> WithSpan<Self> {
WithSpan::new(self).with_context(span_context)
}

fn with_span_handle<T, A: SpanProvider<T>>(
self,
handle: Handle<T>,
arena: &A,
) -> WithSpan<Self> {
WithSpan::new(self).with_handle(handle, arena)
}
}

/// Convenience trait for [`Result`], adding a [`MapErrWithSpan::map_err_inner`]
/// mapping to [`WithSpan::and_then`].
pub trait MapErrWithSpan<E, E2>: Sized {
pub(crate) trait MapErrWithSpan<E, E2>: Sized {
/// The returned output type.
type Output: Sized;

fn map_err_inner<F, E3>(self, func: F) -> Self::Output
where
F: FnOnce(E) -> WithSpan<E3>,
Expand All @@ -389,6 +391,7 @@ pub trait MapErrWithSpan<E, E2>: Sized {

impl<T, E, E2> MapErrWithSpan<E, E2> for Result<T, WithSpan<E>> {
type Output = Result<T, WithSpan<E2>>;

fn map_err_inner<F, E3>(self, func: F) -> Result<T, WithSpan<E2>>
where
F: FnOnce(E) -> WithSpan<E3>,
Expand Down