Skip to content

Commit 8218867

Browse files
authored
Merge pull request #412 from bluss/reborrow-view
Add .reborrow() methods to array views
2 parents 922d475 + 1a5c668 commit 8218867

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/impl_views.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ impl<'a, A, D> ArrayView<'a, A, D>
8282
ArrayView::new_(ptr, dim, strides)
8383
}
8484

85+
/// Convert the view into an `ArrayView<'b, A, D>` where `'b` is a lifetime
86+
/// outlived by `'a'`.
87+
pub fn reborrow<'b>(self) -> ArrayView<'b, A, D>
88+
where 'a: 'b
89+
{
90+
unsafe {
91+
ArrayView::new_(self.as_ptr(), self.dim, self.strides)
92+
}
93+
}
94+
8595
/// Split the array view along `axis` and return one view strictly before the
8696
/// split and one view after the split.
8797
///
@@ -133,7 +143,6 @@ impl<'a, A, D> ArrayView<'a, A, D>
133143
None
134144
}
135145
}
136-
137146
}
138147

139148

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

340+
/// Convert the view into an `ArrayViewMut<'b, A, D>` where `'b` is a lifetime
341+
/// outlived by `'a'`.
342+
pub fn reborrow<'b>(mut self) -> ArrayViewMut<'b, A, D>
343+
where 'a: 'b
344+
{
345+
unsafe {
346+
ArrayViewMut::new_(self.as_mut_ptr(), self.dim, self.strides)
347+
}
348+
}
349+
331350
/// Split the array view along `axis` and return one mutable view strictly
332351
/// before the split and one mutable view after the split.
333352
///

tests/array.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ fn test_matmul_rcarray()
4040
}
4141
}
4242

43+
#[allow(unused)]
44+
fn arrayview_shrink_lifetime<'a, 'b: 'a>(view: ArrayView1<'b, f64>)
45+
-> ArrayView1<'a, f64>
46+
{
47+
view.reborrow()
48+
}
49+
50+
#[allow(unused)]
51+
fn arrayviewmut_shrink_lifetime<'a, 'b: 'a>(view: ArrayViewMut1<'b, f64>)
52+
-> ArrayViewMut1<'a, f64>
53+
{
54+
view.reborrow()
55+
}
56+
4357
#[test]
4458
fn test_mat_mul() {
4559
// smoke test, a big matrix multiplication of uneven size

0 commit comments

Comments
 (0)