Struct Span

pub struct Span { /* private fields */ }

A region of source code, along with macro expansion information.

Implementations

impl Span

fn call_site() -> Self

The span of the invocation of the current procedural macro.

Identifiers created with this span will be resolved as if they were written directly at the macro call location (call-site hygiene) and other code at the macro call site will be able to refer to them as well.

fn mixed_site() -> Self

The span located at the invocation of the procedural macro, but with local variables, labels, and $crate resolved at the definition site of the macro. This is the same hygiene behavior as macro_rules.

fn resolved_at(&self, other: Span) -> Span

Creates a new span with the same line/column information as self but that resolves symbols as though it were at other.

fn located_at(&self, other: Span) -> Span

Creates a new span with the same name resolution behavior as self but with the line/column information of other.

fn unwrap(self) -> Span

Convert proc_macro2::Span to proc_macro::Span.

This method is available when building with a nightly compiler, or when building with rustc 1.29+ without semver exempt features.

Panics

Panics if called from outside of a procedural macro. Unlike proc_macro2::Span, the proc_macro::Span type can only exist within the context of a procedural macro invocation.

fn byte_range(&self) -> Range<usize>

Returns the span's byte position range in the source file.

This method requires the "span-locations" feature to be enabled.

When executing in a procedural macro context, the returned range is only accurate if compiled with a nightly toolchain. The stable toolchain does not have this information available. When executing outside of a procedural macro, such as main.rs or build.rs, the byte range is always accurate regardless of toolchain.

fn start(&self) -> LineColumn

Get the starting line/column in the source file for this span.

This method requires the "span-locations" feature to be enabled.

When executing in a procedural macro context, the returned line/column are only meaningful if compiled with a nightly toolchain. The stable toolchain does not have this information available. When executing outside of a procedural macro, such as main.rs or build.rs, the line/column are always meaningful regardless of toolchain.

fn end(&self) -> LineColumn

Get the ending line/column in the source file for this span.

This method requires the "span-locations" feature to be enabled.

When executing in a procedural macro context, the returned line/column are only meaningful if compiled with a nightly toolchain. The stable toolchain does not have this information available. When executing outside of a procedural macro, such as main.rs or build.rs, the line/column are always meaningful regardless of toolchain.

fn file(&self) -> String

The path to the source file in which this span occurs, for display purposes.

This might not correspond to a valid file system path. It might be remapped, or might be an artificial path such as "<macro expansion>".

fn local_file(&self) -> Option<PathBuf>

The path to the source file in which this span occurs on disk.

This is the actual path on disk. It is unaffected by path remapping.

This path should not be embedded in the output of the macro; prefer file() instead.

fn join(&self, other: Span) -> Option<Span>

Create a new span encompassing self and other.

Returns None if self and other are from different files.

Warning: the underlying proc_macro::Span::join method is nightly-only. When called from within a procedural macro not using a nightly compiler, this method will always return None.

fn source_text(&self) -> Option<String>

Returns the source text behind a span. This preserves the original source code, including spaces and comments. It only returns a result if the span corresponds to real source code.

Note: The observable result of a macro should only rely on the tokens and not on this source text. The result of this function is a best effort to be used for diagnostics only.

Trait Implementations

impl Clone for Span

fn clone(&self) -> Self

impl Copy for Span

impl Debug for Span

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl From<Span> for Span

fn from(proc_span: Span) -> Self

Auto Trait Implementations

impl !Send for Span

impl !Sync for Span

impl Freeze for Span

impl RefUnwindSafe for Span

impl Unpin for Span

impl UnsafeUnpin for Span

impl UnwindSafe for Span

Blanket Implementations

impl<T> Any for Span where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Span where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Span where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Span where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Span

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Span where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Span where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Span where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Span where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>