Skip to content

Add .reborrow() methods to array views #412

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 1 commit into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 20 additions & 1 deletion src/impl_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ impl<'a, A, D> ArrayView<'a, A, D>
ArrayView::new_(ptr, dim, strides)
}

/// Convert the view into an `ArrayView<'b, A, D>` where `'b` is a lifetime
/// outlived by `'a'`.
pub fn reborrow<'b>(self) -> ArrayView<'b, A, D>
where 'a: 'b
{
unsafe {
ArrayView::new_(self.as_ptr(), self.dim, self.strides)
}
}

/// Split the array view along `axis` and return one view strictly before the
/// split and one view after the split.
///
Expand Down Expand Up @@ -133,7 +143,6 @@ impl<'a, A, D> ArrayView<'a, A, D>
None
}
}

}


Expand Down Expand Up @@ -328,6 +337,16 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
ArrayViewMut::new_(ptr, dim, strides)
}

/// Convert the view into an `ArrayViewMut<'b, A, D>` where `'b` is a lifetime
/// outlived by `'a'`.
pub fn reborrow<'b>(mut self) -> ArrayViewMut<'b, A, D>
where 'a: 'b
{
unsafe {
ArrayViewMut::new_(self.as_mut_ptr(), self.dim, self.strides)
}
}

/// Split the array view along `axis` and return one mutable view strictly
/// before the split and one mutable view after the split.
///
Expand Down
14 changes: 14 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ fn test_matmul_rcarray()
}
}

#[allow(unused)]
fn arrayview_shrink_lifetime<'a, 'b: 'a>(view: ArrayView1<'b, f64>)
-> ArrayView1<'a, f64>
{
view.reborrow()
}

#[allow(unused)]
fn arrayviewmut_shrink_lifetime<'a, 'b: 'a>(view: ArrayViewMut1<'b, f64>)
-> ArrayViewMut1<'a, f64>
{
view.reborrow()
}

#[test]
fn test_mat_mul() {
// smoke test, a big matrix multiplication of uneven size
Expand Down