-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
70 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
use std::mem::{self, MaybeUninit}; | ||
|
||
use itertools::Itertools; | ||
|
||
// Code from [this post](https://www.reddit.com/r/learnrust/comments/lfw6uy/comment/gn16m4o) | ||
pub trait CollectArray: Sized + Iterator { | ||
fn collect_array<const N: usize>(self) -> [Self::Item; N] { | ||
// TODO(nenikitov): Replace with compile-time assertions or const generic expressions | ||
// When it will be supported. | ||
assert!(N > 0 && mem::size_of::<Self::Item>() > 0); | ||
let mut array = MaybeUninit::uninit(); | ||
let array_ptr = array.as_mut_ptr() as *mut Self::Item; | ||
|
||
let mut i = 0; | ||
unsafe { | ||
for item in self { | ||
assert!(i < N); | ||
array_ptr.add(i).write(item); | ||
i += 1; | ||
} | ||
assert!(i == N); | ||
array.assume_init() | ||
} | ||
let mut array = MaybeUninit::<[Self::Item; N]>::uninit().transpose(); | ||
|
||
Itertools::zip_eq(array.iter_mut(), self).for_each(|(dest, item)| _ = dest.write(item)); | ||
|
||
// SAFETY: Every single element in the array is initialized | ||
// because we wrote a valid iterator element into it. | ||
unsafe { array.transpose().assume_init() } | ||
} | ||
} | ||
|
||
impl<T> CollectArray for T where T: Iterator {} | ||
|