Index: mojo/public/tools/bindings/generators/cpp_templates/struct_traits_declaration.tmpl |
diff --git a/mojo/public/tools/bindings/generators/cpp_templates/struct_traits_declaration.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/struct_traits_declaration.tmpl |
index 24efbd5531064d623c8922793359cd78f98022ac..9285f992c811a5041476ea65ca55c7072e526825 100644 |
--- a/mojo/public/tools/bindings/generators/cpp_templates/struct_traits_declaration.tmpl |
+++ b/mojo/public/tools/bindings/generators/cpp_templates/struct_traits_declaration.tmpl |
@@ -9,9 +9,15 @@ struct {{export_attribute}} StructTraits<{{mojom_type}}::DataView, |
{%- for field in struct.fields %} |
{%- set return_ref = field.kind|is_object_kind or |
field.kind|is_any_handle_or_interface_kind %} |
+{# We want the field accessor to be const whenever possible to allow #} |
+{# structs to be used as map keys. #} |
+{# TODO(tibell): Make this check more precise to deal with e.g. #} |
+{# custom types which don't contain handles but require non-const #} |
+{# reference for serialization. #} |
+{%- set maybe_const = "" if field.kind|contains_handles else "const" %} |
{%- if return_ref %} |
- static decltype({{mojom_type}}::{{field.name}})& {{field.name}}( |
- {{mojom_type}}Ptr& input) { |
+ static {{maybe_const}} decltype({{mojom_type}}::{{field.name}})& {{field.name}}( |
+ {{maybe_const}} {{mojom_type}}Ptr& input) { |
return input->{{field.name}}; |
} |
{%- else %} |