Skip to content

Adds an array reference type #1440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Mar 18, 2025
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e1ba6ca
Adds an array reference type.
akern40 Oct 4, 2024
cdf5012
Adds some documentation
akern40 Oct 4, 2024
5a5a39c
Fixes some CI/CD issues
akern40 Oct 5, 2024
1acf0e1
More CI/CD fixes
akern40 Oct 5, 2024
09800eb
Last CI/CD fix?
akern40 Oct 5, 2024
0d0a96a
Switches to the "same-repr" approach to allow array views to still be…
akern40 Oct 6, 2024
0f3f8d9
Changes back to Copy-capable ArrayBase and unchanged ArrayBase internals
akern40 Oct 6, 2024
fbafbb2
Removes unused import
akern40 Oct 6, 2024
52b276e
Introduces LayoutRef and starts to move functionality from ArrayBase …
akern40 Oct 11, 2024
a504217
Working version of the conversion to reference types
akern40 Oct 13, 2024
aa58113
Uses a new design with two reference types
akern40 Oct 13, 2024
9a2edb1
Satisfying CI/CD
akern40 Oct 13, 2024
e739d04
Adds Index, equality, IntoIterator, and Hash traits, plus approximates
akern40 Oct 13, 2024
fb7d87c
Finishes conversions of all possible ArrayBase impls to ArrayRef
akern40 Oct 14, 2024
fb24680
Satisfying CI/CD
akern40 Oct 14, 2024
bdc24bd
Adds some aliases to avoid breaking changes, and adds an example of h…
akern40 Oct 14, 2024
c72bf8f
Adds Borrow and ToOwned
akern40 Oct 14, 2024
2cce543
Tests that the *Assign operators work for slices via deref
akern40 Oct 14, 2024
7f70e9e
Somehow missed a `use` for `ToOwned`
akern40 Oct 14, 2024
c835680
Implicitly use deref
akern40 Oct 14, 2024
03fda7f
Adds documentation and aliases for `LayoutRef`
akern40 Oct 20, 2024
407b6c4
Fixes doc links
akern40 Oct 20, 2024
a9b5c37
Adds formatting for ArrayRef
akern40 Oct 20, 2024
8a26c34
Change some examples over to ArrayRef
akern40 Oct 20, 2024
8bb8f39
Adds missed #[repr(transparent)] for RawRef
akern40 Oct 20, 2024
775f06b
Simplifies deref logic and avoids null check
akern40 Oct 20, 2024
6eed17d
Adds documentation to ArrayRef
akern40 Oct 20, 2024
9c9d8c2
Adds missing aliases to ArrayBase from RawRef and LayoutRef
akern40 Oct 21, 2024
4e0bcc1
Adds a short snippet of documentation for `RawRef` and some top-level…
akern40 Oct 21, 2024
1bb06e7
Makes as_ref more explicit through methods on ArrayBase
akern40 Oct 26, 2024
435a9e7
Restore remove_index to DataOwned
akern40 Oct 26, 2024
6fd61c1
Fixes unused imports
akern40 Oct 26, 2024
0dedc3e
Re-fixes the covariant arrayview with the new reference structure
akern40 Mar 18, 2025
a71b509
Fixes the clippy lint and docs broken link
akern40 Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds Borrow and ToOwned
  • Loading branch information
akern40 committed Mar 18, 2025
commit c72bf8f27215080158a5fb05c8956be64152ef7a
71 changes: 65 additions & 6 deletions src/impl_ref_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@
//! To mitigate these problems, `ndarray` also provides `AsRef` and `AsMut` implementations as follows:
//! 1. `ArrayBase` implements `AsRef` to `RawRef` and `LayoutRef` when `S: RawData`
//! 2. `ArrayBase` implements `AsMut` to `RawRef` when `S: RawDataMut`
//! 3. `ArrayBase` implements `AsMut` to `LayoutRef` unconditionally
//! 4. `ArrayRef` implements `AsMut` to `RawRef` and `LayoutRef` unconditionally
//! 5. `RawRef` implements `AsMut` to `LayoutRef`
//! 6. `RawRef` and `LayoutRef` implement `AsMut` to themselves
//! 3. `ArrayBase` implements `AsRef` and `AsMut` to `LayoutRef` unconditionally
//! 4. `ArrayRef` implements `AsRef` and `AsMut` to `RawRef` and `LayoutRef` unconditionally
//! 5. `RawRef` implements `AsRef` and `AsMut` to `LayoutRef`
//! 6. `RawRef` and `LayoutRef` implement `AsRef` and `AsMut` to themselves
//!
//! This allows users to write a single method or trait implementation that takes `T: AsRef<RawRef<A, D>>`
//! or `T: AsRef<LayoutRef<A, D>>` and have that functionality work on any of the relevant array types.

use core::ops::{Deref, DerefMut};
use core::{
borrow::{Borrow, BorrowMut},
ops::{Deref, DerefMut},
};

use crate::{ArrayBase, ArrayRef, Data, DataMut, Dimension, LayoutRef, RawData, RawDataMut, RawRef};
use crate::{Array, ArrayBase, ArrayRef, Data, DataMut, Dimension, LayoutRef, RawData, RawDataMut, RawRef};

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

impl<A, D: Clone + Copy> Copy for LayoutRef<A, D> {}

impl<S, D> Borrow<RawRef<S::Elem, D>> for ArrayBase<S, D>
where S: RawData
{
fn borrow(&self) -> &RawRef<S::Elem, D>
{
self.as_ref()
}
}

impl<S, D> BorrowMut<RawRef<S::Elem, D>> for ArrayBase<S, D>
where S: RawDataMut
{
fn borrow_mut(&mut self) -> &mut RawRef<S::Elem, D>
{
self.as_mut()
}
}

impl<S, D> Borrow<ArrayRef<S::Elem, D>> for ArrayBase<S, D>
where S: Data
{
fn borrow(&self) -> &ArrayRef<S::Elem, D>
{
&**self
}
}

impl<S, D> BorrowMut<ArrayRef<S::Elem, D>> for ArrayBase<S, D>
where
S: DataMut,
D: Dimension,
{
fn borrow_mut(&mut self) -> &mut ArrayRef<S::Elem, D>
{
&mut **self
}
}

impl<A, D> ToOwned for ArrayRef<A, D>
where
A: Clone,
D: Dimension,
{
type Owned = Array<A, D>;

fn to_owned(&self) -> Self::Owned
{
self.to_owned()
}

fn clone_into(&self, target: &mut Array<A, D>)
{
target.zip_mut_with(self, |tgt, src| tgt.clone_from(src));
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy