|
| 1 | +// Copyright 2020 bluss and ndarray developers. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +use crate::imp_prelude::*; |
| 10 | +use crate::{ |
| 11 | + AssignElem, |
| 12 | + Layout, |
| 13 | + NdProducer, |
| 14 | + Zip, |
| 15 | + FoldWhile, |
| 16 | +}; |
| 17 | + |
| 18 | +use std::cell::Cell; |
| 19 | +use std::mem; |
| 20 | +use std::mem::MaybeUninit; |
| 21 | +use std::ptr; |
| 22 | + |
| 23 | +/// An assignable element reference that increments a counter when assigned |
| 24 | +pub(crate) struct ProxyElem<'a, 'b, A> { |
| 25 | + item: &'a mut MaybeUninit<A>, |
| 26 | + filled: &'b Cell<usize> |
| 27 | +} |
| 28 | + |
| 29 | +impl<'a, 'b, A> AssignElem<A> for ProxyElem<'a, 'b, A> { |
| 30 | + fn assign_elem(self, item: A) { |
| 31 | + self.filled.set(self.filled.get() + 1); |
| 32 | + *self.item = MaybeUninit::new(item); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Handles progress of assigning to a part of an array, for elements that need |
| 37 | +/// to be dropped on unwinding. See Self::scope. |
| 38 | +pub(crate) struct PartialArray<'a, 'b, A, D> |
| 39 | + where D: Dimension |
| 40 | +{ |
| 41 | + data: ArrayViewMut<'a, MaybeUninit<A>, D>, |
| 42 | + filled: &'b Cell<usize>, |
| 43 | +} |
| 44 | + |
| 45 | +impl<'a, 'b, A, D> PartialArray<'a, 'b, A, D> |
| 46 | + where D: Dimension |
| 47 | +{ |
| 48 | + /// Create a temporary PartialArray that wraps the array view `data`; |
| 49 | + /// if the end of the scope is reached, the partial array is marked complete; |
| 50 | + /// if execution unwinds at any time before them, the elements written until then |
| 51 | + /// are dropped. |
| 52 | + /// |
| 53 | + /// Safety: the caller *must* ensure that elements will be written in `data`'s preferred order. |
| 54 | + /// PartialArray can not handle arbitrary writes, only in the memory order. |
| 55 | + pub(crate) unsafe fn scope(data: ArrayViewMut<'a, MaybeUninit<A>, D>, |
| 56 | + scope_fn: impl FnOnce(&mut PartialArray<A, D>)) |
| 57 | + { |
| 58 | + let filled = Cell::new(0); |
| 59 | + let mut partial = PartialArray::new(data, &filled); |
| 60 | + scope_fn(&mut partial); |
| 61 | + filled.set(0); // mark complete |
| 62 | + } |
| 63 | + |
| 64 | + unsafe fn new(data: ArrayViewMut<'a, MaybeUninit<A>, D>, |
| 65 | + filled: &'b Cell<usize>) -> Self |
| 66 | + { |
| 67 | + debug_assert_eq!(filled.get(), 0); |
| 68 | + Self { data, filled } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl<'a, 'b, A, D> Drop for PartialArray<'a, 'b, A, D> |
| 73 | + where D: Dimension |
| 74 | +{ |
| 75 | + fn drop(&mut self) { |
| 76 | + if !mem::needs_drop::<A>() { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + let mut count = self.filled.get(); |
| 81 | + if count == 0 { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + Zip::from(self).fold_while((), move |(), elt| { |
| 86 | + if count > 0 { |
| 87 | + count -= 1; |
| 88 | + unsafe { |
| 89 | + ptr::drop_in_place::<A>(elt.item.as_mut_ptr()); |
| 90 | + } |
| 91 | + FoldWhile::Continue(()) |
| 92 | + } else { |
| 93 | + FoldWhile::Done(()) |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<'a: 'c, 'b: 'c, 'c, A, D: Dimension> NdProducer for &'c mut PartialArray<'a, 'b, A, D> { |
| 100 | + // This just wraps ArrayViewMut as NdProducer and maps the item |
| 101 | + type Item = ProxyElem<'a, 'b, A>; |
| 102 | + type Dim = D; |
| 103 | + type Ptr = *mut MaybeUninit<A>; |
| 104 | + type Stride = isize; |
| 105 | + |
| 106 | + private_impl! {} |
| 107 | + fn raw_dim(&self) -> Self::Dim { |
| 108 | + self.data.raw_dim() |
| 109 | + } |
| 110 | + |
| 111 | + fn equal_dim(&self, dim: &Self::Dim) -> bool { |
| 112 | + self.data.equal_dim(dim) |
| 113 | + } |
| 114 | + |
| 115 | + fn as_ptr(&self) -> Self::Ptr { |
| 116 | + NdProducer::as_ptr(&self.data) |
| 117 | + } |
| 118 | + |
| 119 | + fn layout(&self) -> Layout { |
| 120 | + self.data.layout() |
| 121 | + } |
| 122 | + |
| 123 | + unsafe fn as_ref(&self, ptr: Self::Ptr) -> Self::Item { |
| 124 | + ProxyElem { filled: self.filled, item: &mut *ptr } |
| 125 | + } |
| 126 | + |
| 127 | + unsafe fn uget_ptr(&self, i: &Self::Dim) -> Self::Ptr { |
| 128 | + self.data.uget_ptr(i) |
| 129 | + } |
| 130 | + |
| 131 | + fn stride_of(&self, axis: Axis) -> Self::Stride { |
| 132 | + self.data.stride_of(axis) |
| 133 | + } |
| 134 | + |
| 135 | + #[inline(always)] |
| 136 | + fn contiguous_stride(&self) -> Self::Stride { |
| 137 | + self.data.contiguous_stride() |
| 138 | + } |
| 139 | + |
| 140 | + fn split_at(self, _axis: Axis, _index: usize) -> (Self, Self) { |
| 141 | + unimplemented!(); |
| 142 | + } |
| 143 | +} |
| 144 | + |
0 commit comments