Skip to content

Commit b45b3a1

Browse files
committed
Adds Borrow and ToOwned
1 parent 3156346 commit b45b3a1

File tree

1 file changed

+65
-6
lines changed

1 file changed

+65
-6
lines changed

src/impl_ref_types.rs

+65-6
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@
2121
//! To mitigate these problems, `ndarray` also provides `AsRef` and `AsMut` implementations as follows:
2222
//! 1. `ArrayBase` implements `AsRef` to `RawRef` and `LayoutRef` when `S: RawData`
2323
//! 2. `ArrayBase` implements `AsMut` to `RawRef` when `S: RawDataMut`
24-
//! 3. `ArrayBase` implements `AsMut` to `LayoutRef` unconditionally
25-
//! 4. `ArrayRef` implements `AsMut` to `RawRef` and `LayoutRef` unconditionally
26-
//! 5. `RawRef` implements `AsMut` to `LayoutRef`
27-
//! 6. `RawRef` and `LayoutRef` implement `AsMut` to themselves
24+
//! 3. `ArrayBase` implements `AsRef` and `AsMut` to `LayoutRef` unconditionally
25+
//! 4. `ArrayRef` implements `AsRef` and `AsMut` to `RawRef` and `LayoutRef` unconditionally
26+
//! 5. `RawRef` implements `AsRef` and `AsMut` to `LayoutRef`
27+
//! 6. `RawRef` and `LayoutRef` implement `AsRef` and `AsMut` to themselves
2828
//!
2929
//! This allows users to write a single method or trait implementation that takes `T: AsRef<RawRef<A, D>>`
3030
//! or `T: AsRef<LayoutRef<A, D>>` and have that functionality work on any of the relevant array types.
3131
32-
use core::ops::{Deref, DerefMut};
32+
use core::{
33+
borrow::{Borrow, BorrowMut},
34+
ops::{Deref, DerefMut},
35+
};
3336

34-
use crate::{ArrayBase, ArrayRef, Data, DataMut, Dimension, LayoutRef, RawData, RawDataMut, RawRef};
37+
use crate::{Array, ArrayBase, ArrayRef, Data, DataMut, Dimension, LayoutRef, RawData, RawDataMut, RawRef};
3538

3639
// D1: &ArrayBase -> &ArrayRef when data is safe to read
3740
impl<S, D> Deref for ArrayBase<S, D>
@@ -286,3 +289,59 @@ impl<A, D: Clone> Clone for LayoutRef<A, D>
286289
}
287290

288291
impl<A, D: Clone + Copy> Copy for LayoutRef<A, D> {}
292+
293+
impl<S, D> Borrow<RawRef<S::Elem, D>> for ArrayBase<S, D>
294+
where S: RawData
295+
{
296+
fn borrow(&self) -> &RawRef<S::Elem, D>
297+
{
298+
self.as_ref()
299+
}
300+
}
301+
302+
impl<S, D> BorrowMut<RawRef<S::Elem, D>> for ArrayBase<S, D>
303+
where S: RawDataMut
304+
{
305+
fn borrow_mut(&mut self) -> &mut RawRef<S::Elem, D>
306+
{
307+
self.as_mut()
308+
}
309+
}
310+
311+
impl<S, D> Borrow<ArrayRef<S::Elem, D>> for ArrayBase<S, D>
312+
where S: Data
313+
{
314+
fn borrow(&self) -> &ArrayRef<S::Elem, D>
315+
{
316+
&**self
317+
}
318+
}
319+
320+
impl<S, D> BorrowMut<ArrayRef<S::Elem, D>> for ArrayBase<S, D>
321+
where
322+
S: DataMut,
323+
D: Dimension,
324+
{
325+
fn borrow_mut(&mut self) -> &mut ArrayRef<S::Elem, D>
326+
{
327+
&mut **self
328+
}
329+
}
330+
331+
impl<A, D> ToOwned for ArrayRef<A, D>
332+
where
333+
A: Clone,
334+
D: Dimension,
335+
{
336+
type Owned = Array<A, D>;
337+
338+
fn to_owned(&self) -> Self::Owned
339+
{
340+
self.to_owned()
341+
}
342+
343+
fn clone_into(&self, target: &mut Array<A, D>)
344+
{
345+
target.zip_mut_with(self, |tgt, src| tgt.clone_from(src));
346+
}
347+
}

0 commit comments

Comments
 (0)