-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
src/from_fn.rs
Outdated
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)?; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/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) |
There was a problem hiding this comment.
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
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.
ad3736c
to
7aedc59
Compare
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 theAsMut
bound on the inner type to provide access to the inner array, and composeArray::from_fn
in terms ofArray::try_from_fn
.The result is less complexity and API surface, without subtracting any functionality.