pub trait WithSource {
// Required method
fn with_source(self, source: Arc<dyn Source>) -> impl Diagnostic;
}
Required Methods§
Sourcefn with_source(self, source: Arc<dyn Source>) -> impl Diagnostic
fn with_source(self, source: Arc<dyn Source>) -> impl Diagnostic
Provides the current diagnostic with source code, so it can still be reported, even though no source is available at the time of diagnostic creation.
§Examples
use std::sync::Arc;
use error_snippet::{Diagnostic, SimpleDiagnostic, Label, NamedSource, Source, WithSource};
// no source attached
let label = Label::new(None, 60..65, "could not find method 'invok'");
let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
.with_label(label.clone());
let source = Arc::new(NamedSource::new(
"src/lib.rs",
r#"fn main() -> int {
let a = new Testing();
let b = a.invok();
return false;
}"#,
));
// attach the source code to the diagnostic
let diag = diag.with_source(source.clone());
assert_eq!(diag.message(), "Whoops, that wasn't supposed to happen!");
assert_eq!(diag.source_code().unwrap().name(), source.name());
assert_eq!(diag.source_code().unwrap().content(), source.content());
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.