blob: 03d44452b03227afd80a8e7cecb2d474e2cfdc73 [file] [log] [blame]
{{ self.doc_string(union_.attributes) -}}
#[derive(PartialEq,
{% if !union_.is_resource %}
Clone,
{% endif %}
{% if self.emit_debug_impls() %}
Debug,
{% endif %}
)]
pub enum {{ name }} {
{% for member in union_.members %}
{{ member.name|camel }}({{ self.natural_type(member.ty) }}),
{% endfor %}
{% if !union_.is_strict %}
UnknownOrdinal_(u64),
{% endif %}
}
impl ::fidl_next::Encodable for {{ name }} {
type Encoded = {{ wire_name }}{{ static_ }};
}
unsafe impl<___E> ::fidl_next::Encode<___E> for {{ name }}
where
___E: ::fidl_next::encoder::InternalHandleEncoder + ?Sized,
{% if !is_static %}
___E: ::fidl_next::Encoder,
{% endif %}
{% if union_.is_resource %}
___E: {{ self.encode_trait_path() }},
{% endif %}
{
#[inline]
fn encode(
self,
encoder: &mut ___E,
out: &mut ::core::mem::MaybeUninit<Self::Encoded>,
) -> ::core::result::Result<(), ::fidl_next::EncodeError> {
::fidl_next::munge!(let {{ wire_name }} { raw, _phantom: _ } = out);
match self {
{% for member in union_.members %}
Self::{{ member.name|camel }}(value) => ::fidl_next::RawWireUnion::{{ encode_as }}::<
___E,
{{ self.natural_type(member.ty) }},
>(value, {{ member.ordinal }}, encoder, raw)?,
{% endfor %}
{% if !union_.is_strict %}
Self::UnknownOrdinal_(ordinal) => return Err(
::fidl_next::EncodeError::UnknownUnionOrdinal(ordinal as usize)
)
{% endif %}
}
Ok(())
}
}
{% if !union_.is_resource %}
unsafe impl<___E> ::fidl_next::EncodeRef<___E> for {{ name }}
where
___E: ::fidl_next::encoder::InternalHandleEncoder + ?Sized,
{% if !is_static %}
___E: ::fidl_next::Encoder,
{% endif %}
{
#[inline]
fn encode_ref(
&self,
encoder: &mut ___E,
out: &mut ::core::mem::MaybeUninit<Self::Encoded>,
) -> ::core::result::Result<(), ::fidl_next::EncodeError> {
::fidl_next::munge!(let {{ wire_name }} { raw, _phantom: _ } = out);
match self {
{% for member in union_.members %}
Self::{{ member.name|camel }}(value) => ::fidl_next::RawWireUnion::{{ encode_as }}::<
___E,
&{{ self.natural_type(member.ty) }},
>(value, {{ member.ordinal }}, encoder, raw)?,
{% endfor %}
{% if !union_.is_strict %}
Self::UnknownOrdinal_(ordinal) => return Err(
::fidl_next::EncodeError::UnknownUnionOrdinal(
*ordinal as usize
)
)
{% endif %}
}
Ok(())
}
}
{% endif %}
impl ::fidl_next::EncodableOption for {{ name }} {
type EncodedOption = {{ wire_optional_name }}{{ static_ }};
}
unsafe impl<___E> ::fidl_next::EncodeOption<___E> for {{ name }}
where
___E: ?Sized,
{{ name }}: ::fidl_next::Encode<___E>,
{
#[inline]
fn encode_option(
this: ::core::option::Option<Self>,
encoder: &mut ___E,
out: &mut ::core::mem::MaybeUninit<Self::EncodedOption>,
) -> ::core::result::Result<(), ::fidl_next::EncodeError> {
::fidl_next::munge!(let {{ wire_optional_name }} { raw, _phantom: _ } = &mut *out);
if let Some(inner) = this {
let value_out = unsafe { &mut *out.as_mut_ptr().cast() };
::fidl_next::Encode::encode(
inner,
encoder,
value_out,
)?;
} else {
::fidl_next::RawWireUnion::encode_absent(raw);
}
Ok(())
}
}
{% if !union_.is_resource %}
unsafe impl<___E> ::fidl_next::EncodeOptionRef<___E> for {{ name }}
where
___E: ?Sized,
{{ name }}: ::fidl_next::EncodeRef<___E>,
{
#[inline]
fn encode_option_ref(
this: ::core::option::Option<&Self>,
encoder: &mut ___E,
out: &mut ::core::mem::MaybeUninit<Self::EncodedOption>,
) -> ::core::result::Result<(), ::fidl_next::EncodeError> {
::fidl_next::munge!(let {{ wire_optional_name }} { raw, _phantom: _ } = &mut *out);
if let Some(inner) = this {
let value_out = unsafe { &mut *out.as_mut_ptr().cast() };
::fidl_next::EncodeRef::encode_ref(
inner,
encoder,
value_out,
)?;
} else {
::fidl_next::RawWireUnion::encode_absent(raw);
}
Ok(())
}
}
{% endif %}
impl{{ de }} ::fidl_next::FromWire<{{ wire_name }}{{ de }}> for {{ name }} {
#[inline]
fn from_wire(wire: {{ wire_name }}{{ de }}) -> Self {
let wire = ::core::mem::ManuallyDrop::new(wire);
match wire.raw.ordinal() {
{% for member in union_.members %}
{{ member.ordinal }} => Self::{{ member.name|camel }}(
::fidl_next::FromWire::from_wire(
unsafe { wire.raw.get().read_unchecked::<{{ self.wire_type(member.ty) }}>() }
)
),
{% endfor %}
_ => unsafe { ::core::hint::unreachable_unchecked() },
}
}
}
impl{{ de }} ::fidl_next::IntoNatural for {{ wire_name }}{{ de }} {
type Natural = {{ name }};
}
{% if !union_.is_resource %}
impl{{ de }} ::fidl_next::FromWireRef<{{ wire_name }}{{ de }}> for {{ name }} {
#[inline]
fn from_wire_ref(wire: &{{ wire_name }}{{ de }}) -> Self {
match wire.raw.ordinal() {
{% for member in union_.members %}
{{ member.ordinal }} => Self::{{ member.name|camel }}(
::fidl_next::FromWireRef::from_wire_ref(
unsafe { wire.raw.get().deref_unchecked::<{{ self.wire_type(member.ty) }}>() }
)
),
{% endfor %}
_ => unsafe { ::core::hint::unreachable_unchecked() },
}
}
}
{% endif %}
impl{{ de }} ::fidl_next::FromWireOption<{{ wire_optional_name }}{{ de }}> for {{ name }} {
#[inline]
fn from_wire_option(wire: {{ wire_optional_name }}{{ de }}) -> ::core::option::Option<Self> {
if let Some(inner) = wire.into_option() {
Some(::fidl_next::FromWire::from_wire(inner))
} else {
None
}
}
}
impl{{ de }} ::fidl_next::IntoNatural for {{ wire_optional_name }}{{ de }} {
type Natural = ::core::option::Option<{{ name }}>;
}
impl{{ de }} ::fidl_next::FromWireOption<{{ wire_optional_name }}{{ de }}> for Box<{{ name }}> {
#[inline]
fn from_wire_option(wire: {{ wire_optional_name }}{{ de }}) -> ::core::option::Option<Self> {
<
{{ name }} as ::fidl_next::FromWireOption<{{ wire_optional_name }}{{ de }}>
>::from_wire_option(wire).map(Box::new)
}
}
{% if !union_.is_resource %}
impl{{ de }} ::fidl_next::FromWireOptionRef<{{ wire_optional_name }}{{ de }}> for Box<{{ name }}> {
#[inline]
fn from_wire_option_ref(wire: &{{ wire_optional_name }}{{ de }}) -> ::core::option::Option<Self> {
if let Some(inner) = wire.as_ref() {
Some(Box::new(::fidl_next::FromWireRef::from_wire_ref(inner)))
} else {
None
}
}
}
{% endif %}
/// The wire type corresponding to [`{{ name }}`].
#[repr(transparent)]
pub struct {{ wire_name }}{{ de }} {
raw: ::fidl_next::RawWireUnion,
_phantom: ::core::marker::PhantomData<{{ phantom }}>,
}
impl{{ de }} Drop for {{ wire_name }}{{ de }} {
fn drop(&mut self) {
match self.raw.ordinal() {
{% for member in union_.members %}
{{ member.ordinal }} => {
let _ = unsafe {
self.raw.get().read_unchecked::<{{ self.wire_type(member.ty) }}>()
};
},
{% endfor %}
{% if union_.is_strict %}
_ => unsafe { ::core::hint::unreachable_unchecked() },
{% else %}
_ => (),
{% endif %}
}
}
}
unsafe impl ::fidl_next::Wire for {{ wire_name }}{{ static_ }} {
type Decoded<'de> = {{ wire_name }}{{ de }};
#[inline]
fn zero_padding(out: &mut ::core::mem::MaybeUninit<Self>) {
::fidl_next::munge!(let Self { raw, _phantom: _ } = out);
::fidl_next::RawWireUnion::zero_padding(raw);
}
}
{% let access_params %}
{% let access_args %}
{% if union_.members.is_empty() %}
{% let access_params = "" %}
{% let access_args = "" %}
{% else %}
{% let access_params = "<'de>" %}
{% let access_args = "<'_>" %}
{% endif %}
pub mod {{ mod_name }} {
pub enum Ref{{ access_params }} {
{% for member in union_.members %}
{{ member.name|camel }}(&'de {{ self.wire_type(member.ty) }}),
{% endfor %}
{% if !union_.is_strict %}
UnknownOrdinal_(u64),
{% endif %}
}
}
impl{{ de }} {{ wire_name }}{{ de }} {
pub fn as_ref(&self) -> crate::{{ mod_name }}::Ref{{ access_args }} {
match self.raw.ordinal() {
{% for member in union_.members %}
{{ member.ordinal }} => crate::{{ mod_name }}::Ref::{{ member.name|camel }}(
unsafe { self.raw.get().deref_unchecked::<{{ self.anonymous_wire_type(member.ty) }}>() }
),
{% endfor %}
{% if union_.is_strict %}
_ => unsafe { ::core::hint::unreachable_unchecked() },
{% else %}
unknown => crate::{{ mod_name }}::Ref::UnknownOrdinal_(unknown),
{% endif %}
}
}
}
{% if is_static && !union_.is_resource %}
impl{{ de }} Clone for {{ wire_name }}{{ de }} {
fn clone(&self) -> Self {
match self.raw.ordinal() {
{% for member in union_.members %}
{{ member.ordinal }} => Self {
raw: unsafe { self.raw.clone_inline_unchecked::<{{ self.wire_type(member.ty) }}>() },
_phantom: ::core::marker::PhantomData,
},
{% endfor %}
{% if union_.is_strict %}
_ => unsafe { ::core::hint::unreachable_unchecked() },
{% else %}
_ => Self {
raw: unsafe { self.raw.clone_inline_unchecked::<()>() },
_phantom: ::core::marker::PhantomData,
},
{% endif %}
}
}
}
{% endif %}
unsafe impl<___D> ::fidl_next::Decode<___D> for {{ wire_name }}{{ static_ }}
where
___D: ::fidl_next::decoder::InternalHandleDecoder + ?Sized,
{% if !is_static %}
___D: ::fidl_next::Decoder,
{% endif %}
{% if union_.is_resource %}
___D: {{ self.decode_trait_path() }},
{% endif %}
{
fn decode(
mut slot: ::fidl_next::Slot<'_, Self>,
decoder: &mut ___D,
) -> ::core::result::Result<(), ::fidl_next::DecodeError> {
::fidl_next::munge!(let Self { mut raw, _phantom: _ } = slot.as_mut());
match ::fidl_next::RawWireUnion::encoded_ordinal(raw.as_mut()) {
{% for member in union_.members %}
{{ member.ordinal }} => ::fidl_next::RawWireUnion::{{ decode_as }}::<___D, {{ self.static_wire_type(member.ty) }}>(raw, decoder)?,
{% endfor %}
{% if union_.is_strict %}
ord => return Err(::fidl_next::DecodeError::InvalidUnionOrdinal(ord as usize)),
{% else %}
_ => ::fidl_next::RawWireUnion::{{ decode_unknown }}(raw, decoder)?,
{% endif %}
}
Ok(())
}
}
{% if self.emit_debug_impls() %}
impl{{ de }} ::core::fmt::Debug for {{ wire_name }}{{ de }} {
fn fmt(
&self,
f: &mut ::core::fmt::Formatter<'_>,
) -> ::core::fmt::Result {
match self.raw.ordinal() {
{% for member in union_.members -%}
{{ member.ordinal }} => unsafe {
self.raw.get().deref_unchecked::<
{{ self.anonymous_wire_type(member.ty) }}
>().fmt(f)
},
{%- endfor %}
_ => unsafe { ::core::hint::unreachable_unchecked() },
}
}
}
{% endif %}
#[repr(transparent)]
pub struct {{ wire_optional_name }}{{ de }} {
raw: ::fidl_next::RawWireUnion,
_phantom: ::core::marker::PhantomData<{{ phantom }}>,
}
unsafe impl ::fidl_next::Wire for {{ wire_optional_name }}{{ static_ }} {
type Decoded<'de> = {{ wire_optional_name }}{{ de }};
#[inline]
fn zero_padding(out: &mut ::core::mem::MaybeUninit<Self>) {
::fidl_next::munge!(let Self { raw, _phantom: _ } = out);
::fidl_next::RawWireUnion::zero_padding(raw);
}
}
impl{{ de }} {{ wire_optional_name }}{{ de }} {
pub fn is_some(&self) -> bool {
self.raw.is_some()
}
pub fn is_none(&self) -> bool {
self.raw.is_none()
}
pub fn as_ref(&self) -> ::core::option::Option<&{{ wire_name }}{{ de }}> {
if self.is_some() {
Some(unsafe { &*(self as *const Self).cast() })
} else {
None
}
}
pub fn into_option(self) -> ::core::option::Option<{{ wire_name }}{{ de }}> {
if self.is_some() {
Some({{ wire_name }} {
raw: self.raw,
_phantom: ::core::marker::PhantomData,
})
} else {
None
}
}
}
{% if is_static && !union_.is_resource %}
impl{{ de }} Clone for {{ wire_optional_name }}{{ de }} {
fn clone(&self) -> Self {
if self.is_none() {
return {{ wire_optional_name }} {
raw: ::fidl_next::RawWireUnion::absent(),
_phantom: ::core::marker::PhantomData,
};
}
match self.raw.ordinal() {
{% for member in union_.members %}
{{ member.ordinal }} => Self {
raw: unsafe { self.raw.clone_inline_unchecked::<{{ self.wire_type(member.ty) }}>() },
_phantom: ::core::marker::PhantomData,
},
{% endfor %}
{% if union_.is_strict %}
_ => unsafe { ::core::hint::unreachable_unchecked() },
{% else %}
_ => Self {
raw: unsafe { self.raw.clone_inline_unchecked::<()>() },
_phantom: ::core::marker::PhantomData,
},
{% endif %}
}
}
}
{% endif %}
unsafe impl<___D> ::fidl_next::Decode<___D> for {{ wire_optional_name }}{{ static_ }}
where
___D: ::fidl_next::decoder::InternalHandleDecoder + ?Sized,
{% if !is_static %}
___D: ::fidl_next::Decoder,
{% endif %}
{% if union_.is_resource %}
___D: {{ self.decode_trait_path() }},
{% endif %}
{
fn decode(
mut slot: ::fidl_next::Slot<'_, Self>,
decoder: &mut ___D,
) -> ::core::result::Result<(), ::fidl_next::DecodeError> {
::fidl_next::munge!(let Self { mut raw, _phantom: _ } = slot.as_mut());
match ::fidl_next::RawWireUnion::encoded_ordinal(raw.as_mut()) {
{% for member in union_.members %}
{{ member.ordinal }} => ::fidl_next::RawWireUnion::{{ decode_as }}::<___D, {{ self.static_wire_type(member.ty) }}>(raw, decoder)?,
{% endfor %}
0 => ::fidl_next::RawWireUnion::decode_absent(raw)?,
_ => ::fidl_next::RawWireUnion::{{ decode_unknown}}(
raw,
decoder,
)?,
}
Ok(())
}
}
{% if self.emit_debug_impls() %}
impl{{ de }} ::core::fmt::Debug for {{ wire_optional_name }}{{ de }} {
fn fmt(
&self,
f: &mut ::core::fmt::Formatter<'_>,
) -> ::core::fmt::Result {
self.as_ref().fmt(f)
}
}
{% endif %}