From 41bace11a7ad456911ce62bb7b012d6332ec0af1 Mon Sep 17 00:00:00 2001 From: akern40 Date: Fri, 28 Feb 2025 19:22:52 -0500 Subject: [PATCH] Uses a simple fix to enable arraybase to be covariant. (#1480) See rust-lang/rust#115799 and rust-lang/rust#57440 for more details. --- src/lib.rs | 6 +++--- tests/variance.rs | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 tests/variance.rs diff --git a/src/lib.rs b/src/lib.rs index f0b64028f..9ba3b6728 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 -where S: RawData +pub struct ArrayBase::Elem> +where S: RawData { /// 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, + ptr: std::ptr::NonNull, /// The lengths of the axes. dim: D, /// The element count stride per axis. To be parsed as `isize`. diff --git a/tests/variance.rs b/tests/variance.rs new file mode 100644 index 000000000..e72805ff7 --- /dev/null +++ b/tests/variance.rs @@ -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); +}