mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
refactor: zero-copy deserialization when possible
Found this during PR review. We could leverage `#[serde(borrow)]` for zero-copy deserialization for messages from the compiler. We can't use `&str` fields because they may contain escape sequences in the future, which fails the deserialization. See https://github.com/serde-rs/json/issues/742
This commit is contained in:
parent
b396f2c3aa
commit
9a6a9451ba
@ -103,7 +103,7 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// See [`Message::Diagnostic`] and [`Message::WarningCount`].
|
/// See [`Message::Diagnostic`] and [`Message::WarningCount`].
|
||||||
pub fn emit_diag(&self, level: String, diag: String, fixable: bool) -> CargoResult<()> {
|
pub fn emit_diag(&self, level: &str, diag: String, fixable: bool) -> CargoResult<()> {
|
||||||
if let Some(dedupe) = self.output {
|
if let Some(dedupe) = self.output {
|
||||||
let emitted = dedupe.emit_diag(&diag)?;
|
let emitted = dedupe.emit_diag(&diag)?;
|
||||||
if level == "warning" {
|
if level == "warning" {
|
||||||
@ -116,7 +116,7 @@ impl<'a, 'gctx> JobState<'a, 'gctx> {
|
|||||||
} else {
|
} else {
|
||||||
self.messages.push_bounded(Message::Diagnostic {
|
self.messages.push_bounded(Message::Diagnostic {
|
||||||
id: self.id,
|
id: self.id,
|
||||||
level,
|
level: level.to_string(),
|
||||||
diag,
|
diag,
|
||||||
fixable,
|
fixable,
|
||||||
});
|
});
|
||||||
|
@ -54,6 +54,7 @@ mod unit;
|
|||||||
pub mod unit_dependencies;
|
pub mod unit_dependencies;
|
||||||
pub mod unit_graph;
|
pub mod unit_graph;
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
@ -1756,10 +1757,15 @@ fn on_stderr_line_inner(
|
|||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct CompilerMessage {
|
struct CompilerMessage<'a> {
|
||||||
|
// `rendered` contains escape sequences, which can't be
|
||||||
|
// zero-copy deserialized by serde_json.
|
||||||
|
// See https://github.com/serde-rs/json/issues/742
|
||||||
rendered: String,
|
rendered: String,
|
||||||
message: String,
|
#[serde(borrow)]
|
||||||
level: String,
|
message: Cow<'a, str>,
|
||||||
|
#[serde(borrow)]
|
||||||
|
level: Cow<'a, str>,
|
||||||
children: Vec<PartialDiagnostic>,
|
children: Vec<PartialDiagnostic>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1782,7 +1788,8 @@ fn on_stderr_line_inner(
|
|||||||
suggestion_applicability: Option<Applicability>,
|
suggestion_applicability: Option<Applicability>,
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(mut msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
|
if let Ok(mut msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
|
||||||
|
{
|
||||||
if msg.message.starts_with("aborting due to")
|
if msg.message.starts_with("aborting due to")
|
||||||
|| msg.message.ends_with("warning emitted")
|
|| msg.message.ends_with("warning emitted")
|
||||||
|| msg.message.ends_with("warnings emitted")
|
|| msg.message.ends_with("warnings emitted")
|
||||||
@ -1808,7 +1815,7 @@ fn on_stderr_line_inner(
|
|||||||
})
|
})
|
||||||
.any(|b| b);
|
.any(|b| b);
|
||||||
count_diagnostic(&msg.level, options);
|
count_diagnostic(&msg.level, options);
|
||||||
state.emit_diag(msg.level, rendered, machine_applicable)?;
|
state.emit_diag(&msg.level, rendered, machine_applicable)?;
|
||||||
}
|
}
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
@ -1819,12 +1826,14 @@ fn on_stderr_line_inner(
|
|||||||
// cached replay to enable/disable colors without re-invoking rustc.
|
// cached replay to enable/disable colors without re-invoking rustc.
|
||||||
MessageFormat::Json { ansi: false, .. } => {
|
MessageFormat::Json { ansi: false, .. } => {
|
||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
struct CompilerMessage {
|
struct CompilerMessage<'a> {
|
||||||
rendered: String,
|
rendered: String,
|
||||||
#[serde(flatten)]
|
#[serde(flatten, borrow)]
|
||||||
other: std::collections::BTreeMap<String, serde_json::Value>,
|
other: std::collections::BTreeMap<Cow<'a, str>, serde_json::Value>,
|
||||||
}
|
}
|
||||||
if let Ok(mut error) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
|
if let Ok(mut error) =
|
||||||
|
serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get())
|
||||||
|
{
|
||||||
error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
|
error.rendered = anstream::adapter::strip_str(&error.rendered).to_string();
|
||||||
let new_line = serde_json::to_string(&error)?;
|
let new_line = serde_json::to_string(&error)?;
|
||||||
let new_msg: Box<serde_json::value::RawValue> = serde_json::from_str(&new_line)?;
|
let new_msg: Box<serde_json::value::RawValue> = serde_json::from_str(&new_line)?;
|
||||||
@ -1866,12 +1875,14 @@ fn on_stderr_line_inner(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct CompilerMessage {
|
struct CompilerMessage<'a> {
|
||||||
message: String,
|
#[serde(borrow)]
|
||||||
level: String,
|
message: Cow<'a, str>,
|
||||||
|
#[serde(borrow)]
|
||||||
|
level: Cow<'a, str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(msg) = serde_json::from_str::<CompilerMessage>(compiler_message.get()) {
|
if let Ok(msg) = serde_json::from_str::<CompilerMessage<'_>>(compiler_message.get()) {
|
||||||
if msg.message.starts_with("aborting due to")
|
if msg.message.starts_with("aborting due to")
|
||||||
|| msg.message.ends_with("warning emitted")
|
|| msg.message.ends_with("warning emitted")
|
||||||
|| msg.message.ends_with("warnings emitted")
|
|| msg.message.ends_with("warnings emitted")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user