Skip to content
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

Remove FromFn trait #35

Merged
merged 1 commit into from
Jan 30, 2024
Merged

Remove FromFn trait #35

merged 1 commit into from
Jan 30, 2024

Conversation

tarcieri
Copy link
Member

This trait was a sort of crutch to thunk to constructing the inner array type, but isn't strictly necessary and just adds implementation complexity.

Instead, we can have try_from_fn use the AsMut bound on the inner type to provide access to the inner array, and compose Array::from_fn in terms of Array::try_from_fn.

The result is less complexity and API surface, without subtracting any functionality.

src/from_fn.rs Outdated
Comment on lines 63 to 38
let mut array: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() };
try_from_fn_erased(&mut array, cb)?;
let mut array: Array<MaybeUninit<T>, U> = unsafe { MaybeUninit::uninit().assume_init() };
try_from_fn_erased(array.0.as_mut(), f)?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah joy, this again:

error: this call for this type may be undefined behavior
  --> src/from_fn.rs:34:60
   |
34 |         let mut array: Array<MaybeUninit<T>, U> = unsafe { MaybeUninit::uninit().assume_init() };
   |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It would probably be good to leverage Array::uninit to construct this, but that would currently require adding the ArraySize<ArrayType = [T; N]> bound, which gets viral quickly and would impact several other APIs which call this, so I was trying to avoid it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible add this bound on the method itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the bound exists on the method then it needs to be added to everything which calls the method, e.g. the FromIterator impl.

I think I'd like to try a PR which finds a sane way to remove the bound from Array::uninit so we can use that, which also avoids triggering this clippy lint.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to come up with a solution that satisfies the clippy lint, unfortunately.

7deb1a3 removes the bounds on ArrayType and adds allow(clippy::uninit_assumed_init).

It's not ideal, but allows Array::try_from_fn to use Array::uninit and Array::assume_init, so at least we only need to get things right in those two places.

src/from_fn.rs Show resolved Hide resolved
src/lib.rs Outdated
@@ -416,7 +396,7 @@ where
/// state.
pub unsafe fn assume_init(self) -> Array<T, U> {
// TODO(tarcieri): use `MaybeUninit::array_assume_init` when stable
Array(ptr::read(self.0.as_ptr().cast()))
mem::transmute_copy(&self)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little unclear if this is actually correct and it could probably use a better explanatory comment

@tarcieri
Copy link
Member Author

Gonna roll back to what @newpavlov approved and PR the other changes separately

This trait was a sort of crutch to thunk to constructing the inner array
type, but isn't strictly necessary and just adds implementation
complexity.

Instead, we can have `try_from_fn` use the `AsMut` bound on the inner
type to provide access to the inner array, and compose `Array::from_fn`
in terms of `Array::try_from_fn`.

The result is less complexity and API surface, without subtracting any
functionality.
@tarcieri tarcieri merged commit f3797a4 into master Jan 30, 2024
14 checks passed
@tarcieri tarcieri deleted the remove-fromfn-trait branch January 30, 2024 00:28
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