Skip to content

Commit 616a9e6

Browse files
committed
Change to View instead of Cow
1 parent 117182c commit 616a9e6

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/arraylike.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::{
66
aview_mut1,
77
ArrayBase,
88
ArrayRef,
9+
ArrayView,
910
ArrayViewMut,
10-
CowArray,
1111
Data,
1212
DataMut,
1313
Dimension,
@@ -51,7 +51,7 @@ pub trait ArrayLike
5151
type Dim: Dimension;
5252
type Elem;
5353

54-
fn as_array(&self) -> CowArray<'_, Self::Elem, Self::Dim>;
54+
fn view(&self) -> ArrayView<'_, Self::Elem, Self::Dim>;
5555

5656
fn dim(&self) -> Self::Dim;
5757

@@ -60,7 +60,7 @@ pub trait ArrayLike
6060

6161
pub trait ArrayLikeMut: ArrayLike
6262
{
63-
fn as_array_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>;
63+
fn view_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>;
6464

6565
fn as_elem_mut(&mut self) -> Option<&mut Self::Elem>;
6666
}
@@ -71,10 +71,10 @@ where A: ScalarOperand
7171
type Dim = Ix0;
7272
type Elem = A;
7373

74-
fn as_array(&self) -> CowArray<'_, Self::Elem, Self::Dim>
74+
fn view(&self) -> ArrayView<'_, Self::Elem, Self::Dim>
7575
where Self::Elem: Clone
7676
{
77-
aview0(self).into()
77+
aview0(self)
7878
}
7979

8080
fn dim(&self) -> Self::Dim
@@ -91,7 +91,7 @@ where A: ScalarOperand
9191
impl<A> ArrayLikeMut for A
9292
where A: ScalarOperand
9393
{
94-
fn as_array_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
94+
fn view_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
9595
{
9696
// SAFETY: The pointer will be non-null since it's a reference,
9797
// and the view is tied to the lifetime of the mutable borrow
@@ -110,9 +110,9 @@ where D: Dimension
110110
type Dim = D;
111111
type Elem = A;
112112

113-
fn as_array(&self) -> CowArray<'_, Self::Elem, Self::Dim>
113+
fn view(&self) -> ArrayView<'_, Self::Elem, Self::Dim>
114114
{
115-
self.view().into()
115+
self.view()
116116
}
117117

118118
fn dim(&self) -> Self::Dim
@@ -133,7 +133,7 @@ where D: Dimension
133133
impl<A, D> ArrayLikeMut for ArrayRef<A, D>
134134
where D: Dimension
135135
{
136-
fn as_array_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
136+
fn view_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
137137
{
138138
self.view_mut()
139139
}
@@ -156,9 +156,9 @@ where
156156
type Dim = D;
157157
type Elem = S::Elem;
158158

159-
fn as_array(&self) -> CowArray<'_, Self::Elem, Self::Dim>
159+
fn view(&self) -> ArrayView<'_, Self::Elem, Self::Dim>
160160
{
161-
self.into()
161+
self.view()
162162
}
163163

164164
fn dim(&self) -> Self::Dim
@@ -181,7 +181,7 @@ where
181181
S: DataMut,
182182
D: Dimension,
183183
{
184-
fn as_array_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
184+
fn view_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
185185
{
186186
self.view_mut()
187187
}
@@ -202,9 +202,9 @@ impl<A> ArrayLike for [A]
202202

203203
type Elem = A;
204204

205-
fn as_array(&self) -> CowArray<'_, Self::Elem, Self::Dim>
205+
fn view(&self) -> ArrayView<'_, Self::Elem, Self::Dim>
206206
{
207-
aview1(self).into()
207+
aview1(self)
208208
}
209209

210210
fn dim(&self) -> Self::Dim
@@ -224,7 +224,7 @@ impl<A> ArrayLike for [A]
224224

225225
impl<A> ArrayLikeMut for [A]
226226
{
227-
fn as_array_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
227+
fn view_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
228228
{
229229
aview_mut1(self)
230230
}
@@ -245,9 +245,9 @@ impl<A> ArrayLike for Vec<A>
245245

246246
type Elem = A;
247247

248-
fn as_array(&self) -> CowArray<'_, Self::Elem, Self::Dim>
248+
fn view(&self) -> ArrayView<'_, Self::Elem, Self::Dim>
249249
{
250-
(&**self).as_array()
250+
(&**self).view()
251251
}
252252

253253
fn dim(&self) -> Self::Dim
@@ -263,9 +263,9 @@ impl<A> ArrayLike for Vec<A>
263263

264264
impl<A> ArrayLikeMut for Vec<A>
265265
{
266-
fn as_array_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
266+
fn view_mut(&mut self) -> ArrayViewMut<'_, Self::Elem, Self::Dim>
267267
{
268-
(&mut **self).as_array_mut()
268+
(&mut **self).view_mut()
269269
}
270270

271271
fn as_elem_mut(&mut self) -> Option<&mut Self::Elem>
@@ -282,16 +282,17 @@ mod tests
282282

283283
use crate::{array, Array, ArrayLike, DimMax};
284284

285-
fn multiply<T, G>(left: &T, right: &G) -> Array<T::Elem, <T::Dim as DimMax<G::Dim>>::Output>
285+
fn multiply<T, G>(left: &T, right: &G) -> Array<T::Elem::Output, <T::Dim as DimMax<G::Dim>>::Output>
286286
where
287287
T: ArrayLike,
288-
G: ArrayLike<Elem = T::Elem>,
288+
G: ArrayLike,
289289
// Bounds to enable multiplication
290-
T::Elem: Clone + Mul<T::Elem, Output = T::Elem>,
290+
T::Elem: Clone + Mul<G::Elem>,
291+
G::Elem: Clone,
291292
T::Dim: DimMax<G::Dim>,
292293
{
293-
let left = &*left.as_array();
294-
let right = &*right.as_array();
294+
let left = &*left.view();
295+
let right = &*right.view();
295296
left * right
296297
}
297298

0 commit comments

Comments
 (0)