1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2024 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.

use core::fmt;
use core::ptr::NonNull;

use munge::munge;

use crate::{
    decode, encode, Decode, Decoder, DecoderExt as _, Encodable, Encode, Slot, TakeFrom,
    WirePointer,
};

/// A boxed (optional) FIDL value.
#[repr(C)]
pub struct WireBox<'buf, T> {
    ptr: WirePointer<'buf, T>,
}

impl<T> Drop for WireBox<'_, T> {
    fn drop(&mut self) {
        if self.is_some() {
            unsafe {
                self.ptr.as_ptr().drop_in_place();
            }
        }
    }
}

impl<T> WireBox<'_, T> {
    /// Encodes that a value is present in a slot.
    pub fn encode_present(slot: Slot<'_, Self>) {
        munge!(let Self { ptr } = slot);
        WirePointer::encode_present(ptr);
    }

    /// Encodes that a value is absent in a slot.
    pub fn encode_absent(slot: Slot<'_, Self>) {
        munge!(let Self { ptr } = slot);
        WirePointer::encode_absent(ptr);
    }

    /// Returns whether the value is present.
    pub fn is_some(&self) -> bool {
        !self.ptr.as_ptr().is_null()
    }

    /// Returns whether the value is absent.
    pub fn is_none(&self) -> bool {
        !self.is_some()
    }

    /// Returns a reference to the boxed value, if any.
    pub fn as_ref(&self) -> Option<&T> {
        NonNull::new(self.ptr.as_ptr()).map(|ptr| unsafe { ptr.as_ref() })
    }

    /// Returns a mutable reference to the boxed value, if any.
    pub fn as_mut(&mut self) -> Option<&mut T> {
        NonNull::new(self.ptr.as_ptr()).map(|mut ptr| unsafe { ptr.as_mut() })
    }
}

impl<T> Default for WireBox<'_, T> {
    fn default() -> Self {
        Self { ptr: WirePointer::null() }
    }
}

impl<T: fmt::Debug> fmt::Debug for WireBox<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

unsafe impl<'buf, D: Decoder<'buf> + ?Sized, T: Decode<D>> Decode<D> for WireBox<'buf, T> {
    fn decode(slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), decode::DecodeError> {
        munge!(let Self { mut ptr } = slot);

        if WirePointer::is_encoded_present(ptr.as_mut())? {
            let value = decoder.decode_next::<T>()?;
            WirePointer::set_decoded(ptr, value);
        }

        Ok(())
    }
}

impl<T: encode::EncodableOption> Encodable for Option<T> {
    type Encoded<'buf> = T::EncodedOption<'buf>;
}

impl<E: ?Sized, T: encode::EncodeOption<E>> Encode<E> for Option<T> {
    fn encode(
        &mut self,
        encoder: &mut E,
        slot: Slot<'_, Self::Encoded<'_>>,
    ) -> Result<(), encode::EncodeError> {
        T::encode_option(self.as_mut(), encoder, slot)
    }
}

impl<T: TakeFrom<WT>, WT> TakeFrom<WireBox<'_, WT>> for Option<T> {
    fn take_from(from: &mut WireBox<'_, WT>) -> Self {
        from.as_mut().map(|value| T::take_from(value))
    }
}