fidl_next_codec/
util.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
5//! Helper types for encoding and decoding.
6
7use core::hint::unreachable_unchecked;
8use core::marker::PhantomData;
9use core::mem::MaybeUninit;
10
11use crate::{Constrained, Encodable, Encode, EncodeError, Unconstrained, Wire};
12
13/// A type which cannot be constructed.
14pub enum Never {}
15
16impl Unconstrained for Never {}
17
18/// A type which cannot be constructed and encodes as a `W`.
19pub struct EncodableNever<W> {
20    _never: Never,
21    _phantom: PhantomData<W>,
22}
23
24impl<W: Wire + Constrained> Encodable for EncodableNever<W> {
25    type Encoded = W;
26}
27
28unsafe impl<E: ?Sized, W: Wire + Constrained> Encode<E> for EncodableNever<W> {
29    fn encode(
30        self,
31        _: &mut E,
32        _: &mut MaybeUninit<Self::Encoded>,
33        _: W::Constraint,
34    ) -> Result<(), EncodeError> {
35        // SAFETY: `EncodableNever` cannot exist because it has a `Never` field.
36        // Therefore, this code can never be reached.
37        unsafe { unreachable_unchecked() }
38    }
39}