zng-task 0.13.0

Part of the zng project.
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#![cfg_attr(not(ipc), allow(unused))]

use std::{fmt, io, iter::FusedIterator, marker::PhantomData, ops};

use crate::channel::{IpcBytes, IpcBytesIntoIter, IpcBytesMut};

/// Safe bytemuck casting wrapper for [`IpcBytesMut`].
///
/// Use [`IpcBytesMut::cast`] to cast.
pub struct IpcBytesMutCast<T: bytemuck::AnyBitPattern> {
    bytes: IpcBytesMut,
    _t: PhantomData<T>,
}
impl<T: bytemuck::AnyBitPattern> ops::Deref for IpcBytesMutCast<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        bytemuck::cast_slice::<u8, T>(&self.bytes)
    }
}
impl<T: bytemuck::AnyBitPattern + bytemuck::NoUninit> ops::DerefMut for IpcBytesMutCast<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        bytemuck::cast_slice_mut::<u8, T>(&mut self.bytes)
    }
}
impl<T: bytemuck::AnyBitPattern> IpcBytesMutCast<T> {
    /// Convert back to [`IpcBytesMut`].
    pub fn into_inner(self) -> IpcBytesMut {
        self.bytes
    }
}
impl<T: bytemuck::AnyBitPattern> From<IpcBytesMutCast<T>> for IpcBytesMut {
    fn from(value: IpcBytesMutCast<T>) -> Self {
        value.bytes
    }
}
fn item_len_to_bytes<T: 'static>(len: usize) -> io::Result<usize> {
    match len.checked_mul(size_of::<T>()) {
        Some(l) => Ok(l),
        None => Err(io::Error::new(io::ErrorKind::FileTooLarge, "cannot map more than usize::MAX")),
    }
}
impl<T: bytemuck::AnyBitPattern + bytemuck::NoUninit> IpcBytesMutCast<T> {
    /// Allocate zeroed mutable memory.
    pub async fn new(len: usize) -> io::Result<Self> {
        IpcBytesMut::new(item_len_to_bytes::<T>(len)?).await.map(IpcBytesMut::cast)
    }

    /// Allocate zeroed mutable memory.
    pub fn new_blocking(len: usize) -> io::Result<Self> {
        IpcBytesMut::new_blocking(item_len_to_bytes::<T>(len)?).map(IpcBytesMut::cast)
    }

    /// Allocate zeroed mutable memory in a memory map.
    ///
    /// Note that [`new`] automatically selects the best memory storage for the given `len`, this
    /// function enforces the usage of a memory map, the slowest of the options.
    ///
    /// [`new`]: Self::new
    #[cfg(ipc)]
    pub async fn new_memmap(len: usize) -> io::Result<Self> {
        IpcBytesMut::new_memmap(item_len_to_bytes::<T>(len)?).await.map(IpcBytesMut::cast)
    }

    /// Allocate zeroed mutable memory in a memory map.
    ///
    /// Note that [`new_blocking`] automatically selects the best memory storage for the given `len`, this
    /// function enforces the usage of a memory map, the slowest of the options.
    ///
    /// [`new_blocking`]: Self::new_blocking
    #[cfg(ipc)]
    pub fn new_memmap_blocking(len: usize) -> io::Result<Self> {
        IpcBytesMut::new_memmap_blocking(item_len_to_bytes::<T>(len)?).map(IpcBytesMut::cast)
    }

    /// Uses `buf` or copies it to exclusive mutable memory.
    pub async fn from_vec(data: Vec<T>) -> io::Result<Self> {
        IpcBytesMut::from_vec(bytemuck::cast_vec(data)).await.map(IpcBytesMut::cast)
    }

    /// Uses `buf` or copies it to exclusive mutable memory.
    pub fn from_vec_blocking(data: Vec<T>) -> io::Result<Self> {
        IpcBytesMut::from_vec_blocking(bytemuck::cast_vec(data)).map(IpcBytesMut::cast)
    }

    /// Copy data from slice.
    pub fn from_slice_blocking(data: &[T]) -> io::Result<Self> {
        IpcBytesMut::from_slice_blocking(bytemuck::cast_slice(data)).map(IpcBytesMut::cast)
    }

    /// Reference the underlying raw bytes.
    pub fn as_bytes(&mut self) -> &mut IpcBytesMut {
        &mut self.bytes
    }
}
impl<T: bytemuck::AnyBitPattern + bytemuck::NoUninit> IpcBytesMutCast<T> {
    /// Convert to immutable shareable [`IpcBytesCast`].
    pub async fn finish(self) -> io::Result<IpcBytesCast<T>> {
        self.bytes.finish().await.map(IpcBytes::cast)
    }

    /// Convert to immutable shareable [`IpcBytesCast`].
    pub fn finish_blocking(self) -> io::Result<IpcBytesCast<T>> {
        self.bytes.finish_blocking().map(IpcBytes::cast)
    }
}

impl IpcBytesMut {
    /// Safe bytemuck casting wrapper.
    ///
    /// The wrapper will deref to `[T]` and can be converted back to `IpcBytesMust`.
    ///
    /// # Panics
    ///
    /// Panics if cannot cast, se [bytemuck docs] for details.
    ///
    /// [bytemuck docs]: https://docs.rs/bytemuck/1.24.0/bytemuck/fn.try_cast_slice.html
    pub fn cast<T: bytemuck::AnyBitPattern>(self) -> IpcBytesMutCast<T> {
        let r = IpcBytesMutCast {
            bytes: self,
            _t: PhantomData,
        };
        let _assert = &r[..];
        r
    }

    /// Safe bytemuck cast to slice.
    ///
    /// # Panics
    ///
    /// Panics if cannot cast, se [bytemuck docs] for details.
    ///
    /// [bytemuck docs]: https://docs.rs/bytemuck/1.24.0/bytemuck/fn.try_cast_slice.html
    pub fn cast_deref<T: bytemuck::AnyBitPattern>(&self) -> &[T] {
        bytemuck::cast_slice(self)
    }

    /// Safe bytemuck cast to mutable slice.
    ///
    /// # Panics
    ///
    /// Panics if cannot cast, se [bytemuck docs] for details.
    ///
    /// [bytemuck docs]: https://docs.rs/bytemuck/1.24.0/bytemuck/fn.try_cast_slice.html
    pub fn cast_deref_mut<T: bytemuck::AnyBitPattern + bytemuck::NoUninit>(&mut self) -> &mut [T] {
        bytemuck::cast_slice_mut(self)
    }
}

/// Safe bytemuck casting wrapper for [`IpcBytes`].
///
/// Use [`IpcBytes::cast`] to cast.
pub struct IpcBytesCast<T: bytemuck::AnyBitPattern> {
    bytes: IpcBytes,
    _t: PhantomData<T>,
}
impl<T: bytemuck::AnyBitPattern> Default for IpcBytesCast<T> {
    fn default() -> Self {
        Self {
            bytes: Default::default(),
            _t: PhantomData,
        }
    }
}
impl<T: bytemuck::AnyBitPattern> ops::Deref for IpcBytesCast<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        bytemuck::cast_slice::<u8, T>(&self.bytes)
    }
}
impl<T: bytemuck::AnyBitPattern> IpcBytesCast<T> {
    /// Convert back to [`IpcBytes`].
    pub fn into_inner(self) -> IpcBytes {
        self.bytes
    }
}
impl<T: bytemuck::AnyBitPattern> From<IpcBytesCast<T>> for IpcBytes {
    fn from(value: IpcBytesCast<T>) -> Self {
        value.bytes
    }
}
impl<T: bytemuck::AnyBitPattern> Clone for IpcBytesCast<T> {
    fn clone(&self) -> Self {
        Self {
            bytes: self.bytes.clone(),
            _t: PhantomData,
        }
    }
}
impl<T: bytemuck::AnyBitPattern> fmt::Debug for IpcBytesCast<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "IpcBytesCast<{}>(<{} items>)", std::any::type_name::<T>(), self.len())
    }
}
impl<T: bytemuck::AnyBitPattern> serde::Serialize for IpcBytesCast<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.bytes.serialize(serializer)
    }
}
impl<'de, T: bytemuck::AnyBitPattern> serde::Deserialize<'de> for IpcBytesCast<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let bytes = IpcBytes::deserialize(deserializer)?;
        Ok(bytes.cast())
    }
}
impl<T: bytemuck::AnyBitPattern> PartialEq for IpcBytesCast<T> {
    fn eq(&self, other: &Self) -> bool {
        self.bytes == other.bytes
    }
}
impl<T: bytemuck::AnyBitPattern> Eq for IpcBytesCast<T> {}
impl<T: bytemuck::AnyBitPattern + bytemuck::NoUninit> IpcBytesCast<T> {
    /// Copy or move data from vector.
    pub async fn from_vec(data: Vec<T>) -> io::Result<Self> {
        IpcBytes::from_vec(bytemuck::cast_vec(data)).await.map(IpcBytes::cast)
    }

