-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate previous
ReadInto
into 2 traits.
`ReadIntoSingle` for single data item, and `ReadInto` for complex data. Also updated tests.
- Loading branch information
1 parent
f04635f
commit 166fd46
Showing
12 changed files
with
398 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! Helper for initializing an array of `MaybeUninit<T>` elements. | ||
use std::mem::MaybeUninit; | ||
|
||
pub(crate) struct InitializingArray<'a, T, const N: usize> { | ||
array: &'a mut [MaybeUninit<T>; N], | ||
len: usize, | ||
} | ||
|
||
impl<T, const N: usize> Drop for InitializingArray<'_, T, N> { | ||
fn drop(&mut self) { | ||
for i in 0..self.len { | ||
unsafe { | ||
self.array[i].as_mut_ptr().drop_in_place(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T, const N: usize> InitializingArray<'a, T, N> { | ||
pub(crate) fn new(array: &'a mut [MaybeUninit<T>; N]) -> Self { | ||
Self { array, len: 0 } | ||
} | ||
|
||
/// Use [usize::unchecked_add] if it's stablized. | ||
pub(crate) unsafe fn push_unchecked(&mut self, value: T) { | ||
self.array.get_unchecked_mut(self.len).write(value); | ||
// Safety: We just wrote to the array. | ||
self.len = self.len.wrapping_add(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.