Skip to main content

fidl_next_codec/wire/
empty_struct.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use core::mem::MaybeUninit;
6
7use crate::{Constrained, Slot, ValidationError, Wire};
8
9/// An empty struct's wire representation. C/C++ memory layout rules (and hence
10/// FIDL wire rules) require every object to have a unique address so we have to
11/// make a single, tiny type for empty structs.
12#[repr(u8)]
13#[derive(Clone, Copy)]
14pub enum EmptyStruct {
15    /// Empty structs are represented as a single 0u8.
16    Zero = 0,
17}
18
19impl Constrained for EmptyStruct {
20    type Constraint = ();
21
22    fn validate(_: Slot<'_, Self>, _: Self::Constraint) -> Result<(), ValidationError> {
23        Ok(())
24    }
25}
26
27unsafe impl Wire for EmptyStruct {
28    type Narrowed<'de> = Self;
29
30    #[inline]
31    fn zero_padding(_: &mut MaybeUninit<Self>) {}
32}
33
34impl core::fmt::Debug for EmptyStruct {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        write!(f, "(empty)")
37    }
38}