Skip to content

Commit 41bace1

Browse files
authored
Uses a simple fix to enable arraybase to be covariant. (#1480)
See rust-lang/rust#115799 and rust-lang/rust#57440 for more details.
1 parent c7391e9 commit 41bace1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1282,15 +1282,15 @@ pub type Ixs = isize;
12821282
// may change in the future.
12831283
//
12841284
// [`.offset()`]: https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.offset-1
1285-
pub struct ArrayBase<S, D>
1286-
where S: RawData
1285+
pub struct ArrayBase<S, D, A = <S as RawData>::Elem>
1286+
where S: RawData<Elem = A>
12871287
{
12881288
/// Data buffer / ownership information. (If owned, contains the data
12891289
/// buffer; if borrowed, contains the lifetime and mutability.)
12901290
data: S,
12911291
/// A non-null pointer into the buffer held by `data`; may point anywhere
12921292
/// in its range. If `S: Data`, this pointer must be aligned.
1293-
ptr: std::ptr::NonNull<S::Elem>,
1293+
ptr: std::ptr::NonNull<A>,
12941294
/// The lengths of the axes.
12951295
dim: D,
12961296
/// The element count stride per axis. To be parsed as `isize`.

tests/variance.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use ndarray::{Array1, ArrayView1};
2+
3+
fn arrayview_covariant<'a: 'b, 'b>(x: ArrayView1<'a, f64>) -> ArrayView1<'b, f64>
4+
{
5+
x
6+
}
7+
8+
#[test]
9+
fn test_covariance()
10+
{
11+
let x = Array1::zeros(2);
12+
let shorter_view = arrayview_covariant(x.view());
13+
assert_eq!(shorter_view[0], 0.0);
14+
}

0 commit comments

Comments
 (0)