You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Do you see the "bug"? No one else did! The original author only noticed the
154
157
problem when linking to this page years later. This code is kind of dubious
155
-
because abusing the iterator pointers to be *counters* makes them unaligned!
156
-
Our *one job* when using ZSTs is to keep pointers aligned! *forehead slap*
158
+
because abusing the iterator pointers to be _counters_ makes them unaligned!
159
+
Our _one job_ when using ZSTs is to keep pointers aligned! _forehead slap_
157
160
158
161
Raw pointers don't need to be aligned at all times, so the basic trick of
159
-
using pointers as counters is *fine*, but they *should* definitely be aligned
160
-
when passed to `ptr::read`! This is *possibly* needless pedantry
161
-
because `ptr::read` is a noop for a ZST, but let's be a *little* more
162
+
using pointers as counters is _fine_, but they _should_ definitely be aligned
163
+
when passed to `ptr::read`! This is _possibly_ needless pedantry
164
+
because `ptr::read` is a noop for a ZST, but let's be a _little_ more
162
165
responsible and read from `NonNull::dangling` on the ZST path.
163
166
164
167
(Alternatively you could call `read_unaligned` on the ZST path. Either is fine,
165
168
because either way we're making up a value from nothing and it all compiles
166
169
to doing nothing.)
167
170
168
171
<!-- ignore: simplified code -->
172
+
169
173
```rust,ignore
170
174
impl<T> Iterator for RawValIter<T> {
171
175
type Item = T;
@@ -215,8 +219,6 @@ impl<T> DoubleEndedIterator for RawValIter<T> {
215
219
216
220
And that's it. Iteration works!
217
221
218
-
And that's it. Iteration works!
219
-
220
222
One last thing that we need to take into account, is that now when our vec gets dropped it deallocates the memory that was allocated during the time our vec was alive. With ZSTs we did not allocate any memory, and infact we never do. So right now we have unsoundness in our code where we still try deallocate a `NonNull::dangling()` ptr that we use to simulate the ZST in our vec, This means that we would cause an undefined behaviour if we try to deallocate something that we never allocated (obviously and for the right reasons). Lets fix tha, in our raw_vec we are going to tweak our Drop trait and check that we deallocate only types that are sized.
0 commit comments