Restrict From<S>
for {D,Subd}iagnosticMessage
.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static, str>`, which are reasonable. It also includes `&String`, which is pretty weird, and results in many places making unnecessary allocations for patterns like this: ``` self.fatal(&format!(...)) ``` This creates a string with `format!`, takes a reference, passes the reference to `fatal`, which does an `into()`, which clones the reference, doing a second allocation. Two allocations for a single string, bleh. This commit changes the `From` impls so that you can only create a `{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static, str>`. This requires changing all the places that currently create one from a `&String`. Most of these are of the `&format!(...)` form described above; each one removes an unnecessary static `&`, plus an allocation when executed. There are also a few places where the existing use of `&String` was more reasonable; these now just use `clone()` at the call site. As well as making the code nicer and more efficient, this is a step towards possibly using `Cow<'static, str>` in `{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing the `From<&'a str>` impls to `From<&'static str>`, which is doable, but I'm not yet sure if it's worthwhile.
This commit is contained in:
parent
a368898de7
commit
6b62f37402
177 changed files with 791 additions and 787 deletions
|
@ -287,11 +287,19 @@ pub enum SubdiagnosticMessage {
|
|||
FluentAttr(FluentId),
|
||||
}
|
||||
|
||||
/// `From` impl that enables existing diagnostic calls to functions which now take
|
||||
/// `impl Into<SubdiagnosticMessage>` to continue to work as before.
|
||||
impl<S: Into<String>> From<S> for SubdiagnosticMessage {
|
||||
fn from(s: S) -> Self {
|
||||
SubdiagnosticMessage::Str(s.into())
|
||||
impl From<String> for SubdiagnosticMessage {
|
||||
fn from(s: String) -> Self {
|
||||
SubdiagnosticMessage::Str(s)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a str> for SubdiagnosticMessage {
|
||||
fn from(s: &'a str) -> Self {
|
||||
SubdiagnosticMessage::Str(s.to_string())
|
||||
}
|
||||
}
|
||||
impl From<Cow<'static, str>> for SubdiagnosticMessage {
|
||||
fn from(s: Cow<'static, str>) -> Self {
|
||||
SubdiagnosticMessage::Str(s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -352,11 +360,19 @@ impl DiagnosticMessage {
|
|||
}
|
||||
}
|
||||
|
||||
/// `From` impl that enables existing diagnostic calls to functions which now take
|
||||
/// `impl Into<DiagnosticMessage>` to continue to work as before.
|
||||
impl<S: Into<String>> From<S> for DiagnosticMessage {
|
||||
fn from(s: S) -> Self {
|
||||
DiagnosticMessage::Str(s.into())
|
||||
impl From<String> for DiagnosticMessage {
|
||||
fn from(s: String) -> Self {
|
||||
DiagnosticMessage::Str(s)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a str> for DiagnosticMessage {
|
||||
fn from(s: &'a str) -> Self {
|
||||
DiagnosticMessage::Str(s.to_string())
|
||||
}
|
||||
}
|
||||
impl From<Cow<'static, str>> for DiagnosticMessage {
|
||||
fn from(s: Cow<'static, str>) -> Self {
|
||||
DiagnosticMessage::Str(s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue