dynsequence 0.1.0-alpha.4

Sequence-Like data-structure for storing dynamically sized types
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#![warn(
    // missing_docs,
    // rustdoc::missing_doc_code_examples,
    future_incompatible,
    rust_2018_idioms,
    unused,
    trivial_casts,
    trivial_numeric_casts,
    unused_lifetimes,
    unused_qualifications,
    unused_crate_dependencies,
    clippy::cargo,
    clippy::multiple_crate_versions,
    clippy::empty_line_after_outer_attr,
    clippy::fallible_impl_from,
    clippy::redundant_pub_crate,
    clippy::use_self,
    clippy::suspicious_operation_groupings,
    clippy::useless_let_if_seq,
    // clippy::missing_errors_doc,
    // clippy::missing_panics_doc,
    clippy::wildcard_imports
)]
#![doc(html_no_source)]
#![no_std]
#![doc = include_str!("../README.md")]
#![cfg_attr(feature = "unstable", feature(unsize))]

extern crate alloc;

use alloc::vec::Vec;
use core::{
    iter::FusedIterator,
    mem,
    ops::{Deref, DerefMut, Index, IndexMut},
    ptr,
};

#[cfg(feature = "unstable")]
use core::marker::Unsize;

struct DynBlocks {
    block_size: usize,
    max_block_size: usize,
    blocks: Vec<(*mut u8, usize, usize)>,
    next_block_offset: usize,
}

impl DynBlocks {
    pub const fn new() -> Self {
        Self::with_blocksize(128)
    }

    pub const fn with_blocksize(block_size: usize) -> Self {
        const MAX_BLOCK_SIZE: usize = 2048;
        Self {
            block_size,
            max_block_size: if block_size < MAX_BLOCK_SIZE {
                MAX_BLOCK_SIZE
            } else {
                block_size
            },
            blocks: Vec::new(),
            next_block_offset: 0,
        }
    }

    fn next_ptr(&mut self, size: usize, align: usize) -> *mut u8 {
        if let Some((ptr, len, _)) = self.blocks.last_mut().copied() {
            let ptr = unsafe { ptr.add(self.next_block_offset) };
            let align_offset = ptr.align_offset(align);
            if self.next_block_offset + align_offset + size <= len {
                self.next_block_offset += align_offset + size;
                return ptr;
            }
        }
        // allocate a new block
        if size > self.max_block_size {
            // use exact size block
            unsafe {
                let layout = alloc::alloc::Layout::from_size_align_unchecked(size, align);
                let ptr_exact = alloc::alloc::alloc_zeroed(layout);
                self.next_block_offset = size;
                self.blocks.push((ptr_exact, size, align));
                return ptr_exact;
            }
        }
        // use power-of-2 blocksizes (increment)
        let block_size = size.max(self.block_size).next_power_of_two();
        if block_size * 2 <= self.max_block_size {
            self.block_size = block_size * 2;
        }
        unsafe {
            let layout = alloc::alloc::Layout::from_size_align_unchecked(block_size, align);
            let ptr = alloc::alloc::alloc_zeroed(layout);
            self.next_block_offset = size;
            self.blocks.push((ptr, block_size, align));
            ptr
        }
    }

    /// # Safety
    ///
    /// Behavior is undefined if any of the following conditions are violated:
    ///
    /// * `src` must be valid for reads.
    /// * `src` must be properly aligned.
    /// * `src` must point to a properly initialized value of type `T`.
    /// * `src` must not be used or dropped anymore (use std::mem::forget)
    pub unsafe fn push_raw<T: ?Sized>(&mut self, src: *const T) -> DynBlockRef<'_, T> {
        let size = mem::size_of_val::<T>(&*src);
        let align = mem::align_of_val::<T>(&*src);
        let dst = self.next_ptr(size, align);
        ptr::copy_nonoverlapping(src as *const u8, dst, size);

        let mut r = src as *mut T;
        ptr::write(ptr::addr_of_mut!(r), src as *mut T);
        ptr::write(ptr::addr_of_mut!(r) as *mut *mut u8, dst); // replace address

        DynBlockRef(&mut *r)
    }

    // #[inline]
    // pub fn push<T>(&mut self, value: T) -> DynBlockRef<'_, T> {
    //     unsafe {
    //         let r = self.push_raw(&value);
    //         mem::forget(value);
    //         r
    //     }
    // }

    /// # Safety
    ///
    /// all values stored on the blocks must be dropped first (drop_in_place)
    pub unsafe fn clear(&mut self) {
        for (ptr, size, align) in self.blocks.drain(..) {
            let layout = alloc::alloc::Layout::from_size_align_unchecked(size, align);
            unsafe { alloc::alloc::dealloc(ptr, layout) }
        }
    }

    pub fn extend_dynblocks(&mut self, mut other: Self) {
        if other.blocks.is_empty() {
            return;
        }
        self.blocks.append(&mut other.blocks);
        self.next_block_offset = other.next_block_offset;
        if other.block_size > self.block_size {
            self.block_size = other.block_size;
        }
    }
}

impl Default for DynBlocks {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

struct DynBlockRef<'l, T: ?Sized>(&'l mut T);

impl<'l, T: ?Sized> DynBlockRef<'l, T> {
    pub fn into_mut(self) -> &'l mut T {
        let r: *mut T = self.0;
        unsafe {
            mem::forget(self);
            &mut *r
        }
    }

    pub fn as_mut_ptr(&mut self) -> *mut T {
        self.0
    }
}

impl<'l, T: ?Sized> Deref for DynBlockRef<'l, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl<'l, T: ?Sized> DerefMut for DynBlockRef<'l, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0
    }
}

impl<'l, T: ?Sized> Drop for DynBlockRef<'l, T> {
    fn drop(&mut self) {
        unsafe { ptr::drop_in_place(self.0) }
    }
}

pub struct DynSequence<T: ?Sized> {
    ptrs: Vec<*mut T>,
    blocks: DynBlocks,
}

impl<T: ?Sized> DynSequence<T> {
    pub const fn new() -> Self {
        Self::with_blocksize(128)
    }

    pub const fn with_blocksize(block_size: usize) -> Self {
        Self {
            ptrs: Vec::new(),
            blocks: DynBlocks::with_blocksize(block_size),
        }
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.ptrs.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.ptrs.is_empty()
    }

    pub fn clear(&mut self) {
        if mem::needs_drop::<T>() {
            for p in self.ptrs.drain(..) {
                unsafe {
                    ptr::drop_in_place(p);
                }
            }
        }
        // # SAFETY:
        // all values are dropped
        unsafe {
            self.blocks.clear();
        }
    }

    /// Inserts a new value into the DynSequence at the given index (see Vec::insert)
    /// by moving the value behind the pointer and taking ownership.
    ///
    /// # Safety
    ///
    /// Behavior is undefined if any of the following conditions are violated:
    ///
    /// * `src` must be valid for reads.
    /// * `src` must be properly aligned.
    /// * `src` must point to a properly initialized value of type `T`.
    /// * `src` must not be used or dropped anymore (use std::mem::forget)
    pub unsafe fn insert_raw(&mut self, index: usize, src: *const T) -> &mut T {
        let mut r = self.blocks.push_raw(src);
        self.ptrs.insert(index, r.as_mut_ptr());
        r.into_mut()
    }

    /// Adds a new value at the end of the DynSequence (see Vec::push)
    /// by moving the value behind the pointer and taking ownership.
    ///
    /// # Safety
    ///
    /// Behavior is undefined if any of the following conditions are violated:
    ///
    /// * `src` must be valid for reads.
    /// * `src` must be properly aligned.
    /// * `src` must point to a properly initialized value of type `T`.
    /// * `src` must not be used or dropped anymore (use std::mem::forget)
    pub unsafe fn push_raw(&mut self, src: *const T) -> &mut T {
        let mut r = self.blocks.push_raw(src);
        self.ptrs.push(r.as_mut_ptr());
        r.into_mut()
    }

    #[cfg(feature = "unstable")]
    #[inline]
    pub fn insert<U>(&mut self, index: usize, src: U) -> &mut T
    where
        U: Unsize<T>,
    {
        unsafe {
            let r = self.insert_raw(index, &src);
            mem::forget(src);
            r
        }
    }

    #[cfg(feature = "unstable")]
    #[inline]
    pub fn push<U>(&mut self, src: U) -> &mut T
    where
        U: Unsize<T>,
    {
        unsafe {
            let r = self.push_raw(&src);
            mem::forget(src);
            r
        }
    }

    #[inline]
    pub fn get(&self, index: usize) -> Option<&T> {
        let p: *mut T = *self.ptrs.get(index)?;
        unsafe { Some(&*p) }
    }

    #[inline]
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        let p: *mut T = *self.ptrs.get_mut(index)?;
        unsafe { Some(&mut *p) }
    }

    #[inline]
    pub fn as_slice(&self) -> &[&T] {
        unsafe { mem::transmute(self.ptrs.as_slice()) }
    }

    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [&mut T] {
        unsafe { mem::transmute(self.ptrs.as_mut_slice()) }
    }

    #[inline]
    pub fn as_mut_const_slice(&mut self) -> &mut [&T] {
        unsafe { mem::transmute(self.ptrs.as_mut_slice()) }
    }

    #[inline]
    pub fn iter(&self) -> DynSeqIter<'_, T> {
        self.as_slice().iter().copied()
    }

    #[inline]
    pub fn iter_mut(&mut self) -> DynSeqIterMut<'_, T> {
        DynSeqIterMut(self.as_mut_slice().iter_mut())
    }

    pub fn extend_dynsequence(&mut self, mut other: Self) {
        if other.is_empty() {
            return;
        }
        self.blocks.extend_dynblocks(mem::take(&mut other.blocks));
        self.ptrs.extend(mem::take(&mut other.ptrs))
    }
}

impl<T: ?Sized> Index<usize> for DynSequence<T> {
    type Output = T;
    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        self.get(index).expect("index out of bounds")
    }
}

impl<T: ?Sized> IndexMut<usize> for DynSequence<T> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        self.get_mut(index).expect("index out of bounds")
    }
}

pub type DynSeqIter<'l, T> = core::iter::Copied<core::slice::Iter<'l, &'l T>>;
pub struct DynSeqIterMut<'l, T: ?Sized>(core::slice::IterMut<'l, &'l mut T>);

impl<'l, T: ?Sized> Iterator for DynSeqIterMut<'l, T> {
    type Item = &'l mut T;
    #[inline]
    fn next(&mut self) -> Option<&'l mut T> {
        Some(self.0.next()?)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }

    fn nth(&mut self, n: usize) -> Option<&'l mut T> {
        Some(self.0.nth(n)?)
    }

    fn last(self) -> Option<&'l mut T> {
        Some(self.0.last()?)
    }

    fn count(self) -> usize {
        self.0.count()
    }
}

impl<'l, T: ?Sized> DoubleEndedIterator for DynSeqIterMut<'l, T> {
    #[inline]
    fn next_back(&mut self) -> Option<&'l mut T> {
        Some(self.0.next_back()?)
    }
}

