Skip to content

Conversation

tarcieri
Copy link
Member

Also adds tests.

Previously these used the core [T]::as_flattened(_mut) to avoid additional unsafe code, however instead this approach opts to use a similar strategy to how the upstream as_flattened(_mut) is implemented to avoid having to have a bound on the inner ArrayType.

Also adds tests.

Previously these used the core `[T]::as_flattened(_mut)` to avoid
additional unsafe code, however instead this approach opts to use a
similar strategy to how the upstream `as_flattened(_mut)` is implemented
to avoid having to have a bound on the inner `ArrayType`.
@tarcieri tarcieri requested a review from newpavlov September 23, 2025 00:49
@tarcieri
Copy link
Member Author

Note: AFAICT this is non-breaking

@newpavlov
Copy link
Member

newpavlov commented Sep 23, 2025

Wouldn't it be better to also do it for cast_slice_to_core/cast_slice_from_core methods? This way implementation of slice_as_flattened can be kept safe.

@tarcieri
Copy link
Member Author

@newpavlov these methods are straightforward enough. The others aren’t, because the compiler lacks type information about the inner type, which is why it needs the hint.

If you’d like to take a crack at it, go for it

@newpavlov
Copy link
Member

newpavlov commented Sep 23, 2025

The cast methods are also straightforward and their implementation is nothing more than slice::from_raw_parts. My point is that you can completely remove the impl<T, U, const N: usize> Array<T, U> where U: ArraySize<ArrayType<T> = [T; N]> { ... } block and merge its content into impl<T, U> Array<T, U> where U: ArraySize { ... }.

IIUC the ArrayType<T> = [T; N] is redundant since it's already implied by ArraySize and enforced by keeping it sealed.

UPD: Ah, don't mind. We can not do it since N is used in methods signature.

@tarcieri
Copy link
Member Author

Yeah, due to the array sizes you need some kind of linkage that connects the two types, which is what the bound provides

@tarcieri tarcieri merged commit 1afb4f1 into master Sep 23, 2025
14 checks passed
@tarcieri tarcieri deleted the remove-bounds-on-slice-as-flattened branch September 23, 2025 13:14
tarcieri added a commit that referenced this pull request Sep 23, 2025
Also makes `as_flattened(_mut)` into `const fn`.

This uses the same approach as `slice_as_flattened(_mut)` (#144), but
constructs a slice of size `U::USIZE`.
tarcieri added a commit that referenced this pull request Sep 23, 2025
Also makes `as_flattened(_mut)` into `const fn`.

This uses the same approach as `slice_as_flattened(_mut)` (#144), but
constructs a slice of size `U::USIZE`.
@tarcieri tarcieri mentioned this pull request Sep 23, 2025
tarcieri added a commit that referenced this pull request Sep 23, 2025
### Added
- `Array::as_(mut_)ptr` (#147)

### Changed
- Remove bounds on `Array::slice_as_flattened(_mut)`; make `const fn` (#144)
- Make `Array::as_(mut_)slice` a `const fn` (#147)
- Make `Array::<Array<T, U>::as_flattened(_mut)` a `const fn` (#147)
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.

2 participants