| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package templates | 5 package templates |
| 6 | 6 |
| 7 const GenerateUnion = ` | 7 const GenerateUnion = ` |
| 8 {{/* . (dot) refers to a the Go type |rustgen.UnionTemplate| */}} | 8 {{/* . (dot) refers to a the Go type |rustgen.UnionTemplate| */}} |
| 9 {{- define "GenerateUnion" -}} | 9 {{- define "GenerateUnion" -}} |
| 10 {{- $union := . -}} | 10 {{- $union := . -}} |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 {{range $field := $union.Fields}} {{$union.Name}}::{{$field.Name}}(_)
=> {{$union.TagsEnum.Name}}_{{$field.Name}}, | 24 {{range $field := $union.Fields}} {{$union.Name}}::{{$field.Name}}(_)
=> {{$union.TagsEnum.Name}}_{{$field.Name}}, |
| 25 {{end}} {{$union.Name}}::_Unknown(_) => {{$union.TagsEnum.Name}}__UNK
NOWN, | 25 {{end}} {{$union.Name}}::_Unknown(_) => {{$union.TagsEnum.Name}}__UNK
NOWN, |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 fn encode_value(self, encoder: &mut Encoder, context: Context) { | 28 fn encode_value(self, encoder: &mut Encoder, context: Context) { |
| 29 match self { | 29 match self { |
| 30 {{range $field := $union.Fields}} {{$union.Name}}::{{$field.Name}}(va
l) => MojomEncodable::encode(val, encoder, context.clone()), | 30 {{range $field := $union.Fields}} {{$union.Name}}::{{$field.Name}}(va
l) => MojomEncodable::encode(val, encoder, context.clone()), |
| 31 {{end}} {{$union.Name}}::_Unknown(val) => MojomEncodable::encode(val,
encoder, context.clone()), | 31 {{end}} {{$union.Name}}::_Unknown(val) => MojomEncodable::encode(val,
encoder, context.clone()), |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 fn decode_value(decoder: &mut Decoder, context: Context) -> Self { | 34 fn decode_value(decoder: &mut Decoder, context: Context) -> Result<Self, Val
idationError> { |
| 35 // TODO(mknyszek): Validate bytes and tag | 35 let tag = { |
| 36 let (_bytes, tag) = { | |
| 37 let mut state = decoder.get_mut(&context); | 36 let mut state = decoder.get_mut(&context); |
| 38 let bytes = state.decode::<u32>(); | 37 let bytes = state.decode::<u32>(); |
| 39 » let tag = state.decode::<u32>(); | 38 if (bytes as usize) != UNION_SIZE { |
| 40 » (bytes, tag) | 39 return Err(ValidationError::UnexpectedNullUnion); |
| 40 } |
| 41 » state.decode::<u32>() |
| 41 }; | 42 }; |
| 42 match tag { | 43 Ok(match tag { |
| 43 {{range $field := $union.Fields}} {{$union.TagsEnum.Name}}_{{$field.N
ame}} => {{$union.Name}}::{{$field.Name}}(<{{$field.Type}}>::decode(decoder, con
text.clone())), | 44 {{range $field := $union.Fields}} {{$union.TagsEnum.Name}}_{{$field.N
ame}} => {{$union.Name}}::{{$field.Name}}({ |
| 44 {{end}} _ => {{$union.Name}}::_Unknown(u64::decode(decoder, context.clone
())), | 45 match <{{$field.Type}}>::decode(decoder, context.clone()) { |
| 45 } | 46 Ok(value) => value, |
| 47 Err(err) => return Err(err), |
| 48 } |
| 49 }), |
| 50 {{end}} _ => {{$union.Name}}::_Unknown(u64::decode(decoder, context.clone
()).unwrap()), |
| 51 }) |
| 46 } | 52 } |
| 47 } | 53 } |
| 48 | 54 |
| 49 impl MojomEncodable for {{$union.Name}} { | 55 impl MojomEncodable for {{$union.Name}} { |
| 50 impl_encodable_for_union!(); | 56 impl_encodable_for_union!(); |
| 51 fn compute_size(&self, context: Context) -> usize { | 57 fn compute_size(&self, context: Context) -> usize { |
| 52 UNION_SIZE + | 58 UNION_SIZE + |
| 53 match *self { | 59 match *self { |
| 54 {{range $field := $union.Fields}} {{$union.Name}}::{{$field.Name}}(re
f val) => val.compute_size(context.clone()), | 60 {{range $field := $union.Fields}} {{$union.Name}}::{{$field.Name}}(re
f val) => val.compute_size(context.clone()), |
| 55 {{end}} {{$union.Name}}::_Unknown(ref val) => 0, | 61 {{end}} {{$union.Name}}::_Unknown(ref val) => 0, |
| 56 } | 62 } |
| 57 } | 63 } |
| 58 } | 64 } |
| 59 {{end}} | 65 {{end}} |
| 60 ` | 66 ` |
| OLD | NEW |