|
21 | 21 | //! To mitigate these problems, `ndarray` also provides `AsRef` and `AsMut` implementations as follows:
|
22 | 22 | //! 1. `ArrayBase` implements `AsRef` to `RawRef` and `LayoutRef` when `S: RawData`
|
23 | 23 | //! 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 |
28 | 28 | //!
|
29 | 29 | //! This allows users to write a single method or trait implementation that takes `T: AsRef<RawRef<A, D>>`
|
30 | 30 | //! or `T: AsRef<LayoutRef<A, D>>` and have that functionality work on any of the relevant array types.
|
31 | 31 |
|
32 |
| -use core::ops::{Deref, DerefMut}; |
| 32 | +use core::{ |
| 33 | + borrow::{Borrow, BorrowMut}, |
| 34 | + ops::{Deref, DerefMut}, |
| 35 | +}; |
33 | 36 |
|
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}; |
35 | 38 |
|
36 | 39 | // D1: &ArrayBase -> &ArrayRef when data is safe to read
|
37 | 40 | impl<S, D> Deref for ArrayBase<S, D>
|
@@ -286,3 +289,59 @@ impl<A, D: Clone> Clone for LayoutRef<A, D>
|
286 | 289 | }
|
287 | 290 |
|
288 | 291 | 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