Skip to content

Commit

Permalink
Uses a simple fix to enable arraybase to be covariant. (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
akern40 authored Mar 1, 2025
1 parent c7391e9 commit 41bace1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,15 +1282,15 @@ pub type Ixs = isize;
// may change in the future.
//
// [`.offset()`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.offset-1
pub struct ArrayBase<S, D>
where S: RawData
pub struct ArrayBase<S, D, A = <S as RawData>::Elem>
where S: RawData<Elem = A>
{
/// Data buffer / ownership information. (If owned, contains the data
/// buffer; if borrowed, contains the lifetime and mutability.)
data: S,
/// A non-null pointer into the buffer held by `data`; may point anywhere
/// in its range. If `S: Data`, this pointer must be aligned.
ptr: std::ptr::NonNull<S::Elem>,
ptr: std::ptr::NonNull<A>,
/// The lengths of the axes.
dim: D,
/// The element count stride per axis. To be parsed as `isize`.
Expand Down
14 changes: 14 additions & 0 deletions tests/variance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use ndarray::{Array1, ArrayView1};

fn arrayview_covariant<'a: 'b, 'b>(x: ArrayView1<'a, f64>) -> ArrayView1<'b, f64>
{
x
}

#[test]
fn test_covariance()
{
let x = Array1::zeros(2);
let shorter_view = arrayview_covariant(x.view());
assert_eq!(shorter_view[0], 0.0);
}

0 comments on commit 41bace1

Please sign in to comment.