/// Creates a new `Span` from a range of byte indices /// /// Note: end is exclusive, it doesn't belong to the `Span` pubconstfn new(start: u32, end: u32) -> Self {
Span { start, end }
}
/// Returns a new `Span` starting at `self` and ending at `other` pubconstfn until(&self, other: &Self) -> Self {
Span {
start: self.start,
end: other.end,
}
}
/// Modifies `self` to contain the smallest `Span` possible that /// contains both `self` and `other` pubfn subsume(&mutself, other: Self) {
*self = if !self.is_defined() { // self isn't defined so use other
other
} elseif !other.is_defined() { // other isn't defined so don't try to subsume
*self
} else { // Both self and other are defined so calculate the span that contains them both
Span {
start: self.start.min(other.start),
end: self.end.max(other.end),
}
}
}
/// Returns the smallest `Span` possible that contains all the `Span`s /// defined in the `from` iterator pubfn total_span<T: Iterator<Item = Self>>(from: T) -> Self { letmut span: Self = Default::default(); for other in from {
span.subsume(other);
}
span
}
/// Converts `self` to a range if the span is not unknown pubfn to_range(self) -> Option<Range<usize>> { ifself.is_defined() {
Some(self.start as usize..self.end as usize)
} else {
None
}
}
/// Check whether `self` was defined or is a default/unknown span pubfn is_defined(&self) -> bool {
*self != Self::default()
}
/// Return a [`SourceLocation`] for this span in the provided source. pubfn location(&self, source: &str) -> SourceLocation { let prefix = &source[..self.start as usize]; let line_number = prefix.matches('\n').count() as u32 + 1; let line_start = prefix.rfind('\n').map(|pos| pos + 1).unwrap_or(0) as u32; let line_position = self.start - line_start + 1;
impl From<Range<usize>> for Span { fn from(range: Range<usize>) -> Self {
Span {
start: range.start as u32,
end: range.end as u32,
}
}
}
impl std::ops::Index<Span> for str { type Output = str;
#[inline] fn index(&self, span: Span) -> &str {
&self[span.start as usize..span.end as usize]
}
}
/// A human-readable representation for a span, tailored for text source. /// /// Roughly corresponds to the positional members of [`GPUCompilationMessage`][gcm] from /// the WebGPU specification, except /// - `offset` and `length` are in bytes (UTF-8 code units), instead of UTF-16 code units. /// - `line_position` is in bytes (UTF-8 code units), instead of UTF-16 code units. /// /// [gcm]: https://www.w3.org/TR/webgpu/#gpucompilationmessage #[derive(Copy, Clone, Debug, PartialEq, Eq)] pubstruct SourceLocation { /// 1-based line number. pub line_number: u32, /// 1-based column in code units (in bytes) of the start of the span. pub line_position: u32, /// 0-based Offset in code units (in bytes) of the start of the span. pub offset: u32, /// Length in code units (in bytes) of the span. pub length: u32,
}
/// A source code span together with "context", a user-readable description of what part of the error it refers to. pubtype SpanContext = (Span, String);
/// Wrapper class for [`Error`], augmenting it with a list of [`SpanContext`]s. #[derive(Debug, Clone)] pubstruct WithSpan<E> {
inner: E,
spans: Vec<SpanContext>,
}
impl<E> fmt::Display for WithSpan<E> where
E: fmt::Display,
{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.inner.fmt(f)
}
}
#[cfg(test)] impl<E> PartialEq for WithSpan<E> where
E: PartialEq,
{ fn eq(&self, other: &Self) -> bool { self.inner.eq(&other.inner)
}
}
impl<E> Error for WithSpan<E> where
E: Error,
{ fn source(&self) -> Option<&(dyn Error + 'static)> { self.inner.source()
}
}
impl<E> WithSpan<E> { /// Create a new [`WithSpan`] from an [`Error`], containing no spans. pubconstfn new(inner: E) -> Self { Self {
inner,
spans: Vec::new(),
}
}
/// Reverse of [`Self::new`], discards span information and returns an inner error. #[allow(clippy::missing_const_for_fn)] // ignore due to requirement of #![feature(const_precise_live_drops)] pubfn into_inner(self) -> E { self.inner
}
/// Add a new span with description. pubfn with_span<S>(mutself, span: Span, description: S) -> Self where
S: ToString,
{ if span.is_defined() { self.spans.push((span, description.to_string()));
} self
}
/// Add a [`SpanContext`]. pubfn with_context(self, span_context: SpanContext) -> Self { let (span, description) = span_context; self.with_span(span, description)
}
/// Add a [`Handle`] from either [`Arena`] or [`UniqueArena`], borrowing its span information from there /// and annotating with a type and the handle representation. pub(crate) fn with_handle<T, A: SpanProvider<T>>(self, handle: Handle<T>, arena: &A) -> Self { self.with_context(arena.get_span_context(handle))
}
/// Convert inner error into another type. Joins span information contained in `self` /// with what is returned from `func`. pubfn and_then<F, E2>(self, func: F) -> WithSpan<E2> where
F: FnOnce(E) -> WithSpan<E2>,
{ letmut res = func(self.inner);
res.spans.extend(self.spans);
res
}
/// Return a [`SourceLocation`] for our first span, if we have one. pubfn location(&self, source: &str) -> Option<SourceLocation> { ifself.spans.is_empty() { return None;
}
/// Emits a summary of the error to standard error stream. pubfn emit_to_stderr(&self, source: &str) where
E: Error,
{ self.emit_to_stderr_with_path(source, "wgsl")
}
/// Emits a summary of the error to standard error stream. pubfn emit_to_stderr_with_path(&self, source: &str, path: &str) where
E: Error,
{ use codespan_reporting::{files, term}; use term::termcolor::{ColorChoice, StandardStream};
let files = files::SimpleFile::new(path, source); let config = term::Config::default(); let writer = StandardStream::stderr(ColorChoice::Auto);
term::emit(&mut writer.lock(), &config, &files, &self.diagnostic())
.expect("cannot write error");
}
/// Emits a summary of the error to a string. pubfn emit_to_string(&self, source: &str) -> String where
E: Error,
{ self.emit_to_string_with_path(source, "wgsl")
}
/// Emits a summary of the error to a string. pubfn emit_to_string_with_path(&self, source: &str, path: &str) -> String where
E: Error,
{ use codespan_reporting::{files, term}; use term::termcolor::NoColor;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.