Repository navigation
promote Array::assume_init to const - #239
Conversation
1148c27 to
355d5e8
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #239 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 7 7
Lines 562 564 +2
=========================================
+ Hits 562 564 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| // Since we have unique ownership of `self`, it's okay to make a copy because we're throwing | ||
| // the original away (and this should all get optimized to a noop by the compiler, anyway). | ||
| mem::transmute_copy(&self) | ||
| mem::transmute_copy(&ManuallyDrop::new(self)) |
There was a problem hiding this comment.
There's already a complex story around just this mem::transmute_copy and whatever approach is being used to satisfy the compiler in this case needs an explanation for what it's doing and I don't think it should just get unceremoniously shoved into the existing text.
How about instead of using ManuallyDrop, what if you just did mem::forget(self) with its own separate comment like // SAFETY: ownership has been transferred to [return_var_name]. Consume the MaybeUninit array to satisfy const drop checking or thereabouts.
There was a problem hiding this comment.
Maybe it's better to use pointer casting here, something like core::ptr::read((&raw const self).cast())?
There was a problem hiding this comment.
i think the argument for the existing impl (for why we need to use transmute_copy) is enough.
functionally, there is only one reason we cannot upgrade this function from fn to const fn, and that is that self is dropped.
from my point of view, wrapping in ManuallyDrop finishes telling the story that we want
- the compiler is not convinced that
N::ArrayType<T>andN::ArrayType<MaybeUninit<T>>have the same size, so we cannot usecore::mem::transmute - by using
core::mem::transmute_copy, we interpret&N::ArrayType<MaybeUninit<T>>as&N::ArrayType<T>and fake dereference/create a copy to arrive atN::ArrayType<T>, but importantly this does not move out of the original ref - since the original ref used to be
&self,selfwould be dropped at the end of the function. by wrapping this inManuallyDrop, my interpretation is that the process of transmuting also takes the responsibility of drop.
changing this to mem::forget also works, but i think transmute with ManuallyDrop more accurately reflects that something else is taking ownership of the underlying memory.
i don't think i like using pointer casting here.
regardless maybe this warrants a comment cleanup once some decision has been reached on whether or not to use ManuallyDrop or forget
There was a problem hiding this comment.
i've pushed an updated comment to better document the use of ManuallyDrop, but i can turn this into a mem::forget if that is the preference.
There was a problem hiding this comment.
@incertia the thing I thought would be nice about splitting them up is being able to separate the documentation for what is effectively a workaround because the compiler can't prove !Drop for an inner type which is always [MaybeDrop<T>; N] but the compiler can't figure that out, rather than having one giant documentation comment for everything, even if it isn't quite as pure a semantic mapping to what's happening
There was a problem hiding this comment.
sounds good, i'll adjust the impl and the doc
|
Is that a general comment? It doesn't resolve the const drop issue |
|
Ah, I missed note in the OP. IIRC it should be impossible to |
|
This is the problem that That happens even with That's why either Edit: as a general comment, it does seem a bit more straightforward |
|
I see. Then I would prefer to use |
i think there can be some dubious implementations of |
It's ultimately bounded on The compiler has all the information it needs to deduce there is a fixed set of impls since no other crate can add them, and that all of them are |
the
rust-versionis 1.85, andtransmute_copyis const since 1.74.we also move
selfintoManuallyDropto prevent droppingselfat the end ofassume_init, which should be a bug in the previous version i think (in the case where droppingN::ArrayType<MaybeUninit<T>>does something crazy).