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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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::mem::replace;

use munge::munge;

use super::raw::RawWireVector;
use crate::{
    decode, encode, Decode, Decoder, Encodable, Encode, Encoder, EncoderExt as _, Slot, TakeFrom,
    WireVector,
};

/// An optional FIDL vector
#[repr(transparent)]
pub struct WireOptionalVector<'buf, T> {
    raw: RawWireVector<'buf, T>,
}

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

impl<'buf, T> WireOptionalVector<'buf, T> {
    /// Encodes that a vector is present in a slot.
    pub fn encode_present(slot: Slot<'_, Self>, len: u64) {
        munge!(let Self { raw } = slot);
        RawWireVector::encode_present(raw, len);
    }

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

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

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

    /// Takes the vector out of the option, if any.
    pub fn take(&mut self) -> Option<WireVector<'buf, T>> {
        if self.is_some() {
            Some(unsafe {
                WireVector::new_unchecked(replace(&mut self.raw, RawWireVector::null()))
            })
        } else {
            None
        }
    }

    /// Gets a reference to the vector, if any.
    pub fn as_ref(&self) -> Option<&WireVector<'buf, T>> {
        if self.is_some() {
            Some(unsafe { &*(self as *const Self).cast() })
        } else {
            None
        }
    }

    /// Gets a mutable reference to the vector, if any.
    pub fn as_mut(&mut self) -> Option<&mut WireVector<'buf, T>> {
        if self.is_some() {
            Some(unsafe { &mut *(self as *mut Self).cast() })
        } else {
            None
        }
    }
}

impl<T> Default for WireOptionalVector<'_, T> {
    fn default() -> Self {
        Self { raw: RawWireVector::null() }
    }
}

impl<T: fmt::Debug> fmt::Debug for WireOptionalVector<'_, 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 WireOptionalVector<'buf, T>
{
    fn decode(mut slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), decode::DecodeError> {
        munge!(let Self { raw } = slot.as_mut());
        RawWireVector::decode(raw, decoder)?;

        let this = unsafe { slot.deref_unchecked() };
        if this.raw.as_ptr().is_null() && this.raw.len() != 0 {
            return Err(decode::DecodeError::InvalidOptionalSize(this.raw.len()));
        }

        Ok(())
    }
}

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

impl<E: Encoder + ?Sized, T: Encode<E>> encode::EncodeOption<E> for Vec<T> {
    fn encode_option(
        this: Option<&mut Self>,
        encoder: &mut E,
        slot: Slot<'_, Self::EncodedOption<'_>>,
    ) -> Result<(), encode::EncodeError> {
        if let Some(vec) = this {
            encoder.encode_slice(vec.as_mut_slice())?;
            WireOptionalVector::encode_present(slot, vec.len() as u64);
        } else {
            WireOptionalVector::encode_absent(slot);
        }

        Ok(())
    }
}

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