Skip to content

Commit

Permalink
Generate impls for core traits rather than std (#271)
Browse files Browse the repository at this point in the history
Part of the work in commit 2430448 seems to have been reversed at some
point. `print_typedef_enum` will still generate an impl of
`std::error::Error` if the name of the enum contains "errno", but this
isn't as simple of a fix because `core` doesn't have an `Error` trait.

Signed-off-by: Klim Tsoutsman <klim@tsoutsman.com>
  • Loading branch information
tsoutsman committed Jul 7, 2022
1 parent 206263e commit ea71cad
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions crates/gen-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,13 @@ pub trait RustGenerator {

self.push_str("impl");
self.print_generics(&info, lt, true);
self.push_str(" std::fmt::Debug for ");
self.push_str(" core::fmt::Debug for ");
self.push_str(&name);
self.print_generics(&info, lt, false);
self.push_str(" {\n");
self.push_str("fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n");
self.push_str(
"fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
);
self.push_str(&format!("f.debug_struct(\"{}\")", name));
for field in record.fields.iter() {
self.push_str(&format!(
Expand Down Expand Up @@ -650,11 +652,11 @@ pub trait RustGenerator {
let lt = self.lifetime_for(&info, mode);
self.push_str("impl");
self.print_generics(&info, lt, true);
self.push_str(" std::fmt::Debug for ");
self.push_str(" core::fmt::Debug for ");
self.push_str(name);
self.print_generics(&info, lt, false);
self.push_str(" {\n");
self.push_str("fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n");
self.push_str("fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n");
self.push_str("match self {\n");
for (case_name, payload) in cases {
self.push_str(name);
Expand Down Expand Up @@ -769,10 +771,10 @@ pub trait RustGenerator {

self.push_str("}\n");

self.push_str("impl std::fmt::Debug for ");
self.push_str("impl core::fmt::Debug for ");
self.push_str(&name);
self.push_str(
"{\nfn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n",
"{\nfn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
);
self.push_str("f.debug_struct(\"");
self.push_str(&name);
Expand All @@ -784,10 +786,10 @@ pub trait RustGenerator {
self.push_str("}\n");
self.push_str("}\n");

self.push_str("impl std::fmt::Display for ");
self.push_str("impl core::fmt::Display for ");
self.push_str(&name);
self.push_str(
"{\nfn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n",
"{\nfn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n",
);
self.push_str("write!(f, \"{} (error {})\", self.name(), *self as i32)");
self.push_str("}\n");
Expand Down

0 comments on commit ea71cad

Please sign in to comment.