    /// Copy data from the iterator.
    ///
    /// This is most efficient if the [`size_hint`] indicates an exact length (min equals max), otherwise this
    /// will collect to an [`IpcBytesWriter`] that can reallocate multiple times as the buffer grows.
    ///
    /// Note that if the iterator gives an exact length that is the maximum taken, if it ends early the smaller length
    /// is used, if it continues after the given maximum it is clipped.
    ///
    /// [`size_hint`]: Iterator::size_hint
    /// [`IpcBytesWriter`]: crate::channel::IpcBytesWriter
    pub async fn from_iter(iter: impl Iterator<Item = T>) -> io::Result<Self> {
        #[cfg(ipc)]
        {
            let (min, max) = iter.size_hint();
            let l = size_of::<T>();
            let min = min * l;
            let max = max.map(|m| m * l);
            if let Some(max) = max {
                if max <= IpcBytes::INLINE_MAX {
                    return Self::from_vec(iter.collect()).await;
                } else if max == min {
                    let mut r = IpcBytesMut::new(max).await?;
                    let mut actual_len = 0;
                    for (i, f) in r.chunks_exact_mut(l).zip(iter) {
                        i.copy_from_slice(bytemuck::bytes_of(&f));
                        actual_len += 1;
                    }
                    r.truncate(actual_len * l);
                    return r.finish().await.map(IpcBytes::cast);
                }
            }

            let mut writer = IpcBytes::new_writer().await;
            for f in iter {
                use futures_lite::AsyncWriteExt as _;

                writer.write_all(bytemuck::bytes_of(&f)).await?;
            }
            writer.finish().await.map(IpcBytes::cast)
        }
        #[cfg(not(ipc))]
        {
            Self::from_vec(iter.collect()).await
        }
    }

    /// Copy or move data from vector.
    pub fn from_vec_blocking(data: Vec<T>) -> io::Result<Self> {
        IpcBytes::from_vec_blocking(bytemuck::cast_vec(data)).map(IpcBytes::cast)
    }

    /// Copy data from slice.
    pub fn from_slice_blocking(data: &[T]) -> io::Result<Self> {
        IpcBytes::from_slice_blocking(bytemuck::cast_slice(data)).map(IpcBytes::cast)
    }

    /// Copy data from the iterator.
    ///
    /// This is most efficient if the [`size_hint`] indicates an exact length (min equals max), otherwise this
    /// will collect to an [`IpcBytesWriterBlocking`] that can reallocate multiple times as the buffer grows.
    ///
    /// Note that if the iterator gives an exact length that is the maximum taken, if it ends early the smaller length
    /// is used, if it continues after the given maximum it is clipped.
    ///
    /// [`size_hint`]: Iterator::size_hint
    /// [`IpcBytesWriterBlocking`]: crate::channel::IpcBytesWriterBlocking
    pub fn from_iter_blocking(mut iter: impl Iterator<Item = T>) -> io::Result<Self> {
        #[cfg(ipc)]
        {
            let (min, max) = iter.size_hint();
            let l = size_of::<T>();
            let min = min * l;
            let max = max.map(|m| m * l);
            if let Some(max) = max {
                if max <= IpcBytes::INLINE_MAX {
                    return Self::from_vec_blocking(iter.collect());
                } else if max == min {
                    let mut r = IpcBytesMut::new_blocking(max)?;
                    let mut actual_len = 0;
                    for (i, f) in r.chunks_exact_mut(l).zip(&mut iter) {
                        i.copy_from_slice(bytemuck::bytes_of(&f));
                        actual_len += 1;
                    }
                    r.truncate(actual_len * l);
                    return r.finish_blocking().map(IpcBytes::cast);
                }
            }

            let mut writer = IpcBytes::new_writer_blocking();
            for f in iter {
                use std::io::Write as _;

                writer.write_all(bytemuck::bytes_of(&f))?;
            }
            writer.finish().map(IpcBytes::cast)
        }
        #[cfg(not(ipc))]
        {
            Self::from_vec_blocking(iter.collect())
        }
    }

    /// Reference the underlying raw bytes.
    pub fn as_bytes(&self) -> &IpcBytes {
        &self.bytes
    }
}

