|
| 1 | +//! Owning array types. |
| 2 | +
|
| 3 | +use core::{fmt::Debug, marker::PhantomData}; |
| 4 | + |
| 5 | +use super::{ |
| 6 | + arrayref::{RawRefBase, RefBase}, |
| 7 | + backend::Backend, |
| 8 | +}; |
| 9 | + |
| 10 | +/// Base type for arrays with owning semantics. |
| 11 | +pub struct OwnedBase<L, B: Backend> |
| 12 | +{ |
| 13 | + pub(super) aref: RefBase<L, B::Ref>, |
| 14 | + pub(super) own: B::Owned, |
| 15 | +} |
| 16 | + |
| 17 | +/// Base type for array views. |
| 18 | +/// |
| 19 | +/// Views are like references; in fact, they're just wrappers for references. |
| 20 | +/// The difference is that a reference's layout must be identical to the array |
| 21 | +/// from which is has been derived; a view's layout may be different, representing |
| 22 | +/// a segment, strided, or otherwise incomplete look at its parent array. |
| 23 | +#[derive(Debug)] |
| 24 | +pub struct ViewBase<'a, L, R, E> |
| 25 | +{ |
| 26 | + aref: RefBase<L, R>, |
| 27 | + life: PhantomData<&'a E>, |
| 28 | +} |
| 29 | + |
| 30 | +/// Base type for array views with mutable data. |
| 31 | +/// |
| 32 | +/// All kinds of views can have their layout mutated. However, data mutation |
| 33 | +/// is tracked separately via two different types. |
| 34 | +#[derive(Debug)] |
| 35 | +pub struct ViewBaseMut<'a, L, R, E> |
| 36 | +{ |
| 37 | + aref: RefBase<L, R>, |
| 38 | + life: PhantomData<&'a mut E>, |
| 39 | +} |
| 40 | + |
| 41 | +/// Base type for array views without lifetimes. |
| 42 | +#[derive(Debug)] |
| 43 | +pub struct RawViewBase<L, R, E> |
| 44 | +{ |
| 45 | + rref: RawRefBase<L, R>, |
| 46 | + life: PhantomData<*const E>, |
| 47 | +} |
| 48 | + |
| 49 | +/// Base type for array views without lifetimes, but with mutable data. |
| 50 | +#[derive(Debug)] |
| 51 | +pub struct RawViewBaseMut<L, R, E> |
| 52 | +{ |
| 53 | + rref: RawRefBase<L, R>, |
| 54 | + life: PhantomData<*const E>, |
| 55 | +} |
| 56 | + |
| 57 | +/// A trait for arrays with mutable data that can be made unique. |
| 58 | +/// |
| 59 | +/// Essentially all monomorphizations of [`OwnedBase`], [`ViewBaseMut`], |
| 60 | +/// and [`RawViewBaseMut`] should implement Uniqueable; this applies even |
| 61 | +/// when the array type does not have any data sharing capabilities. |
| 62 | +/// |
| 63 | +/// There are already blanket implementations for `ViewBaseMut` and |
| 64 | +/// `RawViewBaseMut`; as a result, any creation of these types |
| 65 | +/// _must_ ensure that the underlying data is unique (a.k.a, unshared) |
| 66 | +/// before creating these mutable views. |
| 67 | +pub unsafe trait Uniqueable |
| 68 | +{ |
| 69 | + fn try_ensure_unique(&mut self); |
| 70 | + |
| 71 | + fn try_is_unique(&self) -> Option<bool>; |
| 72 | +} |
| 73 | + |
| 74 | +/// Trait implemented by all the non-reference array types. |
| 75 | +/// |
| 76 | +/// We'll probably want to split trait into multiple trait, for three reasons: |
| 77 | +/// 1. Adding a "Raw" and "Mut" variants will likely be necessary |
| 78 | +/// 2. We probably don't want a single trait specified by backend, but instead traits |
| 79 | +/// that are specified by reference type, owned type, and backend separately. |
| 80 | +/// This allows for blanket implementations that would otherwise be annoying. |
| 81 | +/// 3. We may want to add subtraits that break the behavior into logical pieces, |
| 82 | +/// instead of having a monolith. |
| 83 | +pub unsafe trait NdArray<B: Backend> |
| 84 | +{ |
| 85 | + fn as_ptr(&self) -> *mut B::Elem; |
| 86 | +} |
| 87 | + |
| 88 | +mod array_impls |
| 89 | +{ |
| 90 | + use core::fmt::Debug; |
| 91 | + use core::ops::{Deref, DerefMut}; |
| 92 | + |
| 93 | + use crate::core::{Backend, RawRefBase, RefBase}; |
| 94 | + |
| 95 | + use super::{OwnedBase, RawViewBase, RawViewBaseMut, Uniqueable, ViewBase, ViewBaseMut}; |
| 96 | + |
| 97 | + impl<L, B: Backend> Deref for OwnedBase<L, B> |
| 98 | + { |
| 99 | + type Target = RefBase<L, B::Ref>; |
| 100 | + |
| 101 | + fn deref(&self) -> &Self::Target |
| 102 | + { |
| 103 | + &self.aref |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + impl<L, B: Backend> DerefMut for OwnedBase<L, B> |
| 108 | + where Self: Uniqueable |
| 109 | + { |
| 110 | + fn deref_mut(&mut self) -> &mut Self::Target |
| 111 | + { |
| 112 | + self.try_ensure_unique(); |
| 113 | + &mut self.aref |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + impl<'a, L, R, E> Deref for ViewBase<'a, L, R, E> |
| 118 | + { |
| 119 | + type Target = RefBase<L, R>; |
| 120 | + |
| 121 | + fn deref(&self) -> &Self::Target |
| 122 | + { |
| 123 | + &self.aref |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + impl<'a, L, R, E> Deref for ViewBaseMut<'a, L, R, E> |
| 128 | + { |
| 129 | + type Target = RefBase<L, R>; |
| 130 | + |
| 131 | + fn deref(&self) -> &Self::Target |
| 132 | + { |
| 133 | + &self.aref |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + impl<'a, L, R, E> DerefMut for ViewBaseMut<'a, L, R, E> |
| 138 | + { |
| 139 | + fn deref_mut(&mut self) -> &mut Self::Target |
| 140 | + { |
| 141 | + &mut self.aref |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + impl<L, R, E> Deref for RawViewBase<L, R, E> |
| 146 | + { |
| 147 | + type Target = RawRefBase<L, R>; |
| 148 | + |
| 149 | + fn deref(&self) -> &Self::Target |
| 150 | + { |
| 151 | + &self.rref |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + impl<L, R, E> Deref for RawViewBaseMut<L, R, E> |
| 156 | + { |
| 157 | + type Target = RawRefBase<L, R>; |
| 158 | + |
| 159 | + fn deref(&self) -> &Self::Target |
| 160 | + { |
| 161 | + &self.rref |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + impl<L, R, E> DerefMut for RawViewBaseMut<L, R, E> |
| 166 | + { |
| 167 | + fn deref_mut(&mut self) -> &mut Self::Target |
| 168 | + { |
| 169 | + &mut self.rref |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + unsafe impl<'a, L, R, E> Uniqueable for ViewBaseMut<'a, L, R, E> |
| 174 | + { |
| 175 | + fn try_ensure_unique(&mut self) {} |
| 176 | + |
| 177 | + fn try_is_unique(&self) -> Option<bool> |
| 178 | + { |
| 179 | + Some(true) |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + unsafe impl<L, R, E> Uniqueable for RawViewBaseMut<L, R, E> |
| 184 | + { |
| 185 | + fn try_ensure_unique(&mut self) {} |
| 186 | + |
| 187 | + fn try_is_unique(&self) -> Option<bool> |
| 188 | + { |
| 189 | + None |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + impl<L, B> Debug for OwnedBase<L, B> |
| 194 | + where |
| 195 | + B: Backend + Debug, |
| 196 | + B::Ref: Debug, |
| 197 | + B::Owned: Debug, |
| 198 | + L: Debug, |
| 199 | + { |
| 200 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result |
| 201 | + { |
| 202 | + f.debug_struct("OwnedBase") |
| 203 | + .field("aref", &self.aref) |
| 204 | + .field("own", &self.own) |
| 205 | + .finish() |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments