Skip to content

Generate Debug for sys types - #488

Merged
GuillaumeGomez merged 2 commits into
gtk-rs:masterfrom
GuillaumeGomez:debug-impl
Nov 11, 2017
Merged

Generate Debug for sys types#488
GuillaumeGomez merged 2 commits into
gtk-rs:masterfrom
GuillaumeGomez:debug-impl

Conversation

@GuillaumeGomez

Copy link
Copy Markdown
Member

Fixes #486.

cc @EPashkin @sdroege

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

All green. :)

Comment thread src/codegen/sys/lib_.rs
try!(writeln!(
w,
"{comment}#[repr(C)]\n{comment}pub struct {name} {{",
"{comment}#[repr(C)]\n{debug}{comment}pub struct {name} {{",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems generated struct with extra line:

#[repr(C)]

pub struct AtkAttribute {
    pub name: *mut c_char,
    pub value: *mut c_char,
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wo. Where does this backline comes from? Oo

Comment thread src/codegen/sys/lib_.rs Outdated
{fields_name})\n\
\t}}\n\
}}\n",
name = klass.c_type,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but this unreadable, and seems repeated at minimum twice.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I'll write a function and maybe comment or create temporary variables in order to make this less horrible.

Comment thread src/codegen/sys/lib_.rs Outdated
try!(writeln!(
w,
"{}#[repr(C)]\n{0}pub struct {} {{",
"{}#[repr(C)]\n{}\n{0}pub struct {} {{",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EPashkin: It comes from here actually.

Comment thread src/codegen/sys/lib_.rs Outdated
\t\twrite!(f, \"{name} @ {{:?}}\", self as *const _)\n\
\t}}\n\
}}\n",
name=record.c_type));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this better extract as function too and maybe use raw string for format

@EPashkin

Copy link
Copy Markdown
Member

This intended that inner fields not comma-separated?

impl ::std::fmt::Debug for AtkObject {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "AtkObject @ {:?} {{ {:?} {:?} {:?} {:?} {:?} {:?} {:?} }}",
               self as *const _,
               self.parent,
               self.description,
               self.name,
               self.accessible_parent,
               self.role,
               self.relation_set,
               self.layer)
    }
}

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Hum yes. Do you want comma?

@EPashkin

Copy link
Copy Markdown
Member

Then its normal, just strange.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

I'll add the comma tomorrow then. ;)

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Updated.

@sdroege

sdroege commented Nov 11, 2017

Copy link
Copy Markdown
Member

For printing things struct-like, please use the functions from Formatter like debug_struct()

Comment thread src/codegen/sys/lib_.rs Outdated
w,
name,
&format!(
"write!(f, \"{name} @ {{:?}} {{{{ {fields}}}}}\",\n\

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO extra space needed {{{{ {fields} }}}}

Comment thread src/codegen/sys/lib_.rs Outdated
impl_content)
}

fn generate_fields_with_debug(w: &mut Write, name: &str, lines: &[String]) -> Result<()> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not generate_debug_with_fields?

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Updated and improved. Thanks for the suggestion @sdroege!

Comment thread src/codegen/sys/lib_.rs
} else {
let field_name = field.split(":").next().unwrap().trim().replace("pub ", "");
Some(if field.replace(",", "").trim().ends_with("c_void") {
format!("\t\t .field(\"{name}\", &\"c_void\")\n", name=field_name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is really need write constant string in debug result?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant: maybe better just skip this field?
Current results:

//gobject
impl ::std::fmt::Debug for GParamSpecString {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        f.debug_struct(&format!("GParamSpecString @ {:?}", self as *const _))
         .field("parent_instance", &self.parent_instance)
         .field("default_value", &self.default_value)
         .field("cset_first", &self.cset_first)
         .field("cset_nth", &self.cset_nth)
         .field("substitutor", &self.substitutor)
         .field("_truncated_record_marker", &"c_void")
         .finish()
    }
}

//gtk
impl ::std::fmt::Debug for GtkRcStyle {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        f.debug_struct(&format!("GtkRcStyle @ {:?}", self as *const _))
         .field("parent_instance", &self.parent_instance)
         .field("name", &self.name)
         .field("bg_pixmap_name", &self.bg_pixmap_name)
         .field("font_desc", &self.font_desc)
         .field("color_flags", &self.color_flags)
         .field("fg", &self.fg)
         .field("bg", &self.bg)
         .field("text", &self.text)
         .field("base", &self.base)
         .field("xthickness", &self.xthickness)
         .field("ythickness", &self.ythickness)
         .field("rc_properties", &self.rc_properties)
         .field("rc_style_lists", &self.rc_style_lists)
         .field("icon_factories", &self.icon_factories)
         .field("_truncated_record_marker", &"c_void")
         .finish()
    }
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want all fields to be there. So better having a useless "c_void" string than nothing from my point of view.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@EPashkin

Copy link
Copy Markdown
Member

@sdroege, is good for you now?

Comment thread src/codegen/sys/lib_.rs
w,
&klass.c_type,
&format!("write!(f, \"{name} @ {{:?}}\", self as *const _)",
name=klass.c_type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use debug_tuple(name).field(the_pointer_string).build() here

Comment thread src/codegen/sys/lib_.rs
w,
&interface.c_type,
&format!("write!(f, \"{name} @ {{:?}}\", self as *const _)",
name=interface.c_type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

Comment thread src/codegen/sys/lib_.rs
w,
&record.c_type,
&format!("write!(f, \"{name} @ {{:?}}\", self as *const _)",
name=record.c_type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

@sdroege

sdroege commented Nov 11, 2017

Copy link
Copy Markdown
Member

Feel free to ignore that comment, it's all good to go for me.

@GuillaumeGomez

GuillaumeGomez commented Nov 11, 2017

Copy link
Copy Markdown
Member Author

To give @EPashkin a bit of context: @sdroege and I discussed about it and globally what came out was:

me: I don't see the use for the debug_tuple. If we have the need of it some day, I'll add it but for now it's not really useful.
@sdroege: Ok.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

I'll let you handle the merging @EPashkin.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Sorry but I want to quickly move towards the next release so I'll merge this and make a PR for the sys repository.

@GuillaumeGomez
GuillaumeGomez merged commit e58a8db into gtk-rs:master Nov 11, 2017
@GuillaumeGomez
GuillaumeGomez deleted the debug-impl branch November 11, 2017 15:30
@EPashkin

Copy link
Copy Markdown
Member

Thanks, @GuillaumeGomez

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants