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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! The [`OperatorAddr`] type

use crate::ids::{OperatorId, PortId};
use core::{
    convert::TryFrom,
    fmt::{self, Debug, Display},
    iter::{FromIterator, IntoIterator},
    mem::ManuallyDrop,
    ops::Deref,
    slice,
};
use tinyvec::{ArrayVec, TinyVec};

#[cfg(feature = "enable_abomonation")]
use abomonation::Abomonation;
#[cfg(feature = "enable_abomonation")]
use std::io;

#[cfg(feature = "serde")]
use serde_dep::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};

#[cfg(feature = "rkyv")]
use rkyv_dep::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};

// TODO: Change this to use `OperatorId` instead of `usize`
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(
    feature = "serde",
    derive(SerdeSerialize, SerdeDeserialize),
    serde(crate = "serde_dep", transparent)
)]
#[cfg_attr(
    feature = "rkyv",
    derive(Archive, RkyvSerialize, RkyvDeserialize),
    archive(crate = "rkyv_dep", repr(transparent)),
    archive_attr(derive(bytecheck::CheckBytes))
)]
#[repr(transparent)]
pub struct OperatorAddr {
    addr: TinyVec<[OperatorId; 8]>,
}

impl OperatorAddr {
    #[inline]
    pub const fn new(addr: TinyVec<[OperatorId; 8]>) -> Self {
        Self { addr }
    }

    #[inline]
    pub fn from_elem(segment: OperatorId) -> Self {
        let zero = OperatorId::new(0);

        Self::new(TinyVec::Inline(ArrayVec::from_array_len(
            [segment, zero, zero, zero, zero, zero, zero, zero],
            1,
        )))
    }

    #[inline]
    fn from_vec(addr: Vec<OperatorId>) -> Self {
        let tiny_vec = ArrayVec::try_from(addr.as_slice())
            .map_or_else(|_| TinyVec::Heap(addr), TinyVec::Inline);

        Self::new(tiny_vec)
    }

    #[inline]
    pub fn from_slice(addr: &[OperatorId]) -> Self {
        let tiny_vec =
            ArrayVec::try_from(addr).map_or_else(|_| TinyVec::Heap(addr.to_vec()), TinyVec::Inline);

        Self::new(tiny_vec)
    }

    #[inline]
    pub fn is_top_level(&self) -> bool {
        self.len() == 1
    }

    #[inline]
    pub fn push(&mut self, segment: PortId) {
        self.addr.push(OperatorId::new(segment.into_inner()));
    }

    #[inline]
    pub fn push_imm(&self, elem: PortId) -> Self {
        let mut this = self.clone();
        this.push(elem);
        this
    }

    #[inline]
    pub fn pop(&mut self) -> Option<OperatorId> {
        self.addr.pop()
    }

    #[inline]
    pub fn pop_imm(&self) -> (Self, Option<OperatorId>) {
        let mut this = self.clone();
        let popped = this.pop();

        (this, popped)
    }

    #[inline]
    pub fn as_slice(&self) -> &[OperatorId] {
        self.addr.as_slice()
    }

    #[inline]
    pub fn iter(&self) -> slice::Iter<'_, OperatorId> {
        self.as_slice().iter()
    }
}

impl From<&[OperatorId]> for OperatorAddr {
    #[inline]
    fn from(addr: &[OperatorId]) -> Self {
        Self::from_slice(addr)
    }
}

impl From<&Vec<OperatorId>> for OperatorAddr {
    #[inline]
    fn from(addr: &Vec<OperatorId>) -> Self {
        Self::from_slice(addr)
    }
}

impl From<Vec<OperatorId>> for OperatorAddr {
    #[inline]
    fn from(addr: Vec<OperatorId>) -> Self {
        Self::from_vec(addr)
    }
}

impl From<Vec<usize>> for OperatorAddr {
    #[inline]
    fn from(addr: Vec<usize>) -> Self {
        // FIXME: Use `Vec::into_raw_parts()` once that's stable
        // FIXME: Use `Vec::into_raw_parts_with_alloc()` once that's stable
        let addr: Vec<OperatorId> = {
            let mut addr = ManuallyDrop::new(addr);
            let (ptr, len, cap) = (addr.as_mut_ptr().cast(), addr.len(), addr.capacity());

            // Safety: `OperatorId` is a transparent wrapper around `usize`
            unsafe { Vec::from_raw_parts(ptr, len, cap) }
        };

        let tiny_vec = ArrayVec::try_from(addr.as_slice())
            .map_or_else(|_| TinyVec::Heap(addr), TinyVec::Inline);

        Self::new(tiny_vec)
    }
}

impl Deref for OperatorAddr {
    type Target = [OperatorId];

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.addr
    }
}

impl Extend<OperatorId> for OperatorAddr {
    #[inline]
    fn extend<T>(&mut self, segments: T)
    where
        T: IntoIterator<Item = OperatorId>,
    {
        self.addr.extend(segments);
    }
}

impl<'a> Extend<&'a OperatorId> for OperatorAddr {
    #[inline]
    fn extend<T>(&mut self, segments: T)
    where
        T: IntoIterator<Item = &'a OperatorId>,
    {
        self.addr.extend(segments.into_iter().copied());
    }
}

impl FromIterator<OperatorId> for OperatorAddr {
    #[inline]
    fn from_iter<T: IntoIterator<Item = OperatorId>>(iter: T) -> Self {
        Self {
            addr: <TinyVec<[OperatorId; 8]>>::from_iter(iter),
        }
    }
}

impl<'a> FromIterator<&'a OperatorId> for OperatorAddr {
    #[inline]
    fn from_iter<T: IntoIterator<Item = &'a OperatorId>>(iter: T) -> Self {
        Self {
            addr: iter.into_iter().copied().collect(),
        }
    }
}

impl Debug for OperatorAddr {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // We can forward to our display implementation
        Display::fmt(self, f)
    }
}

impl Display for OperatorAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            // FIXME: Is there really no way to make `.debug_list()`
            //        call `Display` on elements?
            .entries(self.as_slice().iter().copied().map(OperatorId::into_inner))
            .finish()
    }
}

#[cfg(feature = "enable_abomonation")]
impl Abomonation for OperatorAddr {
    #[inline]
    unsafe fn entomb<W: io::Write>(&self, write: &mut W) -> io::Result<()> {
        match &self.addr {
            TinyVec::Inline(array) => array.into_inner().entomb(write),
            TinyVec::Heap(vec) => vec.entomb(write),
        }
    }

    #[inline]
    unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
        let mut inner = Vec::new();
        let output = Vec::exhume(&mut inner, bytes)?;
        self.addr = TinyVec::Heap(inner);

        Some(output)
    }

    #[inline]
    fn extent(&self) -> usize {
        match &self.addr {
            TinyVec::Inline(array) => array.into_inner().extent(),
            TinyVec::Heap(vec) => vec.extent(),
        }
    }
}