Add Option to Dict.keys field

This commit is contained in:
harupy 2023-01-15 16:43:13 +09:00
parent 2f4fe8b380
commit d7f5dadf47

View file

@ -227,12 +227,12 @@ class StructVisitor(TypeInfoEmitVisitor):
if cons.fields:
self.emit(f"{cons.name} {{", depth)
for f in cons.fields:
self.visit(f, parent, "", depth + 1)
self.visit(f, parent, "", depth + 1, cons.name)
self.emit("},", depth)
else:
self.emit(f"{cons.name},", depth)
def visitField(self, field, parent, vis, depth):
def visitField(self, field, parent, vis, depth, constructor=None):
typ = get_rust_type(field.type)
fieldtype = self.typeinfo.get(field.type)
if fieldtype and fieldtype.has_userdata:
@ -240,7 +240,11 @@ class StructVisitor(TypeInfoEmitVisitor):
# don't box if we're doing Vec<T>, but do box if we're doing Vec<Option<Box<T>>>
if fieldtype and fieldtype.boxed and (not (parent.product or field.seq) or field.opt):
typ = f"Box<{typ}>"
if field.opt:
if field.opt or (
# Add `Option` to allow `Dict.keys` to contain `None` for dictionary unpacking
# in a dict literal.
constructor == "Dict" and field.name == "keys"
):
typ = f"Option<{typ}>"
if field.seq:
typ = f"Vec<{typ}>"