blob: 470a24b26fb9f4b59ae09bd582a1bec5afcafe4f [file] [log] [blame] [edit]
{{/*
// 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 }}),
{{- 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 }}
}
impl fidl::encoding::TopLevel for {{ .Name }} {}
{{ if .IsValueType }}
impl fidl::encoding::Persistable for {{ .Name }} {}
{{- end }}
fidl_union! {
name: {{ .Name }},
members: [
{{- range .Members }}
{{ .Name }} {
ty: {{ .Type }},
ordinal: {{ .Ordinal }},
{{- if .HasHandleMetadata }}
handle_metadata: {
handle_subtype: {{ .HandleSubtype }},
handle_rights: {{ .HandleRights }},
},
{{- end }}
},
{{- end }}
],
{{- if .IsFlexible }}
unknown_member: __Unknown,
{{- end }}
}
{{ end }}