Skip to content
This repository was archived by the owner on Nov 26, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions grust/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,33 @@ def map_field_type(self, field):
.format(field.name, typenode))
return self._map_type(field.type, nullable=True)

def _struct_field_type_is_valid(self, field_type):
if field_type is None:
return False
if isinstance(field_type, ast.Union):
return False
if isinstance(field_type, ast.Array):
return self._struct_field_type_is_valid(field_type.element_type)
if isinstance(field_type, ast.Record):
return self.struct_is_valid(field_type)
if isinstance(field_type, ast.Type) and field_type.target_giname:
if not self.node_is_mappable(field_type):
return False
if field_type.ctype is None or not field_type.ctype.endswith('*'):
crate,name = self._lookup_giname(field_type.target_giname)
return self._struct_field_type_is_valid(crate.namespace.names[name])
return True

def struct_is_valid(self, node):
if isinstance(node, ast.Union):
return False
for field in node.fields:
if field.bits is not None:
return False
if not self._struct_field_type_is_valid(field.type):
return False
return True

def node_is_mappable(self, node):
if isinstance(node, ast.Type) and node == ast.TYPE_VALIST:
return False
Expand Down
9 changes: 7 additions & 2 deletions grust/templates/sys/crate.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def indenter(amount):
ast.Constant: constant,
ast.Record: struct,
ast.Class: struct,
ast.Union: struct,
ast.Interface: interface,
ast.Enum: enum,
ast.Bitfield: flags
Expand Down Expand Up @@ -206,11 +207,15 @@ pub struct ${node.ctype}(gpointer);
pub enum ${node.ctype} { }
% else:
#[repr(C)]
% if not mapper.struct_is_valid(node):
pub struct ${node.ctype};
Copy link
Member

Choose a reason for hiding this comment

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

This might work for objects and some boxed types, but I wouldn't like to get unusable types for copyable values. What I did in the few crate-specific templates I created in repo gi-rust/crates is suppress the unmappable structures and place manual representations in the crate template. With support for unions available in stable Rust, there will be less need for these overrides.

Bit fields could be automated too, if there is a equivalent #[repr(C)] guaranteed by the C standard.

% else:
pub struct ${node.ctype} {
% for field in node.fields:
% for field in node.fields:
${'' if field.private else 'pub '}${sanitize_ident(field.name)}: ${mapper.map_field_type(field)},
% endfor
% endfor
}
% endif
% endif
</%def>\
##
Expand Down