Skip to content

Commit

Permalink
Drop MaybeUninit when failing to read entire slice
Browse files Browse the repository at this point in the history
From the example in #264, the following output is seen:

+1
+1
-1: Bar(1)
-1: Bar(2)
Err(Incomplete(NeedSize { bits: 8 }))
  • Loading branch information
wcampbell0x2a committed Jan 20, 2023
1 parent f712a6b commit 81c1c94
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,19 @@ mod const_generics_impl {
// and never return it in case of error
let mut slice: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() };
let mut rest = input;
for item in slice.iter_mut() {
let (new_rest, value) = T::read(rest, ctx)?;
for (n, item) in slice.iter_mut().enumerate() {
let (new_rest, value) = match T::read(rest, ctx) {
Ok(it) => it,
Err(err) => {
// For each item in the array, drop if we allocated it.
for item in &mut slice[0..n] {
unsafe {
item.assume_init_drop();
}
}
return Err(err);
}
};
item.write(value);
rest = new_rest;
}
Expand Down

0 comments on commit 81c1c94

Please sign in to comment.