rust: Implement Display instead of ToString for enums (#19611)

Rust docs for `ToString` explicitly recommend implementing `Display`:

> This trait is automatically implemented for any type which implements the Display trait. As such, ToString shouldn’t be implemented directly: Display should be implemented instead, and you get the ToString implementation for free.

See: https://doc.rust-lang.org/std/string/trait.ToString.html
See: https://github.com/Nitrokey/nethsm-sdk-rs/pull/33

Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
This commit is contained in:
Wiktor Kwapisiewicz 2024-09-24 15:07:12 +02:00 committed by GitHub
parent 7d8eacc197
commit 5732e2758f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 61 additions and 61 deletions

View File

@ -27,15 +27,15 @@ pub enum {{{classname}}} {
{{/enumVars}}{{/allowableValues}}
}
impl ToString for {{{classname}}} {
fn to_string(&self) -> String {
match self {
impl std::fmt::Display for {{{classname}}} {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
{{#allowableValues}}
{{#enumVars}}
Self::{{{name}}} => String::from("{{{value}}}"),
Self::{{{name}}} => "{{{value}}}",
{{/enumVars}}
{{/allowableValues}}
}
})
}
}
{{/isInteger}}

View File

@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
}
impl ToString for NumericEnumTesting {
fn to_string(&self) -> String {
match self {
Self::Variant0 => String::from("0"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant3 => String::from("3"),
}
impl std::fmt::Display for NumericEnumTesting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::Variant0 => "0",
Self::Variant1 => "1",
Self::Variant2 => "2",
Self::Variant3 => "3",
})
}
}
impl Default for NumericEnumTesting {

View File

@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
}
impl ToString for NumericEnumTesting {
fn to_string(&self) -> String {
match self {
Self::Variant0 => String::from("0"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant3 => String::from("3"),
}
impl std::fmt::Display for NumericEnumTesting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::Variant0 => "0",
Self::Variant1 => "1",
Self::Variant2 => "2",
Self::Variant3 => "3",
})
}
}
impl Default for NumericEnumTesting {

View File

@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
}
impl ToString for NumericEnumTesting {
fn to_string(&self) -> String {
match self {
Self::Variant0 => String::from("0"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant3 => String::from("3"),
}
impl std::fmt::Display for NumericEnumTesting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::Variant0 => "0",
Self::Variant1 => "1",
Self::Variant2 => "2",
Self::Variant3 => "3",
})
}
}
impl Default for NumericEnumTesting {

View File

@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
}
impl ToString for NumericEnumTesting {
fn to_string(&self) -> String {
match self {
Self::Variant0 => String::from("0"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant3 => String::from("3"),
}
impl std::fmt::Display for NumericEnumTesting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::Variant0 => "0",
Self::Variant1 => "1",
Self::Variant2 => "2",
Self::Variant3 => "3",
})
}
}
impl Default for NumericEnumTesting {

View File

@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
}
impl ToString for NumericEnumTesting {
fn to_string(&self) -> String {
match self {
Self::Variant0 => String::from("0"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant3 => String::from("3"),
}
impl std::fmt::Display for NumericEnumTesting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::Variant0 => "0",
Self::Variant1 => "1",
Self::Variant2 => "2",
Self::Variant3 => "3",
})
}
}
impl Default for NumericEnumTesting {

View File

@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
}
impl ToString for NumericEnumTesting {
fn to_string(&self) -> String {
match self {
Self::Variant0 => String::from("0"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant3 => String::from("3"),
}
impl std::fmt::Display for NumericEnumTesting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::Variant0 => "0",
Self::Variant1 => "1",
Self::Variant2 => "2",
Self::Variant3 => "3",
})
}
}
impl Default for NumericEnumTesting {

View File

@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
}
impl ToString for NumericEnumTesting {
fn to_string(&self) -> String {
match self {
Self::Variant0 => String::from("0"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant3 => String::from("3"),
}
impl std::fmt::Display for NumericEnumTesting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
Self::Variant0 => "0",
Self::Variant1 => "1",
Self::Variant2 => "2",
Self::Variant3 => "3",
})
}
}
impl Default for NumericEnumTesting {