Skip to content

Preserve data lifetime in SliceRef/SliceMut subslicing - #47

Open
tim-harding wants to merge 1 commit into
mainfrom
fix-45-subslice-lifetimes
Open

Preserve data lifetime in SliceRef/SliceMut subslicing#47
tim-harding wants to merge 1 commit into
mainfrom
fix-45-subslice-lifetimes

Conversation

@tim-harding

Copy link
Copy Markdown
Owner

Sub-slicing methods were only reachable on SliceRef/SliceMut through Deref, so slice_ref.split_at(n) desugared to (&*slice_ref).split_at(n) and bound the result to the local rather than to the data. That made the standard batching-iterator pattern impossible.

Add self-consuming inherent methods on SliceRef<'a, T> and SliceMut<'a, T>, which shadow the Deref-reached ones and return 'a. SoaIndex now takes a SliceRef/SliceMut so the output lifetime rides in on the argument, keeping the whole path safe. Slice's methods delegate, which also removes split_at_parts.

SliceMut is not Copy, so consuming shadows cost reborrowing. Only the methods named in the issue are shadowed there; iter_mut and friends still reborrow, and as_mut_slice is the explicit reborrow for the rest.

Fixes #45

Sub-slicing methods were only reachable on `SliceRef`/`SliceMut` through
`Deref`, so `slice_ref.split_at(n)` desugared to `(&*slice_ref).split_at(n)`
and bound the result to the local rather than to the data. That made the
standard batching-iterator pattern impossible.

Add `self`-consuming inherent methods on `SliceRef<'a, T>` and
`SliceMut<'a, T>`, which shadow the `Deref`-reached ones and return `'a`.
`SoaIndex` now takes a `SliceRef`/`SliceMut` so the output lifetime rides in
on the argument, keeping the whole path safe. `Slice`'s methods delegate,
which also removes `split_at_parts`.

`SliceMut` is not `Copy`, so consuming shadows cost reborrowing. Only the
methods named in the issue are shadowed there; `iter_mut` and friends still
reborrow, and `as_mut_slice` is the explicit reborrow for the rest.

Fixes #45

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@tim-harding tim-harding self-assigned this Jul 26, 2026
@tim-harding

Copy link
Copy Markdown
Owner Author

I'll sleep on this as it is a breaking change with some consequences to API ergonomics.

@akhilman

Copy link
Copy Markdown

Please hold it for a few days, up to a week. I'l try do an alternative solution.

@akhilman

akhilman commented Aug 2, 2026

Copy link
Copy Markdown

No luck. My idea would not work. I'll publish explanation in the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Methods get and split_at* are reducing slice's lifetime

2 participants