impl IpcBytes {
    /// Safe bytemuck casting wrapper.
    ///
    /// The wrapper will deref to `[T]` and can be converted back to `IpcBytes`.
    ///
    /// # Panics
    ///
    /// Panics if cannot cast, se [bytemuck docs] for details.
    ///
    /// [bytemuck docs]: https://docs.rs/bytemuck/1.24.0/bytemuck/fn.try_cast_slice.html
    pub fn cast<T: bytemuck::AnyBitPattern>(self) -> IpcBytesCast<T> {
        let r = IpcBytesCast {
            bytes: self,
            _t: PhantomData,
        };
        let _assert = &r[..];
        r
    }

    /// Safe bytemuck cast to slice.
    ///
    /// # Panics
    ///
    /// Panics if cannot cast, se [bytemuck docs] for details.
    ///
    /// [bytemuck docs]: https://docs.rs/bytemuck/1.24.0/bytemuck/fn.try_cast_slice.html
    pub fn cast_deref<T: bytemuck::AnyBitPattern>(&self) -> &[T] {
        bytemuck::cast_slice(self)
    }
}

/// An [`IpcBytesCast`] iterator that holds a strong reference to it.
pub struct IpcBytesCastIntoIter<T: bytemuck::AnyBitPattern>(IpcBytesIntoIter, IpcBytesCast<T>);
impl<T: bytemuck::AnyBitPattern> IpcBytesCastIntoIter<T> {
    fn new(bytes: IpcBytesCast<T>) -> Self {
        Self(bytes.bytes.clone().into_iter(), bytes)
    }

    /// The source bytes.
    pub fn source(&self) -> &IpcBytesCast<T> {
        &self.1
    }

    /// Items not yet iterated.
    pub fn rest(&self) -> &[T] {
        bytemuck::cast_slice(self.0.rest())
    }
}
impl<T: bytemuck::AnyBitPattern> Iterator for IpcBytesCastIntoIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        let size = size_of::<T>();
        let r = *bytemuck::from_bytes(self.0.rest().get(..size)?);
        self.0.nth(size - 1);
        Some(r)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (mut min, mut max) = self.0.size_hint();
        min /= size_of::<T>();
        if let Some(max) = &mut max {
            *max /= size_of::<T>();
        }
        (min, max)
    }

    fn nth(&mut self, n: usize) -> Option<T> {
        let size = size_of::<T>();

        let byte_skip = n.checked_mul(size)?;
        let byte_end = byte_skip.checked_add(size)?;

        let bytes = self.0.rest().get(byte_skip..byte_end)?;
        let r = *bytemuck::from_bytes(bytes);

        self.0.nth(byte_end - 1);

        Some(r)
    }

    fn last(mut self) -> Option<Self::Item>
    where
        Self: Sized,
    {
        self.next_back()
    }
}
impl<T: bytemuck::AnyBitPattern> DoubleEndedIterator for IpcBytesCastIntoIter<T> {
    fn next_back(&mut self) -> Option<T> {
        let size = size_of::<T>();

        let len = self.0.rest().len();
        if len < size {
            return None;
        }

        let start = len - size;
        let bytes = &self.0.rest()[start..];
        let r = *bytemuck::from_bytes(bytes);

        self.0.nth_back(size - 1);

        Some(r)
    }

    fn nth_back(&mut self, n: usize) -> Option<T> {
        let size = size_of::<T>();

        let rev_byte_skip = n.checked_mul(size)?;
        let rev_byte_end = rev_byte_skip.checked_add(size)?;
        let len = self.0.rest().len();

        if len < rev_byte_end {
            return None;
        }

        let start = len - rev_byte_end;
        let end = len - rev_byte_skip;

        let bytes = &self.0.rest()[start..end];
        let r = *bytemuck::from_bytes(bytes);

        self.0.nth_back(rev_byte_end - 1);

        Some(r)
    }
}
impl<T: bytemuck::AnyBitPattern> FusedIterator for IpcBytesCastIntoIter<T> {}
impl<T: bytemuck::AnyBitPattern> Default for IpcBytesCastIntoIter<T> {
    fn default() -> Self {
        IpcBytes::empty().cast::<T>().into_iter()
    }
}
impl<T: bytemuck::AnyBitPattern> IntoIterator for IpcBytesCast<T> {
    type Item = T;

    type IntoIter = IpcBytesCastIntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        IpcBytesCastIntoIter::new(self)
    }
}