impl<'l, T: ?Sized> ExactSizeIterator for DynSeqIterMut<'l, T> {
    #[inline]
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl<'l, T: ?Sized> FusedIterator for DynSeqIterMut<'l, T> {}

impl<'l, T: ?Sized> IntoIterator for &'l DynSequence<T> {
    type Item = &'l T;
    type IntoIter = DynSeqIter<'l, T>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'l, T: ?Sized> IntoIterator for &'l mut DynSequence<T> {
    type Item = &'l mut T;
    type IntoIter = DynSeqIterMut<'l, T>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T: ?Sized> Drop for DynSequence<T> {
    fn drop(&mut self) {
        self.clear();
    }
}

impl<T: ?Sized> Default for DynSequence<T> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

unsafe impl<T: ?Sized> Send for DynSequence<T> where T: Send {}
unsafe impl<T: ?Sized> Sync for DynSequence<T> where T: Sync {}

#[doc(hidden)]
pub use ::core::mem::forget as __forget;
#[doc(hidden)]
pub mod __macro_raw_fn {
    #[inline(always)]
    pub unsafe fn push<T: ?Sized>(dst: &mut crate::DynSequence<T>, raw: *mut T) {
        dst.push_raw(raw);
    }
    #[inline(always)]
    pub unsafe fn insert<T: ?Sized>(dst: &mut crate::DynSequence<T>, index: usize, raw: *mut T) {
        dst.insert_raw(index, raw);
    }
}

#[macro_export]
macro_rules! dyn_sequence {
    [$t:ty | $r:expr => { $(
        $m:ident ($e:expr ) $(@ $i:expr)?;
    )* } ] => {
        {
            let r: &mut $crate::DynSequence<$t> = $r;
            $(
                #[allow(unsafe_code, clippy::forget_copy, clippy::forget_ref)]
                unsafe {
                    let mut v = $e;
                    $crate::__macro_raw_fn::$m(r, $($i,)? &mut v);
                    $crate::__forget(v);
                }
            )*
        }
    };
    [$t:ty => $($e:expr),*] => {
        {
            let mut r: $crate::DynSequence::<$t> = $crate::DynSequence::new();
            $(
                #[allow(unsafe_code, clippy::forget_copy, clippy::forget_ref)]
                unsafe {
                    let mut v = $e;
                    $crate::__macro_raw_fn::push(&mut r, &mut v);
                    $crate::__forget(v);
                }
            )*
            r
        }
    };
}

#[cfg(test)]
mod tests {
    use core::any::Any;

    use crate::DynSequence;

    #[test]
    fn test_ctor_macro() {
        let seq: DynSequence<dyn Any> = dyn_sequence![dyn Any =>
            123u8,
            true,
            456u16,
            "Hallo Welt!"
        ];

        assert_eq!(
            Some(&123u8),
            seq.get(0).and_then(|a| a.downcast_ref::<u8>())
        );
        assert_eq!(None, seq.get(0).and_then(|a| a.downcast_ref::<u16>()));
        assert_eq!(
            Some(&true),
            seq.get(1).and_then(|a| a.downcast_ref::<bool>())
        );
        assert_eq!(
            Some(&456u16),
            seq.get(2).and_then(|a| a.downcast_ref::<u16>())
        );
        assert_eq!(
            Some(&"Hallo Welt!"),
            seq.get(3).and_then(|a| a.downcast_ref::<&str>())
        );
        assert!(seq.get(4).is_none());
    }

    #[test]
    fn test_placement_macro() {
        let mut seq: DynSequence<dyn Any> = DynSequence::new();
        dyn_sequence![dyn Any | &mut seq => {
            push(123u8);
            push(456u16);
            insert(true) @ 1;
            push("Hallo Welt!");
        }];

        assert_eq!(
            Some(&123u8),
            seq.get(0).and_then(|a| a.downcast_ref::<u8>())
        );
        assert_eq!(None, seq.get(0).and_then(|a| a.downcast_ref::<u16>()));
        assert_eq!(
            Some(&true),
            seq.get(1).and_then(|a| a.downcast_ref::<bool>())
        );
        assert_eq!(
            Some(&456u16),
            seq.get(2).and_then(|a| a.downcast_ref::<u16>())
        );
        assert_eq!(
            Some(&"Hallo Welt!"),
            seq.get(3).and_then(|a| a.downcast_ref::<&str>())
        );
        assert!(seq.get(4).is_none());
    }

    #[cfg(feature = "unstable")]
    #[test]
    fn test_push_insert_unstable() {
        let mut seq: DynSequence<dyn Any> = DynSequence::new();
        seq.push(123u8);
        seq.push(456u16);
        seq.insert(1, true);
        seq.push("Hallo Welt!");

        assert_eq!(
            Some(&123u8),
            seq.get(0).and_then(|a| a.downcast_ref::<u8>())
        );
        assert_eq!(None, seq.get(0).and_then(|a| a.downcast_ref::<u16>()));
        assert_eq!(
            Some(&true),
            seq.get(1).and_then(|a| a.downcast_ref::<bool>())
        );
        assert_eq!(
            Some(&456u16),
            seq.get(2).and_then(|a| a.downcast_ref::<u16>())
        );
        assert_eq!(
            Some(&"Hallo Welt!"),
            seq.get(3).and_then(|a| a.downcast_ref::<&str>())
        );
        assert!(seq.get(4).is_none());
    }
}