| {{/* |
| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| */}} |
| |
| {{- define "UnionDeclaration" }} |
| {{- range .DocComments }} |
| ///{{ . }} |
| {{- end }} |
| {{- if .IsFlexible }} |
| {{ .Derives.RemoveCustom "PartialEq" }} |
| {{- else }} |
| {{ .Derives }} |
| {{- end }} |
| pub enum {{ .Name }} { |
| {{- range .Members }} |
| {{- range .DocComments }} |
| ///{{ . }} |
| {{- end }} |
| {{ .Name }}({{ .Type.Owned }}), |
| {{- end }} |
| {{- if .IsFlexible }} |
| #[deprecated = "Use `{{ .Name }}::unknown()` to construct and `{{ .Name }}Unknown!()` to exhaustively match."] |
| #[doc(hidden)] |
| #[non_exhaustive] |
| __Unknown { |
| ordinal: u64, |
| }, |
| {{- end }} |
| } |
| |
| {{- if .IsFlexible }} |
| /// Pattern that matches an unknown `{{ .Name }}` member. |
| #[macro_export] |
| macro_rules! {{ .Name }}Unknown { |
| () => { _ }; |
| } |
| |
| // Custom PartialEq so that unknown variants are not equal to themselves. |
| impl PartialEq for {{ .Name }} { |
| fn eq(&self, other: &Self) -> bool { |
| match (self, other) { |
| {{- range .Members }} |
| (Self::{{ .Name }}(x), Self::{{ .Name }}(y)) => *x == *y, |
| {{- end }} |
| _ => false, |
| } |
| } |
| } |
| {{- end }} |
| |
| impl {{ .Name }} { |
| #[inline] |
| pub fn ordinal(&self) -> u64 { |
| match *self { |
| {{- range .Members }} |
| Self::{{ .Name }}(_) => {{ .Ordinal }}, |
| {{- end }} |
| {{- if .IsFlexible }} |
| #[allow(deprecated)] |
| Self::__Unknown { ordinal } => ordinal, |
| {{- end }} |
| } |
| } |
| |
| {{- if .IsStrict }} |
| #[deprecated = "Strict unions should not use `is_unknown`"] |
| #[inline] |
| pub fn is_unknown(&self) -> bool { |
| false |
| } |
| {{- end }} |
| |
| {{- if .IsFlexible }} |
| #[inline] |
| pub fn unknown_variant_for_testing() -> Self { |
| #[allow(deprecated)] |
| Self::__Unknown { ordinal: 0 } |
| } |
| |
| #[inline] |
| pub fn is_unknown(&self) -> bool { |
| match self { |
| #[allow(deprecated)] |
| Self::__Unknown { .. } => true, |
| {{- if .Members }} |
| _ => false, |
| {{- end }} |
| } |
| } |
| {{- end }} |
| } |
| |
| {{- if .IsResourceType }} |
| impl fidl::Standalone for {{ .Name }} {} |
| {{- else }} |
| impl fidl::Persistable for {{ .Name }} {} |
| {{- end }} |
| |
| fidl_union! { |
| name: {{ .Name }}, |
| {{- if .IsResourceType }} |
| resource: true, |
| {{- end }} |
| members: [ |
| {{- range .Members }} |
| {{ .Name }} { |
| ty: {{ .Type.Fidl }}, |
| {{- if .Type.IsResourceType }} |
| resource: true, |
| {{- end }} |
| ordinal: {{ .Ordinal }}, |
| }, |
| {{- end }} |
| ], |
| {{- if .IsFlexible }} |
| unknown_member: __Unknown, |
| {{- end }} |
| } |
| {{- end }} |